Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting FLT_EVAL_METHOD not working

#include <stdio.h>
#include <float.h>

#ifdef FLT_EVAL_METHOD
    #undef FLT_EVAL_METHOD
    #define FLT_EVAL_METHOD 1
#else
    #define FLT_EVAL_METHOD 1
#endif

int main(void)
{
    printf("%f\n%f\n", FLT_MAX, DBL_MAX);
    float fp = FLT_MAX;
    printf("%f\n", fp + 1);
    printf("%f\n", FLT_MAX + 1);
    printf("%f\n", (float) FLT_MAX + 1);
}

The expected output is:

340282346638528859811704183484516925440.000000
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
340282346638528859811704183484516925441.000000
340282346638528859811704183484516925441.000000
340282346638528859811704183484516925441.000000

But the actual output is:

340282346638528859811704183484516925440.000000
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
340282346638528859811704183484516925440.000000
340282346638528859811704183484516925440.000000
340282346638528859811704183484516925440.000000

Since FLT_EVAL_METHOD is set to 1, shouldn't all float expressions evaluated as doubles? What does "evaluate float and double as double, and long double as long double." mean? Are floats treated identical to doubles?

like image 433
nalzok Avatar asked Mar 13 '23 21:03

nalzok


1 Answers

FLT_EVAL_METHOD is not a switch that the programmer uses to control the compiler. It's a means for the compiler/platform to report to the programmer how floating-point expressions will be evaluated. You're not supposed to change its value, and doing so is not supposed to have any effect on anything.

like image 58
Stephen Canon Avatar answered Mar 24 '23 10:03

Stephen Canon