You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
float.__format__() handles trailing zeros inconsistently in “general” format #84957
$ 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?
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.)
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.
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
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
Activity
davidchambers commentedon May 26, 2020
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?
ericvsmith commentedon May 26, 2020
FWIW, which is probably not much with ".g" formatting, this is how Decimal behaves:
mdickinson commentedon May 26, 2020
Very interesting. Agreed that this looks like a bug. It affects old-style formatting, too:
mdickinson commentedon May 26, 2020
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.)
4 remaining items
mdickinson commentedon May 26, 2020
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.
ericvsmith commentedon May 26, 2020
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.
[-]str.format() handles trailing zeros inconsistently in “general” format[/-][+]float.__format__() handles trailing zeros inconsistently in “general” format[/+][-]str.format() handles trailing zeros inconsistently in “general” format[/-][+]float.__format__() handles trailing zeros inconsistently in “general” format[/+]rhettinger commentedon May 27, 2020
+1 for 3.9 and later.
mdickinson commentedon May 29, 2020
New changeset 895c9c1 by Mark Dickinson in branch 'master':
bpo-40780: Fix failure of _Py_dg_dtoa to remove trailing zeros (GH-20435)
895c9c1
mdickinson commentedon May 29, 2020
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
mdickinson commentedon May 29, 2020
Fixed in master and 3.9; not backporting to 3.8 or 3.7, as discussed.