[issue21195] None float format: incomplete documentation
New submission from Eric O. LEBIGOT: The documentation for a None (empty) format for floats indicates that it is equivalent to the g format. This does not appear to be correct (http://stackoverflow.com/questions/16525924/precise-definition-of-float-stri...). The Python 3.4 documentation (https://docs.python.org/3.4/library/string.html#format-specification-mini-la...) seems to be much closer to what Python 2.7 does. It would be useful to have a more correct documentation for the effect of a None format for floats in Python 2.7 (maybe by copying the Python 3.4 documentation if it applies). ---------- assignee: docs@python components: Documentation messages: 215871 nosy: docs@python, lebigot priority: normal severity: normal status: open title: None float format: incomplete documentation type: enhancement versions: Python 2.7 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21195> _______________________________________
Eric V. Smith added the comment: An empty format specifier can (and to my knowledge, does) match str(). So you get:
format(1e10, '') '10000000000.0' format(1e10, 'g') '1e+10' str(1e10) '10000000000.0'
The SO question is asking about an empty "presentation type", which is indeed similar to 'g' for floats. I think this is all the same in 2.7 and 3.4, with the exception of how str() might operate on floats. ---------- nosy: +eric.smith _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21195> _______________________________________
Eric O. LEBIGOT added the comment: These examples are good. I am confused, though, about "The SO question is asking about an empty "presentation type", which is indeed similar to 'g' for floats.": the question is actually about why the '' format gives a result that differs from the 'g' format despite the Python 2.7 documentation saying that a "None" format type is "The same as 'g'.". Both the SO question and your examples show that the Python 2.7 documentation is incorrect, unless the other commenters and I were missing something… Clarifying the documentation would in any case be useful, as the comments to the SO question show… ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21195> _______________________________________
Changes by Mark Dickinson <dickinsm@gmail.com>: ---------- nosy: +mark.dickinson _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21195> _______________________________________
Eric V. Smith added the comment: The rule is: - if the entire format specifier is the empty string (not None, but ''), then return str(value) - else, look at the presentation type. if it is missing, set it to something like 'g' - do the normal float formatting using the presentation type The first of these has an empty string for the format specifier (so uses str(1e10), the rest do not:
format(1e10, '') '10000000000.0' format(1e10, ' ') ' 10000000000.0' format(1e10, ' g') ' 1e+10' format(1e10, ' e') ' 1.000000e+10' format(1e10, ' f') ' 10000000000.000000'
Now, how much the "something like g" part of the above is true is debatable. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21195> _______________________________________
Eric O. LEBIGOT added the comment: The Python 2.7 goes even as far as to say that format(1e10, ' ') should give "the same as" format(1e10, ' g') (not something "similar to g"), which is obviously incorrect. If the Python 3.4 documentation for the empty presentation type of floats were used in the Python 2.7 documentation, it would be closer to the real behavior of Python 2.7, so that would be an improvement. Now, the real question is whether the Python 3.4 documentation applies exactly to Python 2.7, here, or whether improving the Python 2.7 documentation would require a different description. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue21195> _______________________________________
Irit Katriel <iritkatriel@yahoo.com> added the comment: Python 2.7-only issue. ---------- nosy: +iritkatriel resolution: -> out of date stage: -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue21195> _______________________________________
participants (4)
-
Eric O. LEBIGOT
-
Eric V. Smith
-
Irit Katriel
-
Mark Dickinson