views:

171

answers:

7

Hi, Could anyone please me why the output of the following programme is not " different different"?

public static void main(String[] args)
{

float f1=3.2f;
float f2=6.5f;

if(f1==3.2)
System.out.println("same");
else
System.out.println("different");

if(f2==6.5)
System.out.println("same");
else
System.out.println("different");
}

o/p :different same

+1  A: 

This can help understand

RC
+3  A: 

Because 3.2f is a float value and 3.2 is a double value. Floating point numbers are always slightly inaccurate because binary representation cannot implement them accurately, so comparing them for exact equality is a bad idea. Particularly comparing floats with doubles. Expressions such as 3.2f == 3.2f are usually okay, but even those can fail in some languages, e.g. if they represent numbers in registers more accurately than in memory.

Kilian Foth
+1 because this explanation is much more thorough than I bothered to write. :)
edl
I think the question is why should one of them compare as equal. The OP expects them both to be unequal.
GregS
"Floating point numbers are *always* slightly inaccurate" -> wrong. float and double can represent fractions with finite binary representations like 6.5 just fine. See my answer for details.
FredOverflow
True, but remembering which decimal fractions are accurate and which aren't, and using or avoiding == based on that, is really really cumbersome - in fact, not having to remember these details is the entire point of having a float data type with well-defined semantics in the first place! Therefore i always recommend people to regard all floating values as inaccurate, even though technically some aren't.
Kilian Foth
A: 

"3.2f" is a of type float. "3.2" is of type double.

Frank Shearar
A: 

Also, I think anything with a decimal defaults into a double, so when you do a comparison, you have to add 'f' as well like if(f2==6.4f).

edl
+4  A: 

Because 3.2 is not representable exactly as floating point number and 6.5 is (hint: 6.5 = 13 * 2^(-1)), as well as the fact that 3.2 is a double literal but 3.2f is a float literal.

GregS
A: 

try not to compare the floating type value.

HZhang
+8  A: 

6.5 has a finite binary representation: 110.1

Any floating type with at least 4 significant bits can represent this number perfectly.

110.100000000000000000000 (float)
= 6.5

110.10000000000000000000000000000000000000000000000000 (double)
= 6.5

3.2 on the other hand has an infinite binary representation: 101.0011001100110011...

float and double don't have infinite precision and thus can only approximate this number :(

101.001100110011001100110 (float)
= 3.2000000476837158203125

101.00110011001100110011001100110011001100110011001101 (double)
= 3.20000000000000017763568394002504646778106689453125

As you can clearly see, these numbers are not the same!

FredOverflow
@FredOverflow: +1 to you because it's the only answer that actually answers the question asked. I don't know why the OP accepted the other answer which definitely does not explain why one test give *"same"* and the other *"different"*.
NoozNooz42
I would just add that the other reason for this is that Java promotes less precise types to more precise types for comparison, which makes the comparison of 3.2 and 3.2f possible. If it truncated the longer type here, these would be the same, but thankfully it (like C) uses *widening primitive conversions* to do what most people would probably expect: http://bit.ly/d8Yx3N
tgamblin
@NoozNooz42: Hey, I got it correct also (:
GregS
@GregS: trudat! +1 to you too :)
NoozNooz42
Thanks Fred for your answer.
Alvin