The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Message from discussion Exact integer-valued floats

From:
To:
Cc:
Followup To:
Subject:
 Validation: For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon.

More options Sep 21 2012, 3:59 pm
Newsgroups: comp.lang.python
From: Nobody <nob...@nowhere.com>
Date: Fri, 21 Sep 2012 20:59:16 +0100
Local: Fri, Sep 21 2012 3:59 pm
Subject: Re: Exact integer-valued floats

On Fri, 21 Sep 2012 17:29:13 +0000, Steven D'Aprano 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?

CPython's "float" type uses C's "double". For a system where C's "double"
is IEEE-754 double precision, N=2**53 is the correct answer.

An IEEE-754 double precision value consists of a 53-bit integer whose
first bit is a "1", multiplied or divided by a power of two.

The largest 53-bit integer is 2**53-1. 2**53 can be represented as
2**52 * 2**1. 2**53+1 cannot be represented in this form. 2**53+2 can be
represented as (2**52+1) * 2**1.

For values x where 2**52 <= x < 2**53, the the interval between
representable values (aka Unit in the Last Place or ULP) is 1.0.
For 2**51 <= x < 2**52, the ULP is 0.5.
For 2**53 <= x < 2**54, the ULP is 2.0.
And so on.