File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrappers and floats comparision

 
Peter Smorada
Greenhorn
Posts: 12
Java Netbeans IDE Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am taking the exam this Wednesday and i am playing a bit with code.

I came accros this problem
sample of code:
Float f = 124.4f;
Double d = 124.4;

System.out.println("float == double: " + (124.4F == 124.4));
System.out.println("float > double: " + (124.4F > 124.4));
System.out.println("float < double: " + (124.4F < 124.4));
System.out.println("Float > Double: " + (f > d));
System.out.println("Float < Double: " + (f < d));


The result is quite supprising for me:
float == double: false
float > double: true
float < double: false
Float > Double: true
Float < Double: false


I quite don't get this results. Does anyone know why is it so? I would expect all of them to be false except for the first one...
 
Stephan van Hulst
Bartender
Pie
Posts: 4838
34
Chrome Netbeans IDE Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter, welcome to CodeRanch.

When you assign a floating point literal to a float or a double, it will store the value closest to that literal that is representable with 32 or 64 bits respectively. Since doubles have a higher precision than floats, the value they store will be closer to the floating point literal than the value stored by the float.

Let's use 124.4 as an example. The values I use here are made up, and are not correct:

If you assign 124.4f to f, it might actually store the value 124.400012. Since doubles have higher precision, the value you store to d might actually be 124.4000000005.

As you can see, these values are not the same, so == comparison will return false.
The float value is larger than the double value, so this explains the rest of the comparisons as well.

Doubles and floats both store the same 'range' of numbers. Doubles are simply more precise than floats.
 
Peter Smorada
Greenhorn
Posts: 12
Java Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick answer. It's explains everything.

Greeting from Slovakia (country where there is this year's ice hockey world championship)

Peter
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
Let's use 124.4 as an example. The values I use here are made up, and are not correct:

If you assign 124.4f to f, it might actually store the value 124.400012. Since doubles have higher precision, the value you store to d might actually be 124.4000000005.
As you can see, these values are not the same, so == comparison will return false.
The float value is larger than the double value, so this explains the rest of the comparisons as well.


How do i know the exact value stored in a float, Float etc ? Luckily it occurred to me that we can use the NumberFormat class to our advantage. Perhaps we can
use this problem as an example for the need of this class.

Here is the code to see the values inside each variable :




OUTPUT :



PS : Special thanks to Peter Smorada for posting this question and to stephen for explaining.
Good luck to you for the exams.

 
Andreas Svenkson
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A followup question then, Stephen.

Is it safe to assume that any float and double assigned the same value will never be the same? Theoretically there should be a chance that it can sometimes be the same, but obviously we can't ever know when that is the case and most of the time I would guess it isn't?

// Andreas
 
Arhaan Singhania
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Very good question.

Arhaan
 
dennis deems
Ranch Hand
Posts: 808
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andreas Svenkson wrote:Is it safe to assume that any float and double assigned the same value will never be the same?

Try Rahul's demo with the value 124.5
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:
Andreas Svenkson wrote:Is it safe to assume that any float and double assigned the same value will never be the same?

Try Rahul's demo with the value 124.5


Indeed.... man I hope there won't be any questions of this type on the test, it's about as close to impossible to answer as it can get.

// Andreas
 
dennis deems
Ranch Hand
Posts: 808
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andreas Svenkson wrote:
Dennis Deems wrote:
Andreas Svenkson wrote:Is it safe to assume that any float and double assigned the same value will never be the same?

Try Rahul's demo with the value 124.5


Indeed.... man I hope there won't be any questions of this type on the test, it's about as close to impossible to answer as it can get.

// Andreas


Well it may be specific to the JVM, I don't know. But when I run, it shows that float 124.5 == double 124.5
So at least we see that it is not safe to assume that they will never be the same.
 
Mala Gupta
Author
Ranch Hand
Posts: 281
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the decimal part of float/ double is 0, then the literal values are equal. Examine the following code:


cheers
Mala
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mala Gupta wrote:If the decimal part of float/ double is 0, then the literal values are equal. Examine the following code:


cheers
Mala


Nice pointer, thanks.

// Andreas
 
Stephan van Hulst
Bartender
Pie
Posts: 4838
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:How do i know the exact value stored in a float, Float etc?

You don't, unless you take the bits used to represent the float, and calculate the value yourself. A description is in Float.floatToIntBits(float) and Double.doubleToLongBits(double).
Note that most of the time, you aren't interested in the exact value stored. Floats and doubles are approximations of real numbers. If you are interested in exact values, you should *never* use floats or doubles. Use BigDecimal instead.

Andreas Svenkson wrote:Is it safe to assume that any float and double assigned the same value will never be the same? Theoretically there should be a chance that it can sometimes be the same, but obviously we can't ever know when that is the case and most of the time I would guess it isn't?

Exactly, but it's not safe to assume they will never be the same, as has been demonstrated. When you compare different floating point values to each other, you should always use the >, <, >=, <= operators. Never use the == operator. It will almost always fail.

Mala Gupta wrote:If the decimal part of float/ double is 0, then the literal values are equal.

No. You may not assume this. Example:
Both literals imply the integer value 10^20, but the comparison evaluates to false.
 
Mala Gupta
Author
Ranch Hand
Posts: 281
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
Both literals imply the integer value 10^20, but the comparison evaluates to false.


There should be more to it. In the above example, values up to E10 return true, but greater than that returns false! Examine the following code:


cheers
Mala
 
Stephan van Hulst
Bartender
Pie
Posts: 4838
34
Chrome Netbeans IDE Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there's more to it. The interval between values that a floating point can represent become larger for more extreme values. Here is a very simplistic view:

Let's say that our floating point data type had 3 digits of precision. Less extreme have a smaller gap between them: If we have the value 0.000343, the next value we can represent using three digits of precision would be 0.000344, so the gap between them is 0.000001. The inverse is true for extreme values: The next value we can represent after 50200000 is 50300000. There is a gap of 100000!

This means that if we assign the literal 50285364 to our data type that has 3 digits of precision, it would automatically get rounded up to 50300000.

The exact same is also true for float and double, except instead of 3 digits of precision, float has 32 bits of precision, and double has 64 bits of precision.
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all!!!


thanks a lot for this discussion!!!
 
Stephan van Hulst
Bartender
Pie
Posts: 4838
34
Chrome Netbeans IDE Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:[E]xcept instead of 3 digits of precision, float has 32 bits of precision, and double has 64 bits of precision.

For sake of completeness, this actually isn't true. Float uses a mantissa (significance) of 23 bits, a double uses a mantissa of 52 bits. The rest of the bits are used to represent the sign and the scale of the numbers.

But you don't need to know all of this for the SCJP
The most important part is that you know that float and double represent approximate, and not exact values.
 
Mala Gupta
Author
Ranch Hand
Posts: 281
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, indeed. There is quite a lot to add to it.

Thanks, Stephan.

cheers
Mala
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic