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

Keep trailing zeros in precision for string format option g #76971

Closed
sk1d mannequin opened this issue Feb 8, 2018 · 8 comments
Closed

Keep trailing zeros in precision for string format option g #76971

sk1d mannequin opened this issue Feb 8, 2018 · 8 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes docs Documentation in the Doc dir easy type-bug An unexpected behavior, bug, or error

Comments

@sk1d
Copy link
Mannequin

sk1d mannequin commented Feb 8, 2018

BPO 32790
Nosy @mdickinson, @ericvsmith, @matrixise, @miss-islington
PRs
  • bpo-32790: Added info about alt format using # for 'g' in chart #6624
  • [3.8] bpo-32790: Add info about alt format using GH- for 'g' in chart (GH-6624) #16121
  • [3.7] bpo-32790: Add info about alt format using GH- for 'g' in chart (GH-6624) #16122
  • 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 = None
    closed_at = <Date 2019-09-13.17:29:44.215>
    created_at = <Date 2018-02-08.02:08:36.788>
    labels = ['easy', 'type-bug', '3.8', '3.9', '3.7', 'docs']
    title = 'Keep trailing zeros in precision for string format option g'
    updated_at = <Date 2019-09-13.17:29:44.214>
    user = 'https://bugs.python.org/sk1d'

    bugs.python.org fields:

    activity = <Date 2019-09-13.17:29:44.214>
    actor = 'matrixise'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2019-09-13.17:29:44.215>
    closer = 'matrixise'
    components = ['Documentation']
    creation = <Date 2018-02-08.02:08:36.788>
    creator = 'sk1d'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32790
    keywords = ['patch', 'easy']
    message_count = 8.0
    messages = ['311813', '311816', '311831', '311847', '352376', '352378', '352379', '352380']
    nosy_count = 6.0
    nosy_names = ['mark.dickinson', 'eric.smith', 'docs@python', 'matrixise', 'miss-islington', 'sk1d']
    pr_nums = ['6624', '16121', '16122']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32790'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @sk1d
    Copy link
    Mannequin Author

    sk1d mannequin commented Feb 8, 2018

    The documentation starts the the string format parameter 'g':

    General format. For a given precision p >= 1, this rounds the number to **p significant digits** and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.

    I think the behavior of format is inconsistent here:
    >>> format(0.1949, '.2g')
    returns '0.19' as expected but
    >>> format(0.1950, '.2g')
    returns '0.2' instead of '0.20'
    
    This behavior for float is in my opinion the correct one here
    >>> format(0.1950, '.2f')
    returns '0.20'

    @sk1d sk1d mannequin added 3.7 (EOL) end of life type-bug An unexpected behavior, bug, or error labels Feb 8, 2018
    @mdickinson
    Copy link
    Member

    The behaviour here is intentional, though the reasons for doing it this way are at least partly historical: it's the way that %g formatting works in C's *printf functions (see C99 7.19.6.1p8 for details), and as a direct result of that it's also the way that old-style %-based formatting works in Python. That behaviour then got transferred to the new-style .format-based formatting for consistency.

    I don't think we can or should change the current behaviour here: there's a significant risk of breaking existing code.

    However, note that C does offer an *alternate* mode for .g-style formatting, using the '#' character, and this is also available in Python's formatting, both %-based and format-based:

    >>> "%.2g" % 0.1950
    '0.2'
    >>> "%#.2g" % 0.1950
    '0.20'

    and

    >>> format(0.1950, '.2g')
    '0.2'
    >>> format(0.1950, '#.2g')
    '0.20'

    Does this meet your needs?

    @sk1d
    Copy link
    Mannequin Author

    sk1d mannequin commented Feb 8, 2018

    This meet my needs.

    Maybe the documentation could also add this information in the chart for 'g' here: https://docs.python.org/3.7/library/string.html#format-specification-mini-language as only into the running text. As I did not notice it.

    As the text in the table for 'g' is already long, if you do not want to add all the same information again, add at least that trailing zeros will get removed.

    @mdickinson
    Copy link
    Member

    Maybe the documentation could also add this information in the chart for 'g' here:

    Agreed that that would make sense. All the information is *there* in the docs (the alternate format is described a bit above that table), but it's not very easy to extract the information.

    And that table entry is misleading as it currently is, since it suggests that trailing zeros are removed unconditionally: "In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it."

    Perhaps we could adopt something like the wording in the C standard, where it says:

    Finally, unless the # flag is used, any trailing zeros are removed
    from the fractional portion of the result and the decimal-point
    character is removed if there is no fractional portion remaining.

    Re-classifying as a documentation issue.

    @mdickinson mdickinson added the docs Documentation in the Doc dir label Feb 8, 2018
    @csabella csabella added easy 3.8 only security fixes labels Apr 26, 2018
    @matrixise
    Copy link
    Member

    New changeset d44542f by Stéphane Wirtel (bchhabra2490) in branch 'master':
    bpo-32790: Add info about alt format using # for 'g' in chart (GH-6624)
    d44542f

    @miss-islington
    Copy link
    Contributor

    New changeset 77878ca by Miss Islington (bot) in branch '3.7':
    bpo-32790: Add info about alt format using GH- for 'g' in chart (GH-6624)
    77878ca

    @miss-islington
    Copy link
    Contributor

    New changeset e6b14c0 by Miss Islington (bot) in branch '3.8':
    bpo-32790: Add info about alt format using GH- for 'g' in chart (GH-6624)
    e6b14c0

    @matrixise
    Copy link
    Member

    Thank you for your PR and this issue, the PR is merged into master, 3.8 and 3.7.

    @matrixise matrixise added the 3.9 only security fixes label Sep 13, 2019
    @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
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes docs Documentation in the Doc dir easy type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants