Message242113
| 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:22 | wolma | set | recipients:
+ wolma, paul.moore, mark.dickinson |
| 2015年04月27日 13:08:22 | wolma | set | messageid: <1430140102.66.0.828119944569.issue23975@psf.upfronthosting.co.za> |
| 2015年04月27日 13:08:22 | wolma | link | issue23975 messages |
| 2015年04月27日 13:08:22 | wolma | create |
|