-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Wrong default precision in documentation for format #66736
Comments
The format documentation for the Format Specification Mini-Language for python 3.3 (perhaps newer and older as well) at: https://docs.python.org/3.3/library/string.html States for type '' (for floating point numbers):
This appears not to be true, the following code example, run in Python 3.3.2: >>> '{}'.format(3.14159265358979323846264338327950288419)
'3.141592653589793' As it can be seen form the output the default precision appears to be 15. |
I think this is a result of changing the precision of str() to match repr(). 2.7 prints: |
"The precision is a decimal number indicating how many digits should be displayed ... before and after the decimal point for a floating point value formatted with 'g' or 'G'. It seems that str, repr, and '' are using precision 16, and the doc should be changed to match.
>>> '{}'.format(3.14159265358979323846264338327950288419)
'3.141592653589793'
>>> '{:.16g}'.format(3.14159265358979323846264338327950288419)
'3.141592653589793'
>>> str(3.14159265358979323846264338327950288419)
'3.141592653589793' # 16
But I discovered this 'anomaly' (bug?)
>>> str(31.4159265358979323846264338327950288419)
'31.41592653589793'
>>> str(33.14159265358979323846264338327950288419)
'33.1415926535898' # precision 15
I expected this last to be
'33.14159265358979'
as 32... rounds down, not up. repr and '{}'.format act the same. |
None of them is using a fixed precision: they're all using David Gay's implementation of the "shortest string" algorithm (à la Burger and Dybvig). For repr, this is the case since Python 3.1 / 2.7; for str and formatting with no type specifier, since Python 3.2. The docs definitely do need updating here.
'33.1415926535898' is shorter, and rounds back to the same floating-point number, so that's what Gay's algorithm gives here. |
Here's a proposed fix. |
Hmm; it's only outputs in non-scientific notation that are guaranteed to have a decimal point. Patch updated. |
I see now that my expectation, based on decimal rounding rather than binary conversion and rounding, was wrong ;-)
>>> 33.14159265358979323846264338327950288419 == 33.1415926535898
True
>>> 33.14159265358979323846264338327950288419 == 33.14159265358979
False
>>> format(33.14159265358979323846264338327950288419, '.18')
'33.1415926535897967' Tommy: 3.3 only gets security fixes. When a core developer (indicated by the blue and yellow snake symbol) resets Versions, you should leave them alone or ask before changing. As for the patch: 'non-scientific' == 'fixed-point', the expression already used in the table. The rewrite omits the fact the exception is to match str and that g and str are otherwise the same except for fixed versus 'as needed' precision. I note that '' = 'd' for integers also makes '' for integers similar to str() as modified by the preceding options. An alternate rewrite: Similar to 'g', except that fixed-point notation, when used, has at least one digit past the decimal point. The default precision is as high as needed to represent the particular value. The overall effect is to match the output of str() as altered by the other format modifiers. --
The following in the examples could be fixed in the same patch
>>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it always
'+3.140000; -3.140000' add to the comment 'it always displays a sign'. |
Terry: your rewrite looks fine to me. +1 for committing that. |
New changeset 041d0752171a by Terry Jan Reedy in branch '3.4': |
Tommy, thanks for reporting this. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: