<jupyter_start><jupyter_text>**References:**- https://en.wikipedia.org/wiki/IEEE_754- https://en.wikipedia.org/wiki/Double-precision_floating-point_format- https://docs.python.org/3/library/functions.htmlround- https://docs.python.org/3/tutorial/floatingpoint.html- https://docs.python.org/3/library/functions.htmlbin<jupyter_code>import sys
import math
import ctypes
import struct
sys.float_info
sys.int_info
format(0.125, '.12g')
format(0.101, '.12g')
0.3
0.1
0.1 + 0.1 + 0.1 == 0.3
f"{0x1234:b}"<jupyter_output><empty_output><jupyter_text>Binary representation of an integer With formatting<jupyter_code>a = 1
"{0:b}".format(a)<jupyter_output><empty_output><jupyter_text>with `bin()`<jupyter_code>bin(a)<jupyter_output><empty_output><jupyter_text>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}$<jupyter_code>sys.float_info
a = 1.0
a
bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)
bin(struct.unpack('!i',struct.pack('!f',a))[0])
a = 2.0
a
bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)
bin(struct.unpack('!i',struct.pack('!f',a))[0])
a = 4.0
a
bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)
bin(struct.unpack('!i',struct.pack('!f',a))[0])
a = 8.0
a
bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)
bin(struct.unpack('!i',struct.pack('!f',a))[0])
a = 2.0**500
a
bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)
bin(struct.unpack('!i',struct.pack('!f',a))[0])
a = -2.0
a
bin(ctypes.c_uint.from_buffer(ctypes.c_float(a)).value)
bin(struct.unpack('!i',struct.pack('!f',a))[0])<jupyter_output><empty_output><jupyter_text>Representation errors<jupyter_code>sum([0.1] * 10) == 1.0
math.fsum([0.1] * 10) == 1.0<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20074696545284781, 'fraction_numerical': 0.046218487394957986, 'mean_word_length': 6.41522491349481, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '286382', 'license': 'MIT', 'n_tokens_mistral': 940, 'n_tokens_neox': 892, 'n_words': 143, 'path': 'python/core/built-in_types/number_representation.ipynb', 'repo_name': 'denglert/manuals', 'size': '2142'}