[Python-ideas] Hexadecimal floating literals

Victor Stinner victor.stinner at gmail.com
Fri Sep 8 15:05:44 EDT 2017


2017-09-07 23:57 GMT-07:00 Serhiy Storchaka <storchaka at gmail.com>:
> The support of hexadecimal floating literals (like 0xC.68p+2) is included in
> just released C++17 standard. Seems this becomes a mainstream.

Floating literal using base 2 (or base 2^n, like hexadecimal, 2^4) is
the only way to get exact values in a portable way. So yeah, we need
it. We already have float.hex() since Python 2.6.

> In Python float.hex() returns hexadecimal string representation. Is it a
> time to add more support of hexadecimal floating literals? Accept them in
> float constructor and in Python parser? And maybe add support of hexadecimal
> formatting ('%x' and '{:x}')?

I dislike "%x" % float, since "%x" is a very old format from C printf
and I expect it to only work for integers. For example, bytes.hex()
exists (since Python 3.5) but b'%x' % b'hello' doesn't work.

Since format() is a "new" way to format strings, and each type is free
to implement its own formatters, I kind of like the idea of support
float.hex() here.

Do we need a short PEP, since it changes the Python grammar? It may be
nice to describe the exact grammar for float literals.

Victor


More information about the Python-ideas mailing list