Author serhiy.storchaka
Recipients elias, mark.dickinson, rhettinger, serhiy.storchaka
Date 2018-03-01.11:03:33
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1519902213.28.0.467229070634.issue32968@psf.upfronthosting.co.za>
In-reply-to
Content
Not all Fractions can be converted to float.

>>> Fraction(2**2000, 3) // 1.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython/Lib/fractions.py", line 432, in __floordiv__
    return math.floor(a / b)
  File "/home/serhiy/py/cpython/Lib/fractions.py", line 378, in forward
    return fallback_operator(float(a), b)
  File "/home/serhiy/py/cpython/Lib/numbers.py", line 291, in __float__
    return self.numerator / self.denominator
OverflowError: integer division result too large for a float

What is surprising that the modulo operation can fail even if the end result could be converted to float.

>>> Fraction(2**2000, 3) % 1
Fraction(1, 3)
>>> Fraction(2**2000, 3) % 1.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython/Lib/fractions.py", line 440, in __mod__
    div = a // b
  File "/home/serhiy/py/cpython/Lib/fractions.py", line 432, in __floordiv__
    return math.floor(a / b)
  File "/home/serhiy/py/cpython/Lib/fractions.py", line 378, in forward
    return fallback_operator(float(a), b)
  File "/home/serhiy/py/cpython/Lib/numbers.py", line 291, in __float__
    return self.numerator / self.denominator
OverflowError: integer division result too large for a float
History
Date User Action Args
2018-03-01 11:03:33serhiy.storchakasetrecipients: + serhiy.storchaka, rhettinger, mark.dickinson, elias
2018-03-01 11:03:33serhiy.storchakasetmessageid: <1519902213.28.0.467229070634.issue32968@psf.upfronthosting.co.za>
2018-03-01 11:03:33serhiy.storchakalinkissue32968 messages
2018-03-01 11:03:33serhiy.storchakacreate