• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

float vs double equality

 
Greenhorn
Posts: 11
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Omkar chary
Greenhorn
Posts: 11
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 chary
Greenhorn
Posts: 11
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic