Mark Dickinson <dicki...@gmail.com> added the comment:

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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue32968>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to