Generating equally-spaced floats with least rounding error

Chris Angelico rosuav at
Sun Sep 25 08:09:01 CEST 2011

On Sun, Sep 25, 2011 at 3:31 PM, Steven D'Aprano
<steve+comp.lang.python at> wrote:
>>>> 2.1 == F(21, 10)
> False

Ahh, but that may just mean that Fraction doesn't implement an == that
compares with floats.

>>> 2.1==float(F(21,10))

This may be because one oughtn't to use == with floats anyway.

>>> F(2.1)==F(21,10)
>>> F(2.1)
Fraction(4728779608739021, 2251799813685248)

That denominator is a power of 2 (2**51 as it happens), which is
unsurprising for something created from an IEEE floating point value.
Rounding F(21,10) off to fit into float produces this same value.


More information about the Python-list mailing list