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

Allow Fraction constructor to accept float and decimal instances directly. #52541

Closed
mdickinson opened this issue Apr 2, 2010 · 7 comments
Closed
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@mdickinson
Copy link
Member

BPO 8294
Nosy @rhettinger, @mdickinson
Files
  • fraction_from_float.patch
  • fraction_from_float2.patch
  • 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 = 'https://github.com/mdickinson'
    closed_at = <Date 2010-04-03.11:40:56.050>
    created_at = <Date 2010-04-02.20:42:41.715>
    labels = ['type-feature', 'library']
    title = 'Allow Fraction constructor to accept float and decimal instances directly.'
    updated_at = <Date 2010-04-03.11:40:56.049>
    user = 'https://github.com/mdickinson'

    bugs.python.org fields:

    activity = <Date 2010-04-03.11:40:56.049>
    actor = 'mark.dickinson'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2010-04-03.11:40:56.050>
    closer = 'mark.dickinson'
    components = ['Library (Lib)']
    creation = <Date 2010-04-02.20:42:41.715>
    creator = 'mark.dickinson'
    dependencies = []
    files = ['16738', '16740']
    hgrepos = []
    issue_num = 8294
    keywords = ['patch']
    message_count = 7.0
    messages = ['102198', '102199', '102201', '102204', '102205', '102210', '102238']
    nosy_count = 2.0
    nosy_names = ['rhettinger', 'mark.dickinson']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue8294'
    versions = ['Python 3.2']

    @mdickinson
    Copy link
    Member Author

    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)

    @mdickinson mdickinson added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Apr 2, 2010
    @mdickinson
    Copy link
    Member Author

    (The patch is against trunk, btw.)

    @rhettinger
    Copy link
    Contributor

    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]+)?

    @rhettinger rhettinger assigned mdickinson and unassigned rhettinger Apr 2, 2010
    @mdickinson
    Copy link
    Member Author

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

    @rhettinger
    Copy link
    Contributor

    Being wrong is a good reason to eliminate that line from the docstring :-) So, I'm happy with the patch as-is.

    @mdickinson
    Copy link
    Member Author

    Thanks, Raymond. Committed to trunk in r79629. Will forward port to py3k.

    @mdickinson
    Copy link
    Member Author

    Merged to py3k in r79670.

    @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
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants