C ++: how to throw EXCEPTION_FLT_UNDERFLOW?

Well, that sounds weird, but I need some code that calls EXCEPTION_FLT_UNDERFLOW. I already have code to handle this exception. Now I need a sample that throws this damn EXCEPTION_FLT_UNDERFLOW. Any tips?

+4
source share
4 answers

Assuming you need the actual code that will call this:

#include <float.h> int main() { _controlfp_s(NULL, 0, _MCW_EM); // enable all floating point exceptions float f= 1.0f; while (f) { f/=2.0f; // __asm fwait; // optional, if you want to trap the underflow sooner } return 0; } 
+3
source

Try:

 RaiseException(EXCEPTION_FLT_UNDERFLOW, EXCEPTION_NONCONTINUABLE, 0, NULL); 
+2
source

Even in C99 there is no portable way to do this. This works on Linux:

 #define _GNU_SOURCE #include <fenv.h> int main(void) { fesetenv(FE_NOMASK_ENV); feraiseexcept(FE_UNDERFLOW); return 0; } 

but without calls to #define _GNU_SOURCE and fesetenv (which are not fesetenv ), an exception is not thrown (== does not start SIGFPE , in Unix terms), it just sets a flag. And no MSVC iteration supports <fenv.h> , so you are stuck with completely non-standard on Windows.

However, it seems to be the equivalent of Windows:

 #include <float.h> #pragma fenv_access (on) int main(void) { _controlfp_s(0, 0, _MCW_EM); /* throw all floating point exceptions */ /* trigger floating-point underflow here */ } 

You will need to invoke the underflow condition using floating point operations. I do not know how to do it from my head. It would be best to do this manually in assembly language, to avoid interference from the compiler (not even portability anymore, yes, but if you're on Windows, you probably don't need any processor, but x86).

+1
source

throw EXCEPTION_FLT_UNDERFLOW;

-2
source

All Articles