Unexpected results comparing float to Fraction

Ian Kelly ian.g.kelly at gmail.com
Mon Jul 29 21:33:31 CEST 2013


On Mon, Jul 29, 2013 at 12:16 PM, Rotwang <sg552 at hotmail.co.uk> wrote:
> On 29/07/2013 17:40, Ian Kelly wrote:
>> At the point where the float is exactly equal to the value you get
>> from the floating-point division 1/3.
>
>
> But the interpreter has no way of knowing that the value 1/3 that's been
> passed to the Fraction constructor was obtained from the division 1/3,
> rather than, say, 100000000000000001/300000000000000000 or
> 6004799503160661/18014398509481984. How do you propose the constructor
> should decide between the many possible fractions that round to the same
> float, if not by choosing the one that evaluates to it exactly?

It should choose the fraction with the least terms that rounds to the
float.  Whether "least" here means least numerator, least denominator,
least sum of the two, or whatever is not terribly important.



More information about the Python-list mailing list