Nov-25-2023, 10:41 PM
(This post was last modified: Nov-25-2023, 10:41 PM by Gribouillis.)
Python floats are normally represented as 64 bits binary numbers, which means that they cannot represent faithfully more than 16 decimal digits. To see the capabilities of your Python print
sys.float_infoAlso see the following example shows that python cannot tell the difference between 2/3 and 2/3 truncated to 16 digits
>>> x = 2/3 >>> f"{x:.16}" '0.6666666666666666' >>> >>> x == float(f"{x:.15}") False >>> x == float(f"{x:.16}") True >>>Note: this is not a limitation of Python alone, most usual programming languages use the same floating numbers. Specialized libraries such as gmpy2 handle multiprecision floating numbers with any number of digits.