Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add math.as_integer_ratio() #82003

Closed
serhiy-storchaka opened this issue Aug 11, 2019 · 4 comments
Closed

Add math.as_integer_ratio() #82003

serhiy-storchaka opened this issue Aug 11, 2019 · 4 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

BPO 37822
Nosy @rhettinger, @mdickinson, @serhiy-storchaka, @jdemeyer
PRs
  • bpo-37822: Add math.as_integer_ratio(). #15210
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-08-11.21:11:13.151>
    created_at = <Date 2019-08-11.13:55:05.354>
    labels = ['type-feature', 'library', '3.9']
    title = 'Add math.as_integer_ratio()'
    updated_at = <Date 2019-08-12.08:33:31.382>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2019-08-12.08:33:31.382>
    actor = 'jdemeyer'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-08-11.21:11:13.151>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2019-08-11.13:55:05.354>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37822
    keywords = ['patch']
    message_count = 4.0
    messages = ['349390', '349400', '349410', '349447']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'mark.dickinson', 'stutzbach', 'serhiy.storchaka', 'jdemeyer']
    pr_nums = ['15210']
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue37822'
    versions = ['Python 3.9']

    @serhiy-storchaka
    Copy link
    Member Author

    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).

    @serhiy-storchaka serhiy-storchaka added 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Aug 11, 2019
    @rhettinger
    Copy link
    Contributor

    -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.

    @rhettinger
    Copy link
    Contributor

    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

    @jdemeyer
    Copy link
    Contributor

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants