101 15.1. Representation Error Python Tutorial, Release 3.9.5 While pathological cases do exist, for most casual use of floating-point arithmetic you’ll see the result you expect in the end if you simply round the display of your final results to the number of decimal digits you expect. str() usually suffices, and for finer control see the$_{str.format()}$ method’s format specifiers in formatstrings. For use cases which require exact decimal representation, try using the decimal module which implements decimal arithmetic suitable for accounting applications and high-precision applications. Another form of exact arithmetic is supported by the fractions module which implements arithmetic based on rational numbers (so the numbers like 1/3 can be represented exactly). If you are a heavy user of floating point operations you should take a look at the Numerical Python package and many other packages for mathematical and statistical operations supplied by the SciPy project. See <https://scipy.org>. Python provides tools that may help on those rare occasions when you really do want to know the exact value of a float. The$_{float.as_integer_ratio()}$ method expresses the value of a float as a fraction: >>> x =3.14159 >>> x.as_integer_ratio() (3537115888337719, 1125899906842624) Since the ratio is exact, it can be used to losslessly recreate the original value: The$_{float.hex()}$ method expresses a float in hexadecimal (base 16), again giving the exact value stored by your computer: This precise hexadecimal representation can be used to reconstruct the float value exactly: Since the representation is exact, it is useful for reliably porting values across different versions of Python (platform independence) and exchanging data with other languages that support the same format (such as Java and C99). Another helpful tool is the$_{math.fsum()}$ function which helps mitigate loss-of-precision during summation. It tracks “lost digits” as values are added onto a running total. That can make a difference in overall accuracy so that the errors do not accumulate to the point where they affect the final total: This section explains the “0.1” example in detail, and shows how you can perform an exact analysis of cases like this yourself. Basic familiarity with binary floating-point representation is assumed. Representation error refers to the fact that some (most, actually) decimal fractions cannot be represented exactly as binary (base 2) fractions. This is the chief reason why Python (or Perl, C, C++, Java, Fortran, and many others) often won’t display the exact decimal number you expect. Why is that? 1/10 is not exactly representable as a binary fraction. Almost all machines today (November 2000) use IEEE-754 floating point arithmetic, and almost all platforms map Python floats to IEEE-754 “double precision”. 754 doubles contain 53 bits of precision, so on input the computer strives to convert 0.1 to the closest fraction it can of the form J /2** N where J is an integer containing exactly 53 bits. Rewriting 15.1 Representation Error >>> sum([0.1 ] *10 )==1.0 False >>> math.fsum([0.1 ] *10 )==1.0 True >>> x ==float.fromhex('0x1.921f9f01b866ep+1 ') True >>> x.hex() '0x1.921f9f01b866ep+1' >>> x ==3537115888337719 /1125899906842624 True