Possible Duplicate:
strange output in comparision of float with float literal
float f = 1.1;
double d = 1.1;
if(f == d) // returns false!
Why is it so?
Possible Duplicate:
strange output in comparision of float with float literal
float f = 1.1;
double d = 1.1;
if(f == d) // returns false!
Why is it so?
The IEEE 754 32-bit
float
can store:1.1000000238...
The IEEE 754 64-bit
double
can store:1.1000000000000000888...
See why they're not "equal"?
In IEEE 754, fractions are stored in powers of 2:
Now we need a way to represent
0.1
. This is (a simplified version of) the 32-bit IEEE 754 representation (float):With 64-bit
double
, it's even more accurate. It doesn't stop at2^(-25)
, it keeps going for about twice as much. (2^(-48) + 2^(-49) + 2^(-51)
, maybe?)Floats and doubles are stored in a binary format that can not represent every number exactly (it's impossible to represent the infinitely many possible different numbers in a finite space).
As a result they do rounding. Float has to round more than double, because it is smaller, so 1.1 rounded to the nearest valid Float is different to 1.1 rounded to the nearest valud Double.
To see what numbers are valid floats and doubles see Floating Point
Try running this code, the results will make the reason obvious.
The output:
Neither
float
nordouble
can represent 1.1 accurately. When you try to do the comparison the float number is implicitly upconverted to a double. The double data type can accurately represent the contents of the float, so the comparison yields false.Generally you shouldn't compare floats to floats, doubles to doubles, or floats to doubles using
==
.The best practice is to subtract them, and check if the absolute value of the difference is less than a small epsilon.
One reason is because floating point numbers are (more or less) binary fractions, and can only approximate many decimal numbers. Many decimal numbers must necessarily be converted to repeating binary "decimals", or irrational numbers. This will introduce a rounding error.
From wikipedia:
In your particular case, a float and double will have different rounding for the irrational/repeating fraction that must be used to represent
1.1
in binary. You will be hard pressed to get them to be "equal" after their corresponding conversions have introduced different levels of rounding error.The code I gave above solves this by simply checking if the values are within a very short delta. Your comparison changes from "are these values equal?" to "are these values within a small margin of error from each other?"
Also, see this question: What is the most effective way for float and double comparison?
There are also a lot of other oddities about floating point numbers that break a simple equality comparison. Check this article for a description of some of them:
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
The important factors under consideration with
float
ordouble
numbers are:Precision & Rounding
Precision:
The precision of a floating point number is how many digits it can represent without losing any information it contains.
Consider the fraction
1/3
. The decimal representation of this number is0.33333333333333…
with 3′s going out to infinity. An infinite length number would require infinite memory to be depicted with exact precision, butfloat
ordouble
data types typically only have4
or8
bytes. Thus Floating point & double numbers can only store a certain number of digits, and the rest are bound to get lost. Thus, there is no definite accurate way of representing float or double numbers with numbers that require more precision than the variables can hold.Rounding:
There is a non-obvious differences between
binary
anddecimal (base 10)
numbers.Consider the fraction
1/10
. Indecimal
, this can be easily represented as0.1
, and0.1
can be thought of as an easily representable number. However, in binary,0.1
is represented by the infinite sequence:0.00011001100110011…
An example:
This output is:
And not
This is because the double had to truncate the approximation due to it’s limited memory, which results in a number that is not exactly
0.1
. Such an scenario is called a Rounding error.Whenever comparing two close float and double numbers such rounding errors kick in and eventually the comparison yields incorrect results and this is the reason you should never compare floating point numbers or double using
==
.The best you can do is to take their difference and check if it is less than an epsilon.