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

Confusing float formatting for empty presentation type. #50170

Closed
mdickinson opened this issue May 4, 2009 · 9 comments
Closed

Confusing float formatting for empty presentation type. #50170

mdickinson opened this issue May 4, 2009 · 9 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@mdickinson
Copy link
Member

BPO 5920
Nosy @mdickinson, @ericvsmith
Files
  • issue5920.patch
  • issue5920_v2.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-05-05.18:27:46.693>
    created_at = <Date 2009-05-04.11:10:38.257>
    labels = ['interpreter-core', 'type-bug']
    title = 'Confusing float formatting for empty presentation type.'
    updated_at = <Date 2009-05-05.18:27:46.691>
    user = 'https://github.com/mdickinson'

    bugs.python.org fields:

    activity = <Date 2009-05-05.18:27:46.691>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2009-05-05.18:27:46.693>
    closer = 'eric.smith'
    components = ['Interpreter Core']
    creation = <Date 2009-05-04.11:10:38.257>
    creator = 'mark.dickinson'
    dependencies = []
    files = ['13866', '13893']
    hgrepos = []
    issue_num = 5920
    keywords = ['patch']
    message_count = 9.0
    messages = ['87114', '87115', '87148', '87155', '87165', '87170', '87229', '87242', '87267']
    nosy_count = 2.0
    nosy_names = ['mark.dickinson', 'eric.smith']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue5920'
    versions = ['Python 3.1', 'Python 2.7']

    @mdickinson
    Copy link
    Member Author

    I think the change in precision in the following is surprising, and should
    be fixed for 2.7 and 3.1:

    Python 3.1a2+ (py3k:72258:72259, May  4 2009, 11:49:27) 
    [GCC 4.0.1 (Apple Inc. build 5490)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> '{}'.format(10/3.)
    '3.33333333333'
    >>> '{:}'.format(10/3.)
    '3.33333333333'
    >>> '{:13}'.format(10/3.)
    '      3.33333'
    >>> '{:-}'.format(10/3.)
    '3.33333'

    Notice that the first two results above give 12 digits of precision,
    while the third and fourth both give 6 digits of precision.

    The above behaviour can be explained by a close reading of PEP-3101.
    The last two results come from the section describing the empty
    presentation type for floats:

    """similar to 'g', except that it prints at least one digit after the
    decimal point."""

    along with the fact that for 'g', the default precision is 6. The first
    two results come from this sentence, at the end of the same section:

    """For all built-in types, an empty format specification will produce the
    equivalent of str(value)."""

    and the fact that str(float) uses a precision of 12.

    To me, it seems wrong, and potentially confusing, that adding a field
    width, or alignment specifier, or sign specifier, all of which have
    nothing to do with precision, should change the precision.

    One possible solution would be to have the empty presentation type always
    use a precision of 12. The output would still be 'similar to 'g'', except
    for the difference in default precision.

    @mdickinson mdickinson added the type-bug An unexpected behavior, bug, or error label May 4, 2009
    @mdickinson
    Copy link
    Member Author

    Here's a patch, that also changed complex formatting in the same way.

    @ericvsmith
    Copy link
    Member

    About your patch: Wouldn't it make more sense to switch to type 's',
    with a precision of 0, so as to use the same logic that float_str uses?
    I realize it's the same result, but if we're making the point that we
    want to match float_str, it makes sense to me to use the same logic so
    you don't have to walk through the code to figure it out.

    Or, also switch float_str to use 'g' with a precision of
    PyFloat_STR_PRECISION, and get rid of 's' altogether. But maybe we
    should do that as a separate step, after this change.

    @mdickinson
    Copy link
    Member Author

    Wouldn't it make more sense to switch to type 's',
    with a precision of 0, so as to use the same logic that float_str
    uses?

    Yes, that makes some sense. How would you handle
    '{:.10}'.format(10/3.), though? We could either change 's' to allow a
    precision, or use 's' when there's no precision specified and 'g' (with
    the ADD_DOT_0 flag) otherwise.

    Or, also switch float_str to use 'g' with a precision of
    PyFloat_STR_PRECISION, and get rid of 's' altogether.

    This sounds good to me. It does feel as though there's unnecessary
    duplication with the current setup.

    @ericvsmith
    Copy link
    Member

    Yes, that makes some sense. How would you handle
    '{:.10}'.format(10/3.), though? We could either change 's' to allow a
    precision, or use 's' when there's no precision specified and 'g' (with
    the ADD_DOT_0 flag) otherwise.

    Good point, I hadn't thought of that. I'm not a big fan of switching
    between 's' and 'g' depending on whether a precision is specified.

    > Or, also switch float_str to use 'g' with a precision of
    > PyFloat_STR_PRECISION, and get rid of 's' altogether.

    This sounds good to me. It does feel as though there's unnecessary
    duplication with the current setup.

    A major point of 's' was to not specify the precision, so I'd prefer to
    remove 's' and use 'g' with a specified precision.

    @mdickinson
    Copy link
    Member Author

    ... so I'd prefer to remove 's' and use 'g' with a specified precision.

    Let's do that then. I'll update the patch.

    @mdickinson
    Copy link
    Member Author

    Updated patch, that removes the 's' type code.

    @ericvsmith
    Copy link
    Member

    I've reviewed this and it looks good. I'll check it in to py3k shortly.
    Then I'll backport it to 2.7 and fix the user documentation.

    @ericvsmith
    Copy link
    Member

    Committed in py3k r72333 and trunk r72348. I also updated the
    documentation. Closing the issue.

    @ericvsmith ericvsmith added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label May 5, 2009
    @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

    2 participants