File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == operator.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== operator.." Watch "== operator.." New topic

== operator..

suresh seeram
Ranch Hand

Joined: Oct 06, 2001
Posts: 42
Hello frineds,
Can any one explain the the result..
public class Test041
static double d;
static float f;
public static void main(String args[])
compare(Long.MAX_VALUE, Long.MAX_VALUE );
compare(Integer.MAX_VALUE, Integer.MAX_VALUE );
compare(Character.MAX_VALUE, Character.MAX_VALUE);
compare(Short.MAX_VALUE, Short.MAX_VALUE );
compare(Byte.MAX_VALUE, Byte.MAX_VALUE );
static void compare(double d, float f)
if (f == d) System.out.print(" equal" );
else System.out.print(" unequal");
equal unqequal equal equal equal
thanks in advance
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Suresh,
Welcome to the world or IEEE 754. the primitive type double is not the same as float in this world. Let the JLS Spec illuminate you (Section 5.1.2):

Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode.

Bill Krieger
Ranch Hand

Joined: Sep 27, 2001
Posts: 53
Interesting. I would have expected "unequal unequal equal equal equal". When the Long.MAX_VALUE is converted to double, some precision is lost. When the Long.MAX_VALUE is converted to float, even more bits of precision are lost. When the float version is converted to double for the comparison, is the extra precision just accidently coming out equal?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
With this line:
System.out.println("d= " + d +
"\n" + "f= " + f + "\n" + "(double)f: " + (double)f);
we can see the following output:
d= 9.223372036854776E18
f= 9.223372E18
(double)f: 9.223372036854776E18
d= 2.147483647E9
f= 2.14748365E9
(double)f: 2.147483648E9
d= 65535.0
f= 65535.0
(double)f: 65535.0
d= 32767.0
f= 32767.0
(double)f: 32767.0
d= 127.0
f= 127.0
(double)f: 127.0
Also if we add:
compare(5555555555L, 5555555555L);
the output:
d= 5.555555555E9
f= 5.5555553E9
(double)f: 5.555555328E9
I would say that comparing floating point numbers with == is very risky. I think it's better to establish the precission that is going to be meaningfull for our comparision before doing the actual comparision. Maybe rounding...
Please correct or confirm.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: == operator..
It's not a secret anymore!