Floating point equality [was Re: What exactly is "exact" (was Clean Singleton Docstrings)]

Chris Kaynor ckaynor at zindagigames.com
Thu Jul 21 20:43:15 EDT 2016


On Thu, Jul 21, 2016 at 4:54 PM, Ben Bacarisse <ben.usenet at bsb.me.uk> wrote:

> Steven D'Aprano <steve at pearwood.info> writes:
> <snip>
> > Or you might be using a language like Javascript, which intentionally has
> > only floats for numbers. That's okay, you can still perform exact integer
> > arithmetic, so long as you stay within the bounds of ±2**16.
>
> Small point: it's 2**52.
>

If you really want to be picky, it is 2**53, inclusive:

>>> 2**53-2.0

9007199254740990.0

>>> 2**53-1.0

9007199254740991.0

>>> 2**53+0.0 # Can no longer store odd numbers, but 2**53 is even so it
can still be stored.

9007199254740992.0

>>> 2**53+1.0

9007199254740992.0

>>> 2**53+2.0
9007199254740994.0

This works as there is an implied one bit in the field for floats, giving
53 bits of storage despite only having 52 bits of storage. As the sign is
stored in a separate bit, the same limit applies to both positive and
negative numbers.



More information about the Python-list mailing list