-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Support .as_integer_ratio() in fractions.Fraction #82017
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
Comments
Currently, the fractions.Fraction constructor accepts an .as_integer_ratio() method only for the specific types "float" and "Decimal". It would be good to support this for arbitrary classes. This is part of what was proposed in bpo-37822, 8000 but without adding the math.operator() function. |
I afraid this can slow down the Fraction constructor. |
See bpo-37884 which uses a C accelerator. |
Note that that doesn't replace this issue, because I need to support as_integer_ratio both in the *numerator* and *denominator*. |
No, it doesn't! It even speeds up the constructor in some cases: ./python -m perf timeit --duplicate 200 -s 'from fractions import Fraction; x = 1' 'Fraction(x)' ./python -m perf timeit --duplicate 200 -s 'from fractions import Fraction; x = 1' 'Fraction(x, x)' ./python -m perf timeit --duplicate 200 -s 'from fractions import Fraction; x = Fraction(1)' 'Fraction(x)' ./python -m perf timeit --duplicate 200 -s 'from fractions import Fraction; x = Fraction(1)' 'Fraction(x, x)' ./python -m perf timeit --duplicate 200 -s 'from fractions import Fraction; x = 1.0' 'Fraction(x)' |
I don't think this is a door we should open: >>> Fraction(3.5, 2.5)
Fraction(7, 5) This currently raises a useful exception: TypeError: both arguments should be Rational instances That is especially helpful in avoiding cases like this: >>> Fraction(1.1, 3.3)
Fraction(2476979795053773, 7430939385161318) If that output is desired, the two conversions should be explicit. >>> Fraction(1.1) / Fraction(3.3)
Fraction(2476979795053773, 7430939385161318 I recommend rejecting this feature request as being more likely to be hazardous than helpful. |
Concur with @rhettinger. |
This proposition was rejected because it used So I reopen this issue with more limited goal -- supporting |
Any numbers that have the as_integer_ratio() method (e.g. numpy.float128) can now be converted to a fraction.
Agreed this would be a nice generalisation for the case of converting a user-defined type to a fraction (the user defined type just has to define |
…120271) Any objects that have the as_integer_ratio() method (e.g. numpy.float128) can now be converted to a fraction.
Uh oh!
There was an error while loading. Please reload this page.
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:
Linked PRs
The text was updated successfully, but these errors were encountered: