Question
How can I visualize the floating number 0.1
in the 64 bit binary format stored in memory in Python, like in the image? A Python code which can show the sign bit, exponent bits, and precision bits like (1,01010110111,1010101...).
Background
A float number needs to be represented in binary bits, and as far as I could understand from the articles, it is ± p × 2**e
where ± is i 1 bit, p is 52 bits precision, and e is exponent in 11 bits.
I suppose the reason for (3 * 0.1 == 0.3) is False
is because 0.1
cannot be represented in 64 bits and it is 0.1000000000000000055511151231257827021181583404541015625
in Python in decimal.
print(3 * 0.1 == 0.3) # False
print(0.3 * 1 == 0.3) # True
print("%0.55f" % (1.0 / 10))
-----
0.1000000000000000055511151231257827021181583404541015625
print("%0.55f" % (0.1000000000000000055511151231257827021181583404541015625 - 0.1))
-----
0.0000000000000000000000000000000000000000000000000000000
However, still not able to understand how it works and would like to see how it looks like in the binary format.