In [1]:

import sys
import math
import ctypes
import struct




In [2]:

sys.float_info




Out[2]:

sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)




In [3]:

sys.int_info




Out[3]:

sys.int_info(bits_per_digit=30, sizeof_digit=4)




In [4]:

format(0.125, '.12g')




Out[4]:

'0.125'




In [5]:

format(0.101, '.12g')




Out[5]:

'0.101'




In [6]:

0.3




Out[6]:

0.3




In [7]:

0.1




Out[7]:

0.1




In [8]:

0.1 + 0.1 + 0.1 == 0.3




Out[8]:

False




In [9]:

f"{0x1234:b}"




Out[9]:

'1001000110100'



### Binary representation of an integer

With formatting



In [10]:

a = 1




In [11]:

"{0:b}".format(a)




Out[11]:

'1'



with bin()



In [12]:

bin(a)




Out[12]:

'0b1'



### Binary representation of a float

radix point: the symbol used in numerical representations to separate the integer part of a number (to the left of the radix point) from its fractional part (to the right of the radix point)

Double precision:

• sign (+/-): 1 bit
• exponent: 11 bits
• significand precision: 53 bits (52 explicitly stored)

$e$ biased exponent

$(-1)^{sign} \left( 1.b_{p-1} b_{p-2} ... b_{0} \right)_{2} \cdot 2^{e-1023}$

$(-1)^{sign} \left( 1 + \sum_{i=1}^{p} b_{52-i} 2^{-i} \right) \cdot 2^{e-1023}$



In [13]:

sys.float_info




Out[13]:

sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)




In [14]:

a = 1.0
a




Out[14]:

1.0




In [15]:

bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)




Out[15]:

'0b111111100000000000000000000000'




In [16]:

bin(struct.unpack('!i',struct.pack('!f',a))[0])




Out[16]:

'0b111111100000000000000000000000'




In [17]:

a = 2.0
a




Out[17]:

2.0




In [18]:

bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)




Out[18]:

'0b1000000000000000000000000000000'




In [19]:

bin(struct.unpack('!i',struct.pack('!f',a))[0])




Out[19]:

'0b1000000000000000000000000000000'




In [20]:

a = 4.0
a




Out[20]:

4.0




In [21]:

bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)




Out[21]:

'0b1000000100000000000000000000000'




In [22]:

bin(struct.unpack('!i',struct.pack('!f',a))[0])




Out[22]:

'0b1000000100000000000000000000000'




In [23]:

a = 8.0
a




Out[23]:

8.0




In [24]:

bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)




Out[24]:

'0b1000001000000000000000000000000'




In [25]:

bin(struct.unpack('!i',struct.pack('!f',a))[0])




Out[25]:

'0b1000001000000000000000000000000'




In [26]:

a = 2.0**500
a




Out[26]:

3.273390607896142e+150




In [27]:

bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)




Out[27]:

'0b1111111100000000000000000000000'




In [28]:

bin(struct.unpack('!i',struct.pack('!f',a))[0])




---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-28-9085d3719c70> in <module>()
----> 1 bin(struct.unpack('!i',struct.pack('!f',a))[0])

OverflowError: float too large to pack with f format




In [ ]:

a = -2.0
a




In [ ]:

bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)




In [ ]:

bin(struct.unpack('!i',struct.pack('!f',a))[0])



## Representation errors



In [ ]:

sum([0.1] * 10) == 1.0




In [ ]:

math.fsum([0.1] * 10) == 1.0




In [ ]: