[Python-ideas] Hexadecimal floating literals
Chris Barker
chris.barker at noaa.gov
Fri Sep 22 13:06:17 EDT 2017
On Fri, Sep 22, 2017 at 8:37 AM, Guido van Rossum <guido at python.org> wrote:
> On Thu, Sep 21, 2017 at 9:20 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>
>> >>> one_tenth = 0x1.0 / 0xA.0
>> >>> two_tenths = 0x2.0 / 0xA.0
>> >>> three_tenths = 0x3.0 / 0xA.0
>> >>> three_tenths == one_tenth + two_tenths
>> False
>>
>
> OMG Regardless of whether we introduce this feature, .hex() is the way to
> show what's going on here:
>
> >>> 0.1.hex()
> '0x1.999999999999ap-4'
> >>> 0.2.hex()
> '0x1.999999999999ap-3'
> >>> 0.3.hex()
> '0x1.3333333333333p-2'
> >>> (0.1+0.2).hex()
> '0x1.3333333333334p-2'
> >>>
>
> This shows so clearly that there's 1 bit difference!
>
Thanks! I really should add this example to the math.isclose() docs....
.hex is mentioned in:
https://docs.python.org/3/tutorial/floatingpoint.html
but I don't see it used in a nice clear example like this.
-CHB
>
>
> --
> --Guido van Rossum (python.org/~guido <http://python.org/%7Eguido>)
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170922/d1c19427/attachment.html>
More information about the Python-ideas
mailing list