# Wrappers and floats comparision

- 1

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

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

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

- 1

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

- 0

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.

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.

- 0

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

- 0

Dennis Deems wrote:Andreas Svenkson wrote:Is it safe to assume that any float and double assigned the same value willneverbe 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

- 0

Andreas Svenkson wrote:Dennis Deems wrote:Andreas Svenkson wrote:Is it safe to assume that any float and double assigned the same value willneverbe 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.

Ranch Hand

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

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

Ranch Hand

- 1

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.

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

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

I agree. Here's the link: http://aspose.com/file-tools |