8000 bpo-32968: Make modulo and floor division involving Fraction and floa… · python/cpython@393f1ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 393f1ff

Browse files
elias6mdickinson
authored andcommitted
bpo-32968: Make modulo and floor division involving Fraction and float consistent with other operations (#5956)
Make mixed-type `%` and `//` operations involving `Fraction` and `float` objects behave like all other mixed-type arithmetic operations: first the `Fraction` object is converted to a `float`, then the `float` operation is performed as normal. This fixes some surprising corner cases, like `Fraction('1/3') % inf` giving a NaN. Thanks Elias Zamaria for the patch.
1 parent 74734f7 commit 393f1ff

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

Lib/fractions.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -427,23 +427,18 @@ def _div(a, b):
427427

428428
__truediv__, __rtruediv__ = _operator_fallbacks(_div, operator.truediv)
429429

430-
def __floordiv__(a, b):
430+
def _floordiv(a, b):
431431
"""a // b"""
432432
return math.floor(a / b)
433433

434-
def __rfloordiv__(b, a):
435-
"""a // b"""
436-
return math.floor(a / b)
434+
__floordiv__, __rfloordiv__ = _operator_fallbacks(_floordiv, operator.floordiv)
437435

438-
def __mod__(a, b):
436+
def _mod(a, b):
439437
"""a % b"""
440438
div = a // b
441439
return a - b * div
442440

443-
def __rmod__(b, a):
444-
"""a % b"""
445-
div = a // b
446-
return a - b * div
441+
__mod__, __rmod__ = _operator_fallbacks(_mod, operator.mod)
447442

448443
def __pow__(a, b):
449444
"""a ** b

Lib/test/test_fractions.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,19 @@ def testMixedArithmetic(self):
401401
self.assertTypedEquals(10.0 + 0j, (1.0 + 0j) / F(1, 10))
402402

403403
self.assertTypedEquals(0, F(1, 10) // 1)
404-
self.assertTypedEquals(0, F(1, 10) // 1.0)
404+
self.assertTypedEquals(0.0, F(1, 10) // 1.0)
405405
self.assertTypedEquals(10, 1 // F(1, 10))
406406
self.assertTypedEquals(10**23, 10**22 // F(1, 10))
407-
self.assertTypedEquals(10, 1.0 // F(1, 10))
407+
self.assertTypedEquals(1.0 // 0.1, 1.0 // F(1, 10))
408408

409409
self.assertTypedEquals(F(1, 10), F(1, 10) % 1)
410410
self.assertTypedEquals(0.1, F(1, 10) % 1.0)
411411
self.assertTypedEquals(F(0, 1), 1 % F(1, 10))
412-
self.assertTypedEquals(0.0, 1.0 % F(1, 10))
412+
self.assertTypedEquals(1.0 % 0.1, 1.0 % F(1, 10))
413+
self.assertTypedEquals(0.1, F(1, 10) % float('inf'))
414+
self.assertTypedEquals(float('-inf'), F(1, 10) % float('-inf'))
415+
self.assertTypedEquals(float('inf'), F(-1, 10) % float('inf'))
416+
self.assertTypedEquals(-0.1, F(-1, 10) % float('-inf'))
413417

414418
# No need for divmod since we don't override it.
415419

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,7 @@ Masazumi Yoshikawa
18141814
Arnaud Ysmal
18151815
Bernard Yue
18161816
Moshe Zadka
1817+
Elias Zamaria
18171818
Milan Zamazal
18181819
Artur Zaprzala
18191820
Mike Zarnstorff
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Modulo and floor division involving Fraction and float should return float.

0 commit comments

Comments
 (0)
0