[New-bugs-announce] [issue23975] numbers.Rational implements __float__ incorrectly

Wolfgang Maier report at bugs.python.org
Thu Apr 16 22:06:57 CEST 2015

New submission from Wolfgang Maier:

numbers.Rational defines __float__ like this:

def __float__(self):
    """float(self) = self.numerator / self.denominator

    It's important that this conversion use the integer's "true"
    division rather than casting one side to float before dividing
    so that ratios of huge integers convert without overflowing.

    return self.numerator / self.denominator

This assumes that division of two Integral numbers returns a float, which the numbers ABC does not enforce in any way.
IMO, the only logical assumption is that division of any two Integral or Rational numbers gives a Real, for which the ABC guarantees a __float__ method in turn.
So I think Rational.__float__ should

return float(self.numerator / self.denominator)

components: Library (Lib)
messages: 241270
nosy: wolma
priority: normal
severity: normal
status: open
title: numbers.Rational implements __float__ incorrectly
type: behavior
versions: Python 3.5

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list