Frage

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);                   
    }
War es hilfreich?

Lösung

old_control & ~_EM_ZERODIVIDE | _EM_OVERFLOW

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

Andere Tipps

If you are using C++ then it usually works better to have a class that handles two basic floating-point exception related operations.

1) Temporarily disabling specific exceptions. 2) Temporarily enabling specific exceptions.

In both cases the class can take care of making the requested change to the floating-point exception settings and then resetting it. You can find an example of such classes here:

http://randomascii.wordpress.com/2012/04/21/exceptional-floating-point/

Depending on your needs you can either use them as-is, or copy the constructor/destructor implementations to your functions.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top