Unexpected results comparing float to Fraction

Chris Angelico rosuav at gmail.com
Thu Aug 1 02:32:56 EDT 2013


On Thu, Aug 1, 2013 at 7:20 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> I know this, and that's not what surprised me. What surprised me was that
> Fraction converts the float to a fraction, then compares. It surprises me
> because in other operations, Fractions down-cast to float.
>
> Adding a float to a Fraction converts the Fraction to the nearest float,
> then adds:
>
> py> 1/3 + Fraction(1, 3)
> 0.6666666666666666

Hmm. This is the one that surprises me. That would be like the
addition of a float and an int resulting in an int (at least in C; in
Python, where floats have limited range and ints have arbitrary
precision, the matter's not quite so clear-cut). Perhaps this needs to
be changed?

ChrisA



More information about the Python-list mailing list