Take the 2-minute tour ×

In C:

float a = 1.3;
if (a == 1.3)
    printf("Hello c");  
    printf("Bye c"); 

//output: Bye c

In Java:

float a = 1.3;
if (a == 1.3)
    System.out.println("Hello java");
    System.out.println("Bye java");

//output: Hello java

Why is this so?

share|improve this question

migrated from programmers.stackexchange.com Nov 14 '11 at 6:47


I've done several small corrections in your code, there was no way your original code worked. Have you seen your code produce the output or did you just copied it from somewhere? –  Yannis Nov 14 '11 at 6:11

3 Answers 3

You cannot exactly represent "1.3" as a binary float.

The conversion of the literal 1.3 to a float will result in a floating point number that is close to but not exactly equal to 1.3.

In the Java case you struck lucky because the JVM interprets the "1.3" as a standard java float and converts to the same almost 1.3. binary number. In the C case the standard says 1.3 should be interpreted as a "double" and converts it to a binary floating point that is a little closer to the decimal value 1.3 than the ordinary float.

The bottom line is that you should never compare floats for equality as small variations in storage, order of calculation etc. will produce slightly different results. You should always "bracket" any float comparison like so:

if (a > 1.299 && a < 1.301) // something close to 1.3
share|improve this answer
Just to be a complete nit-pick: in C, 1.3 is always a double. The syntax for (single-precision) float literals is 1.3f. –  tdammers Nov 14 '11 at 6:37
@tdammmers -- thanks. I was not sure whether this was compiler specific or mandated by the standard. –  James Anderson Nov 14 '11 at 6:52

Just to help you wrap your brain around the answer, let me give you a simple analogy. Remember that floating point numbers are approximate. Many numbers cannot be exactly represented. Now, imagine we do the same thing with decimals, say we use six digits after the decimal place.

How do we represent 1/3? .333333 is the best we can do. But now 3 * 1/3 will not equal 1.

How do we represent 2/3? .666666 will at least ensure 2 * 1/3 = 2/3. But 2/3 + 1/3 will not equal 1.

We can represent 2/3 as .666667. But then 2 * 1/3 won't equal 2/3. But at least 1/3 + 2/3 will equal one.

So the point is, with approximate representations, you can't ensure exactly right answers, only close enough answers. In general, you should never compare floating point numbers for equality unless you are 100% positive you understand what you are doing and why.

share|improve this answer

The first thing that must be known with respect to C is that it is very wrong to test 2 floating point numbers for equality because of the implementation of the floating point numbers by the compiler. '1.3' when store in a float variable ,you have not filled up all the decimal place to which the compiler stores and will likely to be filled with some digits thus giving rise to the failure for the test of equality

share|improve this answer

Your Answer


