This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Allow Fraction constructor to accept float and decimal instances directly.
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.2
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: mark.dickinson Nosy List: mark.dickinson, rhettinger
Priority: normal Keywords: patch

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

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.
Date User Action Args
2022-04-11 14:56:59adminsetgithub: 52541
2010-04-03 11:40:56mark.dickinsonsetstatus: open -> closed

messages: + msg102238
stage: resolved
2010-04-02 22:28:10mark.dickinsonsetmessages: + msg102210
versions: - Python 2.7
2010-04-02 21:13:06rhettingersetmessages: + msg102205
2010-04-02 21:07:07mark.dickinsonsetfiles: + fraction_from_float2.patch

messages: + msg102204
2010-04-02 21:04:17rhettingersetassignee: rhettinger -> mark.dickinson
resolution: accepted
messages: + msg102201
2010-04-02 20:43:32mark.dickinsonsetmessages: + msg102199
2010-04-02 20:42:41mark.dickinsoncreate