classification
Title: Add math.as_integer_ratio()
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: jdemeyer, mark.dickinson, rhettinger, serhiy.storchaka, stutzbach
Priority: normal Keywords: patch

Created on 2019-08-11 13:55 by serhiy.storchaka, last changed 2019-08-12 08:33 by jdemeyer. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 15210 open serhiy.storchaka, 2019-08-11 13:57
Messages (4)
msg349390 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-08-11 13:55
There are two interfaces to represent a number as a ratio. The numbers.Rational interface has two properties: numerator and denominator. float and Decimal do not support this interface, but they provide method as_integer_ratio() which return a 2-tuple (numerator, denominator).

I propose to add math.as_integer_ratio() which unites both interfaces: uses the as_integer_ratio() method if it is defined, and uses the numerator and denominator properties otherwise. It will help in applications that work with exact numbers (e.g. modules fractions and statistics).
msg349400 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-08-11 17:03
-1 for having this spill into the math module.   We're already started down a different path; otherwise, there would have been no need to add the as_integer_ratio method to int/bool.  That was done so that x.as_integer_ratio() would work for all the concrete classes where it made sense.  Right now, the only concrete class that is missing the method is Fractions.  Adding it there is trivial and it doesn't garbage-up the math module with a pass-through function.
msg349410 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-08-11 21:11
Guido decided to stick with the current approach of adding as_integer_ratio() as needed to concrete classes.  It is an optional API for user classes that register as numbers.Rational().  

See https://bugs.python.org/msg349404
msg349447 - (view) Author: Jeroen Demeyer (jdemeyer) * Date: 2019-08-12 08:33
Aren't you worried about using the non-special non-reserved attributes like "as_integer_ratio"? That's the reason why I proposed a dunder name "__ratio__" instead of "as_integer_ratio".

In my opinion, it was a mistake in PEP 3141 to use non-reserved names like "numerator" and "denominator" because they might already be used by existing classes with a different meaning (this is precisely what happened with SageMath).

It's an honest question. If you say, "we thought about it and think it's just fine to use as_integer_ratio()", just say so and that's fine for me.
History
Date User Action Args
2019-08-12 08:33:31jdemeyersetnosy: + jdemeyer
messages: + msg349447
2019-08-11 21:11:13rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg349410

stage: patch review -> resolved
2019-08-11 17:03:28rhettingersetmessages: + msg349400
2019-08-11 13:57:28serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request14938
2019-08-11 13:55:05serhiy.storchakacreate