← 输入框|github.com|刷新
Skip to content

float.__format__() handles trailing zeros inconsistently in “general” format #84957

Closed

Description

@davidchambers
BPO 40780
Nosy @rhettinger, @mdickinson, @ericvsmith, @davidchambers, @miss-islington
PRs
  • bpo-40780: Fix failure of _Py_dg_dtoa to remove trailing zeros #20435
  • [3.9] bpo-40780: Fix failure of _Py_dg_dtoa to remove trailing zeros (GH-20435) #20514
  • 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 2020-05-29.13:48:02.325>
    created_at = <Date 2020-05-26.14:15:19.529>
    labels = ['type-bug', '3.9', '3.10']
    title = 'float.__format__() handles trailing zeros inconsistently in \xe2\x80\x9cgeneral\xe2\x80\x9d format'
    updated_at = <Date 2020-05-29.13:48:02.324>
    user = 'https://github.com/davidchambers'

    bugs.python.org fields:

    activity = <Date 2020-05-29.13:48:02.324>
    actor = 'mark.dickinson'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2020-05-29.13:48:02.325>
    closer = 'mark.dickinson'
    components = []
    creation = <Date 2020-05-26.14:15:19.529>
    creator = 'davidchambers'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40780
    keywords = ['patch']
    message_count = 13.0
    messages = ['369983', '369996', '370002', '370006', '370009', '370012', '370021', '370025', '370026', '370133', '370309', '370312', '370313']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'mark.dickinson', 'eric.smith', 'davidchambers', 'miss-islington']
    pr_nums = ['20435', '20514']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue40780'
    versions = ['Python 3.9', 'Python 3.10']

    Activity

    1. davidchambers commented on May 26, 2020

      davidchambersmannequin
      MannequinAuthor

      According to https://docs.python.org/3/library/string.html#format-specification-mini-language, “insignificant trailing zeros are removed from the significand” when 'g' is specified. I encountered a situation in which a trailing zero is not removed:

          $ python3
          Python 3.7.7 (default, Mar 10 2020, 15:43:03)
          [Clang 11.0.0 (clang-1100.0.33.17)] on darwin
          Type "help", "copyright", "credits" or "license" for more information.
          >>> '{0:.3g}'.format(1504)
          '1.5e+03'
          >>> '{0:.3g}'.format(1505)
          '1.50e+03'
          >>> '{0:.3g}'.format(1506)
          '1.51e+03'

      Is this behaviour intentional? If so, why is the trailing zero in 1.50 considered significant for 1505 but insignificant for 1504?

    2. ericvsmith commented on May 26, 2020

      @ericvsmith
      Member

      FWIW, which is probably not much with ".g" formatting, this is how Decimal behaves:

      >>> from decimal import Decimal as D
      >>> format(D(1504), '.3g')
      '1.50e+3'
      >>> format(D(1505), '.3g')
      '1.50e+3'
      >>> format(D(1506), '.3g')
      '1.51e+3'
      >>> format(D(1504.0), '.3g')
      '1.50e+3'
      >>> format(D(1505.0), '.3g')
      '1.50e+3'
      >>> format(D(1506.0), '.3g')
      '1.51e+3'
      >>> format(D("1504.0"), '.3g')
      '1.50e+3'
      >>> format(D("1505.0"), '.3g')
      '1.50e+3'
      >>> format(D("1506.0"), '.3g')
      '1.51e+3'
    3. mdickinson commented on May 26, 2020

      @mdickinson
      Member

      Very interesting. Agreed that this looks like a bug. It affects old-style formatting, too:

      >>> "%.3g" % 1503
      '1.5e+03'
      >>> "%.3g" % 1504
      '1.5e+03'
      >>> "%.3g" % 1505
      '1.50e+03'
    4. mdickinson commented on May 26, 2020

      @mdickinson
      Member

      This appears to go all the way down to _Py_dg_dtoa, which in mode 3 is supposed to suppress trailing zeros.

      I'll dig in and see if I can find a fix. (Assigning to me so that I don't forget it, but I also don't want to block anyone else - if anyone else feels like working on this, please do go ahead.)

    5. self-assigned this
      on May 26, 2020
    6. 4 remaining items

    7. mdickinson commented on May 26, 2020

      @mdickinson
      Member

      I'm wondering how far back the fix should be backported. Clearly it should go into the 3.9 branch as well as master, but it feels like the sort of fix where the behaviour change resulting from the fix is as likely to break code as the bug itself.

    8. ericvsmith commented on May 26, 2020

      @ericvsmith
      Member

      I think I'd just do 3.9 and master. It does seem subtle for a minor release, when people are less likely to be looking at the release notes.

    9. changed the title [-]str.format() handles trailing zeros inconsistently in “general” format[/-] [+]float.__format__() handles trailing zeros inconsistently in “general” format[/+] on May 26, 2020
    10. changed the title [-]str.format() handles trailing zeros inconsistently in “general” format[/-] [+]float.__format__() handles trailing zeros inconsistently in “general” format[/+] on May 26, 2020
    11. rhettinger commented on May 27, 2020

      @rhettinger
      Contributor

      +1 for 3.9 and later.

    12. mdickinson commented on May 29, 2020

      @mdickinson
      Member

      New changeset 895c9c1 by Mark Dickinson in branch 'master':
      bpo-40780: Fix failure of _Py_dg_dtoa to remove trailing zeros (GH-20435)
      895c9c1

    13. mdickinson commented on May 29, 2020

      @mdickinson
      Member

      New changeset ad088ca by Miss Islington (bot) in branch '3.9':
      bpo-40780: Fix failure of _Py_dg_dtoa to remove trailing zeros (GH-20435) (GH-20514)
      ad088ca

    14. mdickinson commented on May 29, 2020

      @mdickinson
      Member

      Fixed in master and 3.9; not backporting to 3.8 or 3.7, as discussed.

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

    3.10 (EOL)end of life3.9 (EOL)end of lifetype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions