# float vs double equality

posted 3 years ago

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?

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?

posted 3 years ago

I think you might want to double-check what she

I suspect that if you try:

and

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

posted 3 years ago

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?

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?

posted 3 years ago

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

What is more

I suspect strongly that this is not

HIH

Winston

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

Articles by Winston can be found here

posted 3 years ago

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.

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.