## The Design of Software (CLOSED)A public forum for discussing the design of software, from the user interface to the code architecture. Now closed. |
||

The "Design of Software" discussion group has been merged with the main
Joel on Software discussion group.
The archives will remain online indefinitely. |
Has anybody got an experience of using _matherr in Visual C++
The example in the Help seems to suggest you just define your own function if you want to be called for maths exceptions, but I seem unable to trigger it (C++ console mode project). I figured it might be name mangling so I put extern "C" round my function, and this doesn't seem to help. It is probably something stupid, but I just can't see it. Thanks in advance.
Are you clear about what "maths exceptions" means? ISTR it means invalid operations in math functions like log of negative numbers, inverse sin of numbers greater than 1, things like that.
It doesn't mean float divide by zero; for that I think you have to enable floating point interrupts in the processor and then catch the exceptions when they occur. Do some searching on MSDN for more information. I forget the exact routine names off the top of my head, but I think they may begin with _fpXXXXX.
Thanks Ian yes I am
I want to capture things like logs of negatives, underflow, overflow, etc. I have an expression evaluator that will evaluate mathematical expressions (sin, cos, tan, log, atan, etc etc) The problem is to capture the maths exception when one of these functions is called with a stupid value.
You might have to use _controlfp or _control87 to enable floating-point exceptions first? I'm pretty sure that they start out disabled, and you have to do this to enable them. (I've never used _matherr to trap them, though, that may use some different mechanism.)
Tom_ Monday, September 19, 2005
I believe that _matherr deals with invalid arguments to the math functions like log and log10. _controlfp deals with floating point traps in the hardware. The two are complementary but non-overlapping.
For example, _matherr will not catch an overflow caused by a simple multiplication of two doubles, but it will catch an overflow caused by an excessively large argument to the exp() function. (The former because floating point multiply is not a math library function, it is a direct processor operation.)
#include <iostream>
#include <cmath> using namespace std; int main() { double x, y; do { cout << "x: "; cin >> x; y = exp(x); cout << "exp(x) = " << y << "\n" << endl; } while (x != 0); return 0; } int _matherr( struct _exception *except ) { if( except->type == _OVERFLOW ) { cout << "Error: " << except->name << "(" << except->arg1 << ") overflowed" << endl; except->retval = 8.21841e+307; return 1; } else { return 0; } } D:\overflow>cl -GX overflow.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86 Copyright (C) Microsoft Corp 1984-1998. All rights reserved. overflow.cpp Microsoft (R) Incremental Linker Version 6.00.8447 Copyright (C) Microsoft Corp 1992-1998. All rights reserved. /out:overflow.exe overflow.obj D:\overflow>overflow x: 707 exp(x) = 1.11224e+307 x: 708 exp(x) = 3.02338e+307 x: 709 exp(x) = 8.21841e+307 x: 710 Error: exp(710) overflowed exp(x) = 8.21841e+307 x: 711 Error: exp(711) overflowed exp(x) = 8.21841e+307 |

Powered by FogBugz