Search code examples
c++cfloating-pointfloating-point-exceptions

Floating point: Disable specific exception


I set tow functions, one to enable exception for floating point and one to disable exception.
In the code bellow, I have enabled tow exception in one time (_EM_ZERODIVIDE and _EM_OVERFLOW), after I need to disable just _EM_ZERODIVIDE and let _EM_OVERFLOW enabled.
What argument to pass to my function ResetFloatExeption (....).
See code for detail.

    #include <stdio.h>
    #include <float.h>
    #include <math.h>
    #pragma fenv_access (on)


    // SetFloatExeption
    void SetFloatExeption (unsigned int new_control)
    {
         _clearfp(); 
        _controlfp_s(0,new_control, _MCW_EM);
    }

    // ResetFloatExeption
    void ResetFloatExeption (unsigned int new_control) 
    {
         _clearfp(); 
        _controlfp_s(0,new_control, _MCW_EM);
    }

    //***************  main  ****//
    void main( void )
    {
        unsigned int old_control;
        double a = 1.1;
        double b = 0.0;
        float d;

        _controlfp_s(&old_control,0,0);         

        // Enable exception _EM_ZERODIVIDE and _EM_OVERFLOW

        SetFloatExeption (old_control & ~(_EM_ZERODIVIDE | _EM_OVERFLOW) );
         // Here, How to call ResetFloatExeption to disable juste _EM_ZERODIVIDE and let  _EM_OVERFLOW  enabled
        ResetFloatExeption(old_control & ???);      
        fprintf(stdout,"a/b= %.10e\n",a/b);     

        int exponent = 50;
        d = pow(10.0, exponent);                

        printf("d = %f\n",d);                   
    }

Solution

  • old_control & ~_EM_ZERODIVIDE | _EM_OVERFLOW
    

    Both of your functions do the same. May be you should delete one?