Skip to content

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

Closed

Description

@mdickinson
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']

    Activity

    1. mdickinson commented on Apr 2, 2010

      @mdickinson
      MemberAuthor

      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)
    2. added
      stdlib
      type-feature
      on Apr 2, 2010
    3. mdickinson commented on Apr 2, 2010

      @mdickinson
      MemberAuthor

      (The patch is against trunk, btw.)

    4. rhettinger commented on Apr 2, 2010

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

    5. mdickinson commented on Apr 2, 2010

      @mdickinson
      MemberAuthor

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

    6. rhettinger commented on Apr 2, 2010

      @rhettinger
      Contributor

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

    7. mdickinson commented on Apr 2, 2010

      @mdickinson
      MemberAuthor

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

    8. mdickinson commented on Apr 3, 2010

      @mdickinson
      MemberAuthor

      Merged to py3k in r79670.

    9. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibtype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions