Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

0.1 float is greater than 0.1 double. I expected it to be false [duplicate]

Let:

double d = 0.1; float f = 0.1; 

should the expression

(f > d) 

return true or false?

Empirically, the answer is true. However, I expected it to be false.

As 0.1 cannot be perfectly represented in binary, while double has 15 to 16 decimal digits of precision, and float has only 7. So, they both are less than 0.1, while the double is more close to 0.1.

I need an exact explanation for the true.

like image 212
Hesham Eraqi Avatar asked Oct 10 '13 09:10

Hesham Eraqi


People also ask

Is 0.1 a double or float?

As 0.1 cannot be perfectly represented in binary, while double has 15 to 16 decimal digits of precision, and float has only 7 . So, they both are less than 0.1 , while the double is more close to 0.1 .

Why is the number 0.1 Not a good number to use in simulations?

0.1 cannot be represented as accurately in base-2 as in base-10 due to the missing prime factor of 5. Just as 1/3 takes an infinite number of digits to represent in decimal, but is "0.1" in base-3, 0.1 takes an infinite number of digits in base-2 where it does not in base-10.

Is 0.1 A float value?

The number 0.1 in floating-point The finite representation of 1/10 is 0.0 0011 ‾ 0.0\overline{0011} 0.00011, but it can't be represented in floating-point because we can't deal with bars in floating-point. We can represent it only in fixed digits/bits using any data type.

Is 1.2 float or double?

1.2 is a double (8 bytes). 1.2f is a float (4 bytes). Show activity on this post. Any literal number in your code which includes a decimal point is interpreted as a double , not a float , unless you mark it as a float by appending f .


1 Answers

I'd say the answer depends on the rounding mode when converting the double to float. float has 24 binary bits of precision, and double has 53. In binary, 0.1 is:

0.1₁₀ = 0.0001100110011001100110011001100110011001100110011…₂              ^        ^         ^   ^               1       10        20  24 

So if we round up at the 24th digit, we'll get

0.1₁₀ ~ 0.000110011001100110011001101 

which is greater than the exact value and the more precise approximation at 53 digits.

like image 71
kennytm Avatar answered Oct 21 '22 04:10

kennytm