File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# Wrappers and floats comparision

Greenhorn

Joined: May 02, 2011
Posts: 12

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...

OCPJP
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4594

27

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.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Greenhorn

Joined: May 02, 2011
Posts: 12

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

Joined: Jan 21, 2011
Posts: 637

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.

SCJP 6. Learning more now.
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
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

Joined: Mar 28, 2011
Posts: 89
Hello,

Very good question.

Arhaan
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Jan 17, 2011
Posts: 179
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

Joined: Mar 12, 2011
Posts: 808
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

Joined: Sep 27, 2002
Posts: 280

9
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

Joined: Jan 17, 2011
Posts: 179
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

Joined: Sep 20, 2010
Posts: 4594

27

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

Joined: Sep 27, 2002
Posts: 280

9
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

Joined: Sep 20, 2010
Posts: 4594

27

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

Joined: Mar 11, 2011
Posts: 103

Hey all!!!

thanks a lot for this discussion!!!

If debugging is the process of removing bugs, then programming must be the process of putting them in. -- Edsger Dijkstra

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4594

27

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

Joined: Sep 27, 2002
Posts: 280

9
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

subject: Wrappers and floats comparision