From: Greg C. <chi...@co...> - 2006-03-06 15:24:04
|
On 2006-3-6 14:24 UTC, lz...@vi... wrote: > > How can I trap math errors ? > i tried with definitions of > my own "matherr" The nonstandard matherr() extension generally deals only with exceptional conditions encountered inside functions in the math library. If you call sqrt(-1.0), it might be called. Some platforms have deprecated it; others might not supply it at all. > double n= 999; > cout << (double) n/0 << endl; No math-library function is called here. This would generally be a run-time exception. The compiler emits a warning unless you've inhibited it. With *nix, you'd use a SIGFPE handler. I've tried writing one for MinGW, but wasn't able to get it to clear the exception and continue. With msvc, you'd use SEH, which doesn't work well with MinGW. I know of no good way to trap math exceptions with MinGW. It is best to avoid writing code that raises them, anyway. > but > it seems to be ignored by the compiler and the application "crash" !!! Division by zero can be avoided by checking the divisor before performing the division. I think that's the best way. I compiled your program, with this obvious change: - include <math.h> + #include <math.h> using three different MinGW releases, and they all printed 1.#INF That's because of the way the compiler sets the floating-point control word by default; perhaps you have changed it to unmask the ZM bit that controls the zero-divide exception. |