c++visual-studio-2010seh

How to throw EXCEPTION_FLT_UNDERFLOW?


I need a sample code, that throws EXCEPTION_FLT_UNDERFLOW. I already have code to handle that exception. Now I need sample, that throws it. Any advises?


Solution

  • Assuming you want actual code that will trigger 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;
    }