New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add math.as_integer_ratio() #82003
Comments
There are two interfaces to represent a number as a ratio. The numbers.Rational interface has two properties: numerator and denominator. float and Decimal do not support this interface, but they provide method as_integer_ratio() which return a 2-tuple (numerator, denominator). I propose to add math.as_integer_ratio() which unites both interfaces: uses the as_integer_ratio() method if it is defined, and uses the numerator and denominator properties otherwise. It will help in applications that work with exact numbers (e.g. modules fractions and statistics). |
-1 for having this spill into the math module. We're already started down a different path; otherwise, there would have been no need to add the as_integer_ratio method to int/bool. That was done so that x.as_integer_ratio() would work for all the concrete classes where it made sense. Right now, the only concrete class that is missing the method is Fractions. Adding it there is trivial and it doesn't garbage-up the math module with a pass-through function. |
Guido decided to stick with the current approach of adding as_integer_ratio() as needed to concrete classes. It is an optional API for user classes that register as numbers.Rational(). |
Aren't you worried about using the non-special non-reserved attributes like "as_integer_ratio"? That's the reason why I proposed a dunder name "__ratio__" instead of "as_integer_ratio". In my opinion, it was a mistake in PEP-3141 to use non-reserved names like "numerator" and "denominator" because they might already be used by existing classes with a different meaning (this is precisely what happened with SageMath). It's an honest question. If you say, "we thought about it and think it's just fine to use as_integer_ratio()", just say so and that's fine for me. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: