Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KostyaEsmukov/e3a00632c9f38986abc24e7e14eb59b0 to your computer and use it in GitHub Desktop.
Save KostyaEsmukov/e3a00632c9f38986abc24e7e14eb59b0 to your computer and use it in GitHub Desktop.

IEEE 754 double binary representation with Python

>>> import struct
>>> pd = lambda x: format(struct.unpack('!Q', struct.pack('!d', x))[0], '064b')
>>> pd(180.00000000000003)    
# sign (1 bit) | exponent (11 bit) | fraction (52 bit)
#\/         \/
'0100000001100110100000000000000000000000000000000000000000000001'
>>> pd(180)
'0100000001100110100000000000000000000000000000000000000000000000'
>>> pd(-180)
'1100000001100110100000000000000000000000000000000000000000000000'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment