Unexpected results comparing float to Fraction

Grant Edwards invalid at invalid.invalid
Mon Jul 29 14:46:33 EDT 2013


On 2013-07-29, MRAB <python at mrabarnett.plus.com> wrote:
> On 29/07/2013 17:40, Ian Kelly wrote:
>> On Mon, Jul 29, 2013 at 10:20 AM, Chris Angelico <rosuav at gmail.com> wrote:
>>> On Mon, Jul 29, 2013 at 5:09 PM, MRAB <python at mrabarnett.plus.com> wrote:
>>>> I'm surprised that Fraction(1/3) != Fraction(1, 3); after all, floats
>>>> are approximate anyway, and the float value 1/3 is more likely to be
>>>> Fraction(1, 3) than Fraction(6004799503160661, 18014398509481984).
>>>
>>> At what point should it become Fraction(1, 3)?
>>
>> At the point where the float is exactly equal to the value you get
>> from the floating-point division 1/3.  If it's some other float then
>> the user didn't get there by entering 1/3, so it's not worth trying to
>> pretend that they did.
>
> I thought that you're not meant to check for equality when using floats.

You check for equality if equality is what you want to check.  However
much of the time when people _think_ they want to check for FP
equality, they're wrong.

You'll have to consult with a spiritual advisor to determin what you
are "meant" to do...

-- 
Grant Edwards               grant.b.edwards        Yow! Awright, which one of
                                  at               you hid my PENIS ENVY?
                              gmail.com            



More information about the Python-list mailing list