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.

classification
Title: Support .as_integer_ratio() in fractions.Fraction
Type: Stage: patch review
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jdemeyer, mark.dickinson, rhettinger, serhiy.storchaka, stutzbach
Priority: normal Keywords: patch

Created on 2019-08-13 09:41 by jdemeyer, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 15327 open jdemeyer, 2019-08-18 21:08
PR 15328 open jdemeyer, 2019-08-18 21:41
Messages (6)
msg349536 - (view) Author: Jeroen Demeyer (jdemeyer) * (Python triager) Date: 2019-08-13 09:41
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 #37822, but without adding the math.operator() function.
msg349933 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-08-19 05:02
I afraid this can slow down the Fraction constructor.
msg349940 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-08-19 06:20
See issue37884 which uses a C accelerator.
msg349942 - (view) Author: Jeroen Demeyer (jdemeyer) * (Python triager) Date: 2019-08-19 08:36
> See issue37884 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*.
msg349945 - (view) Author: Jeroen Demeyer (jdemeyer) * (Python triager) Date: 2019-08-19 09:08
> I afraid this can slow down the Fraction constructor.

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)'
BEFORE: Mean +- std dev: 826 ns +- 20 ns
AFTER:  Mean +- std dev: 814 ns +- 17 ns

./python -m perf timeit --duplicate 200 -s 'from fractions import Fraction; x = 1' 'Fraction(x, x)'
BEFORE: Mean +- std dev: 1.44 us +- 0.03 us
AFTER:  Mean +- std dev: 1.46 us +- 0.02 us

./python -m perf timeit --duplicate 200 -s 'from fractions import Fraction; x = Fraction(1)' 'Fraction(x)'
BEFORE: Mean +- std dev: 1.64 us +- 0.03 us
AFTER:  Mean +- std dev: 1.30 us +- 0.04 us

./python -m perf timeit --duplicate 200 -s 'from fractions import Fraction; x = Fraction(1)' 'Fraction(x, x)'
BEFORE: Mean +- std dev: 3.03 us +- 0.05 us
AFTER:  Mean +- std dev: 2.34 us +- 0.06 us

./python -m perf timeit --duplicate 200 -s 'from fractions import Fraction; x = 1.0' 'Fraction(x)'
BEFORE: Mean +- std dev: 1.82 us +- 0.02 us
AFTER:  Mean +- std dev: 1.29 us +- 0.04 us
msg409531 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2022-01-02 22:00
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.
History
Date User Action Args
2022-04-11 14:59:19adminsetgithub: 82017
2022-01-02 22:00:12rhettingersetmessages: + msg409531
2019-08-19 09:08:06jdemeyersetmessages: + msg349945
2019-08-19 08:36:44jdemeyersetmessages: + msg349942
2019-08-19 06:20:20serhiy.storchakasetmessages: + msg349940
2019-08-19 05:02:46serhiy.storchakasetmessages: + msg349933
2019-08-18 21:41:06jdemeyersetpull_requests: + pull_request15046
2019-08-18 21:08:46jdemeyersetkeywords: + patch
stage: patch review
pull_requests: + pull_request15045
2019-08-13 09:41:19jdemeyercreate