Floating point arithmetic and machine epsilon

前端 未结 2 1688
北恋
北恋 2020-12-19 10:09

I\'m trying to compute an approximation of the epsilon value for the float type (and I know it\'s already in the standard library).

The epsilon values o

2条回答
  •  一向
    一向 (楼主)
    2020-12-19 10:33

    A C99 C compiler can evaluate floating-point expressions as if they were of a more precise floating-point type than their actual type.

    The macro FLT_EVAL_METHOD is set by the compiler to indicate the strategy:

    -1 indeterminable;

    0 evaluate all operations and constants just to the range and precision of the type;

    1 evaluate operations and constants of type float and double to the range and precision of the double type, evaluate long double operations and constants to the range and precision of the long double type;

    2 evaluate all operations and constants to the range and precision of the long double type.

    For historical reasons, two common choices when targeting the x86 processors are 0 and 2.

    File m.c is your first program. If I compile it, using my compiler, thus, I obtain:

    $ gcc -std=c99 -mfpmath=387 m.c
    $ ./a.out 
    float eps = 1.084202e-19
    $ gcc -std=c99  m.c
    $ ./a.out 
    float eps = 1.192093e-07
    

    If I compile this other program below, the compiler sets the macro according to what it does:

    #include 
    #include 
    
    int main(){
      printf("%d\n", FLT_EVAL_METHOD);
    }
    

    Results:

    $ gcc -std=c99 -mfpmath=387 t.c
    $ ./a.out 
    2
    $ gcc -std=c99 t.c
    $ ./a.out 
    0
    

提交回复
热议问题