strange output in comparison of float with float literal

c, c++, double-precision, floating-point

Solution

This happens because in your statement

  if(f == 0.7)

the 0.7 is treated as a double. Try 0.7f to ensure the value is treated as a float:

  if(f == 0.7f)

But as Michael suggested in the comments below you should never test for exact equality of floating-point values.

Problem

``` float f = 0.7; if( f == 0.7 ) printf("equal"); else printf("not equal"); ``` Why is the output `not equal` ? Why does this happen?

Original source