On 1/21/2020 4:32 AM, Serhiy Storchaka wrote:
21.01.20 10:37, Eric V. Smith пише:
For what it's worth, float's repr internally uses a format of '.17g'.
So, format(value, '.17g') will be equal to repr(f), where f is any
float.
It was in Python 2, but since Python 3.1 it returns the shortest
unambiguous representation, which may be shorter than 17 digits.
https://docs.python.org/3/whatsnew/3.1.html#other-language-changes
https://bugs.python.org/issue1580
Yes (I wrote a lot of that), but '.17g' doesn't mean to always show 17
digits. See
https://github.com/python/cpython/blob/master/Python/pystrtod.c#L825
where the repr (which is format_code =='r') is translated to format_code
= 'g' and precision = 17.
But I was wrong about them being equivalent: 'g' will drop the trailing
'.0' if it exists, and repr() will not (via flags = Py_DTSF_ADD_DOT_0).
And I'm almost positive that 2.7 also uses short float repr, but doesn't
have str == repr for floats. But 2.7 is dead to me (despite that fact
that I use it extensively for one client!), so I'm not going to bother
double checking.
Eric
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/FILPSQ3FOJF5IZNL4BEZSKJJS6RNKT4A/
Code of Conduct: http://python.org/psf/codeofconduct/