This question already has an answer here:
What is the exact range of (contiguous) integers that can be expressed as a double (resp. float?) The reason I ask is because I am curious for questions such as this one when a loss of accuracy will occur.
That is
- What is the least positive integer
m
such thatm+1
cannot be precisely expressed as a double (resp. float)? - What is the greatest negative integer
-n
such that-n-1
cannot be precisely expressed as a double (resp. float)? (May be the same as the above).
This means that every integer between -n
and m
has an exact floating-point representation. I'm basically looking for the range [-n, m]
for both floats and doubles.
Let's limit the scope to the standard IEEE 754 32-bit and 64-bit floating point representations. I know that the float has 24 bits of precision and the double has 53 bits (both with a hidden leading bit), but due to the intricacies of the floating point representation I'm looking for an authoritative answer for this. Please don't wave your hands!
(Ideal answer would prove that all the integers from 0
to m
are expressible, and that m+1
is not.)
Since you're asking about IEEE floating-point types, the language does not matter.
Output:
So the limit for float is 2^24. And the limit for double is 2^53. Negatives are the same since the only difference is the sign bit.