Author mark.dickinson
Recipients elias, jyasskin, mark.dickinson, rhettinger, serhiy.storchaka
Date 2018-03-15.09:52:57
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1521107578.48.0.467229070634.issue32968@psf.upfronthosting.co.za>
In-reply-to
Content
Yes, that sort of thing is going to happen as soon as floating-point enters the mix. There will be surprises from Fraction % float as well as float % Fraction:

>>> from fractions import Fraction
>>> Fraction(10**23) // 1e22
9.0

And that's again surprising, because 1e22 is exactly equal to 10**22:

>>> 1e22 == 10**22
True

This isn't special to Fractions: the same is true for mixed-type int-float arithmetic:

>>> 10**23 // 1e22
9.0


As you say, this is the result of rounding error.

There's not much we can do about that except for make sure that people are aware that precision can be lost when a Fraction is converted to a float. (One could conceivably outlawed mixed-type Fraction-float operations altogether, but rightly or wrongly that's not the decision that was made when the Fraction type was introduced, and changing that now would amount to gratuitous breakage.)

So yes, modifying both __floordiv__ and __rfloordiv__ together (and similarly for __mod__) is the right thing to do: we definitely don't want Fraction % float and float % Fraction to return different types.

On the testing front, testing the result value of something like 1.0 // Fraction(1, 10) is a little bit dodgy, because the result is only predictable under assumptions that we're using IEEE 754 arithmetic, and that's (at the moment) not an assumption that the Python core makes. I'd suggest using a safer case like 1.0 // Fraction(3, 10).

> I thought about fixing it, to make the behavior more consistent, but I thought that was a bit risky.

I think you have excellent instincts here. :-) But in this case, I do think it's better to be consistent, and to have a easy-to-learn and simple-to-remember rule (mixed-type Fraction-float arithmetic operations convert the Fraction to a float, regardless of the operation or the ordering of the operands). The floating-point surprises are a fact of life anyway, regardless of what Fraction does.
History
Date User Action Args
2018-03-15 09:52:58mark.dickinsonsetrecipients: + mark.dickinson, rhettinger, jyasskin, elias, serhiy.storchaka
2018-03-15 09:52:58mark.dickinsonsetmessageid: <1521107578.48.0.467229070634.issue32968@psf.upfronthosting.co.za>
2018-03-15 09:52:58mark.dickinsonlinkissue32968 messages
2018-03-15 09:52:57mark.dickinsoncreate