`j`

: Next unread message`k`

: Previous unread message`j a`

: Jump to all threads`j l`

: Jump to MailingList overview

Thibault Hilaire

13 Sep
2017
13 Sep
'17

10:36 a.m.

Hi everybody

I chose it because it's easy to write. Maybe math.pi is a better example :-)

math.pi.hex() '0x1.921fb54442d18p+1'

3.141592653589793 is four fewer characters to type, just as accurate, and far more recognizable.

Of course, for a lost of numbers, the decimal representation is simpler, and just as accurate as the radix-2 hexadecimal representation. But, due to the radix-10 and radix-2 used in the two representations, the radix-2 may be much easier to use. In the "Handbook of Floating-Point Arithmetic" (JM Muller et al, Birkhauser editor, page 40),the authors claims that the largest exact decimal representation of a double-precision floating-point requires 767 digits !! So it is not always few characters to type to be just as accurate !! For example (this is the largest exact decimal representation of a single-precision 32-bit float):

1.17549421069244107548702944484928734882705242874589333385717453057158887047561890426550235133618116378784179687e-38 and 0x1.fffffc0000000p-127 are exactly the same number (one in decimal representation, the other in radix-2 hexadecimal)!

So, we have several alternatives:

- do nothing, and continue to use float.hex() and float.fromhex()
support one of some of the following possibilities:

a) support the hexadecimal floating-point literals, like released in C++17 (I don't know if some other languages already support this)

`>>> x = 0x1.2492492492492p-3`

b) extend the constructor float to be able to build float from hexadecimal

`>>> x = float('0x1.2492492492492p-3')`

I don't know if we should add a "base=None" or not c) extend the string formatting with '%a' (as in C since C99) and '{:a}'

`>>> s = '%a' % (x,)`

Serhly proposes to use '%x' and '{:x}', but I prefer to be consistent with C

To my point of view (my needs are maybe not very representative, as computer scientist working in computer arithmetic), a full support for radix-2 representation is required (it is sometimes easier/quicker to exchange data between different softwares in plain text, and radix-2 hexadecimal is the best way to do it, because it is exact). Also, support option a) will help me to generate python code (from other python or C code) without building the float at runtime with fromhex(). My numbers will be literals, not string converted in float! Option c) will help me to print my data in the same way as in C, and be consistent (same formatting character) And option b) will be just here for consistency with new hexadecimal literals...

Finally, I am now considering writing a PEP from Serhly Storchaka's idea, but if someone else wants to start it, I can help/contribute.

Thanks

Thibault