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

10e667.__format__('+') should return 'inf' #48732

Closed
DinoV opened this issue Dec 1, 2008 · 18 comments
Closed

10e667.__format__('+') should return 'inf' #48732

DinoV opened this issue Dec 1, 2008 · 18 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@DinoV
Copy link
Contributor

DinoV commented Dec 1, 2008

BPO 4482
Nosy @mdickinson, @Cito, @ericvsmith, @DinoV
Files
  • inf-test.patch
  • inf.patch
  • issue4482_tests.patch
  • issue4482_tests-1.patch
  • issue4482_tests-2.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/ericvsmith'
    closed_at = <Date 2009-12-02.18:03:15.283>
    created_at = <Date 2008-12-01.17:52:24.986>
    labels = ['interpreter-core', 'type-bug']
    title = "10e667.__format__('+') should return 'inf'"
    updated_at = <Date 2009-12-02.18:03:15.282>
    user = 'https://github.com/DinoV'

    bugs.python.org fields:

    activity = <Date 2009-12-02.18:03:15.282>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2009-12-02.18:03:15.283>
    closer = 'eric.smith'
    components = ['Interpreter Core']
    creation = <Date 2008-12-01.17:52:24.986>
    creator = 'dino.viehland'
    dependencies = []
    files = ['13474', '13475', '13708', '15431', '15432']
    hgrepos = []
    issue_num = 4482
    keywords = ['patch']
    message_count = 18.0
    messages = ['76703', '84581', '85906', '85907', '85909', '85910', '86050', '86051', '86077', '86340', '86341', '86344', '87093', '95874', '95875', '95876', '95880', '95914']
    nosy_count = 6.0
    nosy_names = ['mark.dickinson', 'cito', 'eric.smith', 'stutzbach', 'cdavid', 'dino.viehland']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue4482'
    versions = ['Python 3.1', 'Python 2.7']

    @DinoV
    Copy link
    Contributor Author

    DinoV commented Dec 1, 2008

    10e667.__format__('+') returns '+1.0#INF'

    vs:

    10e667.__format__('') which returns 'inf'

    The docs say 'inf' is right.

    @DinoV DinoV added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Dec 1, 2008
    @ericvsmith ericvsmith self-assigned this Dec 1, 2008
    @stutzbach
    Copy link
    Mannequin

    stutzbach mannequin commented Mar 30, 2009

    Here's a patch to add tests for this and a few other problems with
    formatting inf/-inf/nan, as well as a patch that fixes the problems.

    @ericvsmith
    Copy link
    Member

    This is a duplicate of 4799, which I've closed.

    I've resisted fixing this because differences in various platform
    sprintf's have made it difficult. Now that Mark Dickinson and I are
    close to removing the use of sprintf for float formatting, I'll have the
    tools to deal with this better.

    Not sure what this means for 2.7, yet. We're not planning on backporting
    the new float formatting to 2.7.

    @ericvsmith
    Copy link
    Member

    Actually this isn't quite a duplicate of 4799, but it's close. Any fix
    to this issue will also address 4799's original report:

    On windows, with python 2.6, s = '%s' % float('inf') is 'inf',
    but s ='%f' % float('inf') is equal to '1.#INF'.

    @stutzbach
    Copy link
    Mannequin

    stutzbach mannequin commented Apr 12, 2009

    The patch I submitted adds a special-case for inf/-inf/NaN so that
    sprintf is avoided for those values. Should work on any platform, I
    believe.

    I'm not sure how it interacts with your 3.x plans. Seems like it would
    be a good patch for 2.7 at least, though.

    (and the patch that adds tests should go into both 2.7 and 3.x)

    @ericvsmith
    Copy link
    Member

    Agreed. That might be the strategy for 2.7.

    But it depends on how much of the other float and int formatting code I
    can re-use from 3.1. If I can hide the 2.7/3.1 differences in
    PyOS_double_to_string, then the 2.7 and 3.1 code should be identical,
    and the fix will be different in the 3.1 code base.

    I'll decide in the next few weeks.

    @mdickinson
    Copy link
    Member

    A somewhat related comment about formatting of infs and nans: I think
    that a formatted nan should never include a sign, even when it's
    explicitly asked for. For example:

    "%+e" % float('nan')

    should give "nan" rather than "+nan" or "-nan". (In contrast, infinities
    should, and currently do, get the requested sign.)

    @mdickinson
    Copy link
    Member

    Here are my proposed tests for this---they're for percent formatting
    rather than format() formatting. They conflict slightly with Daniel's
    tests, in that I think

    format(float('inf'), '+')

    should produce '+inf' rather than plain 'inf'.

    @stutzbach
    Copy link
    Mannequin

    stutzbach mannequin commented Apr 17, 2009

    I used 'inf' instead of "+inf' because the original bug report states
    that the docs say it should be 'inf'.

    Now that I actually look at the docs, I don't agree with the original
    report's interpretation and agree that '+inf' is better.

    @mdickinson
    Copy link
    Member

    A somewhat related comment about formatting of infs and nans: I think
    that a formatted nan should never include a sign, even when it's
    explicitly asked for.

    I'm not so sure about this any more. Sometimes the role of an explicit
    sign is to act as a binary operator or visual separator, when there's
    something immediately preceding the float being formatted. In this case
    one still wants the sign, even for nans.

    For example:

    >>> "{}{:+}".format(1, float('nan'))
    '1+nan'

    Also, the implementation is definitely easier this way. :-)

    @ericvsmith
    Copy link
    Member

    I agree on the alignment issue. Maybe if you explicitly ask for the
    "sign" of a NaN, it should always be a space.

    And when requesting leading zeros, you also get leading spaces instead?

    @mdickinson
    Copy link
    Member

    Maybe if you explicitly ask for the "sign" of a NaN, it should
    always be a space.

    Actually, I think '+nan' for format(float('nan'), '+'), and ' nan'
    for format(float('nan'), ' '). Then nans and infs can be treated
    identically, except that the sign of a nan is always considered to be
    positive, regardless of what the _Py_dg_dtoa function returns.

    And when requesting leading zeros, you also get leading spaces
    instead?

    Should do the same thing for nans and infs here. But certainly leading
    zeros should be avoided. Leading spaces sounds fine to me.

    @ericvsmith
    Copy link
    Member

    With the new PyOS_double_to_string, this issue largely goes away. There
    are some remaining issues with commas, 'n' and the like. I'll address those.

    @ericvsmith
    Copy link
    Member

    This patch has tests that currently pass on trunk. I think this is the
    desired behavior. I need to augment the tests for format() testing.

    @mdickinson
    Copy link
    Member

    Looks good to me, except that the second comment in the patch doesn't make
    a lot of sense any more.

    @ericvsmith
    Copy link
    Member

    This version adds format() testing and fixes the comments (I hope).

    @mdickinson
    Copy link
    Member

    LGTM.

    @ericvsmith
    Copy link
    Member

    Added tests to trunk in r76632 and py3k in r76634.

    Note that the only thing that was changed are the tests. The actual
    functionality was fixed earlier when the float formatting was reworked
    as part of the short float repr changes.

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants