Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is comparing floats inconsistent in Java?

class Test{       public static void main(String[] args){           float f1=3.2f;           float f2=6.5f;            if(f1==3.2){             System.out.println("same");           }else{             System.out.println("different");           }         if(f2==6.5){             System.out.println("same");           }else{               System.out.println("different");           }     }   }   

output:

different same 

Why is the output like that? I expected same as the result in first case.

like image 333
PSR Avatar asked May 18 '13 18:05

PSR


People also ask

Why is it a problem to compare two floating point numbers?

In the case of floating-point numbers, the relational operator (==) does not produce correct output, this is due to the internal precision errors in rounding up floating-point numbers. In the above example, we can see the inaccuracy in comparing two floating-point numbers using “==” operator.

Can you compare floats in Java?

The compare() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call. Parameters: The function accepts two parameters: f1: The first float value to be compared.

Are floating point comparisons reliable?

The floating point comparison is not similar to the integer comparison. To compare two floating point values, we have to consider the precision in to the comparison. For example, if two numbers are 3.1428 and 3.1415, then they are same up to the precision 0.01, but after that, like 0.001 they are not same.

Is float or double more accurate Java?

Though both float and double datatype are used to represent floating-point numbers in Java, a double data type is more precise than float. A double variable can provide precision up to 15 to 16 decimal points as compared to float precision of 6 to 7 decimal digits.


1 Answers

The difference is that 6.5 can be represented exactly in both float and double, whereas 3.2 can't be represented exactly in either type. and the two closest approximations are different.

An equality comparison between float and double first converts the float to a double and then compares the two. So the data loss.


You shouldn't ever compare floats or doubles for equality; because you can't really guarantee that the number you assign to the float or double is exact.

This rounding error is a characteristic feature of floating-point computation.

Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits.

In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.

Check What Every Computer Scientist Should Know About Floating-Point Arithmetic for more!

like image 199
Abimaran Kugathasan Avatar answered Oct 15 '22 00:10

Abimaran Kugathasan