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
.
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 .
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.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With