File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes float vs double equality Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "float vs double equality" Watch "float vs double equality" New topic
Author

float vs double equality

Omkar onky
Greenhorn

Joined: Oct 12, 2012
Posts: 11
I am preparing for my Job.
Going to write OCJP 6 certification.
Here i got a weird problem.
I am comparing floating point and double literals.
In SCJP kathy seirra book, kathy mentioned: a float value never equals to double value, in comparison always we get false.

If that is correct what about this code?

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

Omkar onky wrote:In SCJP kathy seirra book, kathy mentioned: a float value never equals to double value, in comparison always we get false.
If that is correct what about this code?

I think you might want to double-check what she actually said in the book, because if she did, then it's plainly wrong - as you have conclusively proved. In most comparisons involving numeric types of different sizes, the smaller one will be converted to the larger before the comparison takes place, and since floating-point numbers are notorious for not holding exact values - particularly for fractions involving 10ths - the double version will be different from a float expanded to a double.

I suspect that if you try:
System.out.println( 0.75 == 0.75f );
and
System.out.println( 0.0625 == 0.0625f );
you'll find that they print out true as well.

I suggest you read this. It may well help to explain some of this stuff.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Omkar onky
Greenhorn

Joined: Oct 12, 2012
Posts: 11
thanks for your reply Winston.
I found these comparisons also giving true!
System.out.println(2.25f==2.25);
System.out.println(2.75f==2.75);
System.out.println(2.125f==2.125);
System.out.println(2.625f==2.625);

.25, 75, 125, 625 are the factors of 5. giving true.
But 175 is also a factor of 5.
System.out.println(2.175f == 2.175);//false

what i am trying to ask you is, is there any way to
identify?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

Omkar onky wrote:what i am trying to ask you is, is there any way to identify?

Yes, but it's tricky, and I'm not sure that that's what you actually need to do.

And BTW, those values that give true are not factors of 5 but fractions involving some power of 2. Integer amounts (within certain limits) will also compare equal.

What is more likely to work is to cast the larger type (the double) to the smaller (float), viz:
System.out.println(0.1f == (float) 0.1);

I suspect strongly that this is not guaranteed to work because of the inexact nature of FP values and vagaries of rounding; although you may have trouble finding values for which it doesn't. For more details, you need to check out the relevant chapter in the JLS.

HIH

Winston
Omkar onky
Greenhorn

Joined: Oct 12, 2012
Posts: 11
According to your knowledge is there any
trick to identify which type of values direct comparison gives true?
I am just eager to know...Mr.Winston.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

Floating-point values in a float or double are stored as binary fractions, in the IEEE-754 single and double precision formats. Note that these formats cannot store any number with arbitrary precision. Only numbers that consist of combinations of (positive or negative) powers of 2 that fit in the range of the formats can be stored exactly.

It's interesting to know the exact details of how floating-point numbers are stored, but the SCJP or OCJP exam does not require you to know this into this level of detail.

There are not going to be questions on the exam that expect you to know that 2.25 == 2.25f but 2.1 != 2.1f.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: float vs double equality