In [1]:
import sys
import math
import ctypes
import struct
In [2]:
sys.float_info
Out[2]:
In [3]:
sys.int_info
Out[3]:
In [4]:
format(0.125, '.12g')
Out[4]:
In [5]:
format(0.101, '.12g')
Out[5]:
In [6]:
0.3
Out[6]:
In [7]:
0.1
Out[7]:
In [8]:
0.1 + 0.1 + 0.1 == 0.3
Out[8]:
In [9]:
f"{0x1234:b}"
Out[9]:
With formatting
In [10]:
a = 1
In [11]:
"{0:b}".format(a)
Out[11]:
with bin()
In [12]:
bin(a)
Out[12]:
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:
$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]:
In [14]:
a = 1.0
a
Out[14]:
In [15]:
bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)
Out[15]:
In [16]:
bin(struct.unpack('!i',struct.pack('!f',a))[0])
Out[16]:
In [17]:
a = 2.0
a
Out[17]:
In [18]:
bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)
Out[18]:
In [19]:
bin(struct.unpack('!i',struct.pack('!f',a))[0])
Out[19]:
In [20]:
a = 4.0
a
Out[20]:
In [21]:
bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)
Out[21]:
In [22]:
bin(struct.unpack('!i',struct.pack('!f',a))[0])
Out[22]:
In [23]:
a = 8.0
a
Out[23]:
In [24]:
bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)
Out[24]:
In [25]:
bin(struct.unpack('!i',struct.pack('!f',a))[0])
Out[25]:
In [26]:
a = 2.0**500
a
Out[26]:
In [27]:
bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)
Out[27]:
In [28]:
bin(struct.unpack('!i',struct.pack('!f',a))[0])
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])
In [ ]:
sum([0.1] * 10) == 1.0
In [ ]:
math.fsum([0.1] * 10) == 1.0
In [ ]: