This week's book giveaway is in the Java 8 forum. We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line! See this thread for details.

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.

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.

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

posted

0

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

posted

0

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 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: 3393

9

posted

0

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: 245

9

posted

0

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: 3393

9

posted

1

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.

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: 3393

9

posted

1

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.