-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Allow Fraction constructor to accept float and decimal instances directly. #52541
Comments
Here's a patch that allows direct construction of a Fraction instance from a float or Decimal instance, performing an exact conversion in either case. >>> from fractions import Fraction
>>> from decimal import Decimal
>>> Fraction(1.1)
Fraction(2476979795053773, 2251799813685248)
>>> Fraction(Decimal('1.1'))
Fraction(11, 10)
>>> Fraction(Decimal(1.1))
Fraction(2476979795053773, 2251799813685248) |
(The patch is against trunk, btw.) |
The patch looks like what I expected. In the docstring, it would be nice if we kept the line with the spec for decimal strings: [-+]?[0-9]+((/|.)[0-9]+)? |
Unfortunately, that line is wrong (or at least incomplete), since decimals in exponential form are also accepted: >>> Fraction('2.3e4')
Fraction(23000, 1) I could try to reinstate a fixed version. Attaching a second version of the patch: same as the first, except for some doc tweaks. (Rewording, markup fixes.) |
Being wrong is a good reason to eliminate that line from the docstring :-) So, I'm happy with the patch as-is. |
Thanks, Raymond. Committed to trunk in r79629. Will forward port to py3k. |
Merged to py3k in r79670. |
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: