Exact integer-valued floats

Ian Kelly ian.g.kelly at gmail.com
Fri Sep 21 14:13:09 EDT 2012


On Fri, Sep 21, 2012 at 11:29 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> The question is, what is the largest integer number N such that every
> whole number between -N and N inclusive can be represented as a float?
>
> If my tests are correct, that value is 9007199254740992.0 = 2**53.
>
> Have I got this right? Is there a way to work out the gap between one
> float and the next?

That looks mathematically correct.  The "gap" between floats is the
equivalent of a difference of 1 bit in the significand.  For a
floating point number represented as (sign * c * 2 ** q), where c is
an integer, the gap between floats is equal to 2 ** q.  There are 53
bits of precision in a double-precision float (technically an implicit
1 followed by 52 bits), so q becomes greater than 0 at 2 ** 53.

Cheers,
Ian



More information about the Python-list mailing list