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 2019-08-19 09:08 by jdemeyer.

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 (5)
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
History
Date User Action Args
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