Generating equally-spaced floats with least rounding error

Chris Angelico rosuav at gmail.com
Sun Sep 25 02:09:01 EDT 2011


On Sun, Sep 25, 2011 at 3:31 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> 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))
True

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

>>> F(2.1)==F(21,10)
False
>>> 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.

ChrisA



More information about the Python-list mailing list