Issue 8294: Allow Fraction constructor to accept float and decimal instances directly. (original) (raw)

Created on 2010-04-02 20:42 by mark.dickinson, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
fraction_from_float.patch mark.dickinson,2010-04-02 20:42
fraction_from_float2.patch mark.dickinson,2010-04-02 21:07
Messages (7)
msg102198 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-04-02 20:42
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)
msg102199 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-04-02 20:43
(The patch is against trunk, btw.)
msg102201 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-04-02 21:04
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]+)?
msg102204 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-04-02 21:07
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.)
msg102205 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-04-02 21:13
Being wrong is a good reason to eliminate that line from the docstring :-) So, I'm happy with the patch as-is.
msg102210 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-04-02 22:28
Thanks, Raymond. Committed to trunk in r79629. Will forward port to py3k.
msg102238 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-04-03 11:40
Merged to py3k in r79670.
History
Date User Action Args
2022-04-11 14:56:59 admin set github: 52541
2010-04-03 11:40:56 mark.dickinson set status: open -> closedmessages: + stage: resolved
2010-04-02 22:28:10 mark.dickinson set messages: + versions: - Python 2.7
2010-04-02 21:13:06 rhettinger set messages: +
2010-04-02 21:07:07 mark.dickinson set files: + fraction_from_float2.patchmessages: +
2010-04-02 21:04:17 rhettinger set assignee: rhettinger -> mark.dickinsonresolution: acceptedmessages: +
2010-04-02 20:43:32 mark.dickinson set messages: +
2010-04-02 20:42:41 mark.dickinson create