Assign double constant to float variable without warning in C?

10,099

Solution 1

-Wall does not enable warnings about loss of precision, truncation of values, etc. because these warnings are annoying noise and "fixing" them requires cluttering correct code with heaps of ugly casts. If you want warnings of this nature you need to enable them explicitly.

Also, your use of printf has nothing to do with the precision of the actual variables, just the precision printf is printing at, which defaults to 6 places after the decimal point.

Solution 2

%f can be used with float and double. If you want more precision use

printf("f: %.16f",d);

And this is what's going on under the hood:

float f = 3.1415926;  // The double 3.1415926 is truncated to float
double d = 3.1415926;  
printf("f: %f\n", f);  
printf("d: %f\n", d);  
f = 3.1415926f;       // Float is specified
printf("f: %f\n", f);  
int i = 3.1415926;    // Truncation from double to int
printf("i: %d\n", i); 

Solution 3

If you want to get warnings for this, I believe that -Wconversion flags them in mainline gcc-4.3 and later.

If you happen to use OS X, -Wshorten-64-to-32 has been flagging them in Apple's GCC since gcc-4.0.1. I believe that clang matches the mainline gcc behavior, however.

Share:
10,099
OnTheEasiestWay
Author by

OnTheEasiestWay

Updated on June 07, 2022

Comments

  • OnTheEasiestWay
    OnTheEasiestWay over 1 year

    In C programming language, the floating point constant is double type by default
    so 3.1415 is double type, unless use 'f' or 'F' suffix to indicate float type.

    I assume const float pi = 3.1415 will cause a warning, but actually not.

    when I try these under gcc with -Wall:

    float f = 3.1415926;  
    double d = 3.1415926;  
    printf("f: %f\n", f);  
    printf("d: %f\n", d);  
    f = 3.1415926f;  
    printf("f: %f\n", f);  
    int i = 3.1415926;  
    printf("i: %d\n", i);  
    

    the result is:

    f: 3.141593  
    d: 3.141593  
    f: 3.141593  
    i: 3
    

    the result (including double variable) obviously lose precision, but compile without any warning.
    so what did the compiler do with this? or did I misunderstand something?

  • Bill Lynch
    Bill Lynch over 12 years
    Shouldn't that be %lf for a double?
  • Harsh Pathak
    Harsh Pathak over 12 years
    @sharth: lf and f are different for scanf but the same in printf
  • Admin
    Admin over 12 years
    The variadic nature of printf ensures the floats are implicitly cast to doubles when passed?
  • R.. GitHub STOP HELPING ICE
    R.. GitHub STOP HELPING ICE over 12 years
    Yes, although that's rather unrelated to OP's question.
  • supercat
    supercat over 9 years
    I wonder which is greater: the number of mistakes which "possible loss of precision from double to float" messages catch, or the number of mistakes that result when code a programmer who has to explicitly forces lots of things to float to make the compiler happy, also forces to float something which shouldn't have been [e.g. const float oneTenth = 1.0f/10.0f; float value1 = 8.0f * oneTenth; double value2 = 4.0f * oneTenth; Note that if oneTenth could have been double, everything would have worked perfectly.