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

Fraction('1e6') should be valid. #50062

Closed
mdickinson opened this issue Apr 22, 2009 · 8 comments
Closed

Fraction('1e6') should be valid. #50062

mdickinson opened this issue Apr 22, 2009 · 8 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@mdickinson
Copy link
Member

BPO 5812
Nosy @rhettinger, @mdickinson
Files
  • fraction_from_exp.patch
  • fraction_of_fractions.patch
  • fraction_of_fractions2.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 2009-04-24.14:09:18.762>
    created_at = <Date 2009-04-22.10:04:24.993>
    labels = ['type-feature', 'library']
    title = "Fraction('1e6') should be valid."
    updated_at = <Date 2009-04-24.14:09:18.760>
    user = 'https://github.com/mdickinson'

    bugs.python.org fields:

    activity = <Date 2009-04-24.14:09:18.760>
    actor = 'mark.dickinson'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2009-04-24.14:09:18.762>
    closer = 'mark.dickinson'
    components = ['Library (Lib)']
    creation = <Date 2009-04-22.10:04:24.993>
    creator = 'mark.dickinson'
    dependencies = []
    files = ['13735', '13739', '13742']
    hgrepos = []
    issue_num = 5812
    keywords = ['patch']
    message_count = 8.0
    messages = ['86283', '86286', '86288', '86302', '86311', '86333', '86339', '86410']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'mark.dickinson', 'jyasskin']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue5812'
    versions = ['Python 3.1', 'Python 2.7']

    @mdickinson
    Copy link
    Member Author

    When the Fractions module was first added to Python, it was decided that
    when constructing from a string, decimal strings should be allowed, but
    not those including an exponent. For example, Fraction('1.1') is
    currently valid, but Fraction('1.1e6') is not.

    I think exponents should be permitted, for a couple of reasons:

    (1) consistency: there's a clearly-defined notion of a numeric string
    of the form ([sign] integer_part [fractional_part] [exponent]); the
    float and Decimal constructors both accept all numeric strings.
    Fraction currently accepts some, but not all of these.

    (2) Easy interactions with floats: with this addition, a Fraction can
    always be constructed from the str() or repr() of a finite float or
    finite Decimal; without it, only some of those strings can be converted.

    (3) Ease of parsing files containing numeric strings.

    (4) It's a very simple change! See attached patch.

    Jeffrey, any thoughts?

    @mdickinson mdickinson self-assigned this Apr 22, 2009
    @mdickinson mdickinson added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Apr 22, 2009
    @rhettinger
    Copy link
    Contributor

    Also, it would be nice if the Fraction constructor accepted both a
    numerator and denominator that we also fractions. This came up in a
    demonstration of continued fractions and it bombed when the denominator
    was not allowed to be a Fraction itself. The meaning is well-defined
    and it is also a simple change.

    @mdickinson
    Copy link
    Member Author

    Also, it would be nice if the Fraction constructor accepted both a
    numerator and denominator that we also fractions.

    This makes sense to me. It reminds me of the way that complex(real,
    imag) allows real and imag to be complex numbers, and does the 'right
    thing'.

    @jyasskin
    Copy link
    Mannequin

    jyasskin mannequin commented Apr 22, 2009

    Sounds good to me. I can't find any real objections to the new format in
    bpo-1682, just me complaining that it might be feature creep.

    @mdickinson
    Copy link
    Member Author

    Fraction constructor modified to accept all numeric strings
    in r71806 (py3k), 71808 (trunk).

    Leaving this open for Raymond's suggested change.

    @mdickinson
    Copy link
    Member Author

    Here's a patch for making Fraction(3, Fraction(4, 5)) valid.
    It's against the trunk.

    @mdickinson
    Copy link
    Member Author

    Hmm. That patch isn't quite right, in at least two respects

    • if the single-argument constructor is using LBYL (i.e., an
      explicit isinstance(x, Rational), then the two-argument
      constructor should too.
    • the zero-division check should come *after* the type check;
      that is, Rational(1, 0j) should raise TypeError rather than
      ZeroDivisionError.

    Here's an updated version, that also makes the default second argument
    None rather than 1 and uses an 'is None' instead of '== 1' to determine
    number of arguments; this means that Fraction(3, 1.0) is no longer
    valid.

    @mdickinson
    Copy link
    Member Author

    Applied in r71832 (trunk), r71834 (py3k).

    One nice aspect of this change is that "Fraction(a, b)" is now
    a safe alternative to "a/b" in places where a and b might be
    either Fractions or integers.

    @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