This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author wolma
Recipients mark.dickinson, paul.moore, wolma
Date 2015-04-27.13:08:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1430140102.66.0.828119944569.issue23975@psf.upfronthosting.co.za>
In-reply-to
Content
After considering this for a while, I think:

return float(self.numerator / self.denominator)

is the best solution:

* it is simple and works reasonably well as a default

* it fixes Rational.__float__ for cases, in which numerator / denominator returns a custom Real instance

* in the problematic scenario brought up by Mark, in which truediv of the numerator and denominator returns another Rational creating a potentially infinite recursion, a RuntimeError will be raised when the maximum recursion limit is reached. This is not an unheard of error to run into while trying to implement a custom numeric type and will provide reasonable (though not ideal) information about the problem.

* the workaround for the above is obvious: it is ok for self.numerator / self.denominator to return a Rational instance, but its type should overwrite Rational.__float__ to break the recursion. This could get documented in the docstring of Rational.__float__.

I've attached a tentative patch.
History
Date User Action Args
2015-04-27 13:08:22wolmasetrecipients: + wolma, paul.moore, mark.dickinson
2015-04-27 13:08:22wolmasetmessageid: <1430140102.66.0.828119944569.issue23975@psf.upfronthosting.co.za>
2015-04-27 13:08:22wolmalinkissue23975 messages
2015-04-27 13:08:22wolmacreate