November 27, 2016

Floating point arithmetic issues in Python 3

By default, numbers having a decimal point are represented as floating point numbers.


<class 'float'>

Floating point numbers are represented in computer hardware in base 2. Floating point numbers are used to represent decimal (base 10) numbers in the most common use cases. But most decimal fractions cannot be represented exactly as binary fractions.

To understand this problem, consider the conversion of a decimal fraction to a decimal number: 1/3 is equal to 0.333... with repeating 3’s. When represented as a decimal number, it will never be equal to 1/3.

Similarly, many decimal numbers cannot be represented accurately in base 2.

Some examples:

print("0.1 + 0.2 = {}".format(0.1+0.2))
0.1 + 0.2 = 0.30000000000000004

print("0.1 + 0.4 = {}".format(0.1+0.4))
0.1 + 0.4 = 0.5

print("1/10 = {}".format(1/10))
1/10 = 0.1

print("Is (0.1 + 0.1 + 0.1 == 0.3) ?")
print(0.1 + 0.1 + 0.1 == 0.3)
Is (0.1 + 0.1 + 0.1 == 0.3) ?

In the above examples, notice that some decimal numbers can be represented accurately in base 2 while others cannot be represented accurately.

I won’t pretend that I understand the intricate details behind why this happens, so I will link to some resources if you are interested to learn more: The Perils of Floating Point and What Every Computer Scientist Should Know About Floating-Point Arithmetic.

How can we overcome this issue in Python? Some solutions are:

  • using round() function
  • using the decimal module
  • using the fractions module

Let me explain each of these solutions a little more:

Using round(), floating point numbers can be rounded to a specified number of decimal places. If it’s used for post rounding a result (round after performing all operations on floats), working with floats will work as expected. Example:

print("Is (0.1 + 0.1 + 0.1 == 0.3) ?")
print(round(0.1 + 0.1 + 0.1, 5) == round(0.3, 5))
Is (0.1 + 0.1 + 0.1 == 0.3) ?

The round() function accepts a number as the first parameter and another number to specify the precision after the decimal point as the second parameter. Example:

print(round(10.33333, 3))

Edit: It has been pointed out to me that using round() may not be the best solution in certain cases where the rounded number will be used in calculations further in the program. Using round() in such cases will lead to accuracy issues. An option is to use string formatting to display the required number of digits since this will not round the numbers. Syntax is as follows:

print('{: .3f}'.format(10.34123))

Another alternative is to use the decimal module when dealing with decimal numbers and accuracy is very important. Example:

from decimal import getcontext, Decimal
from math import pi


getcontext().prec = 5

getcontext().prec = 30

Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=
[InvalidOperation, DivisionByZero, Overflow])

getcontext() allows us to specify the precision and the rounding technique to be used, The default rounding technique is ROUND_HALF_EVEN which rounds to nearest with ties going to nearest even integer.

To deal directly with fractions, the fractions module can be used which supports rational number arithmetic. Example:

from fractions import Fraction

num1 = Fraction(2,3)
num2 = Fraction(1,3)

print("num1 = {} and num2 = {}".format(num1,num2))

print(num1 + num2)

print(num1 - num2)



num1 = 2/3 and num2 = 1/3
<class 'fractions.Fraction'>

There are multiple ways to construct fractions and the details can be found in the official documentation which is linked below.

Source code for today’s plog is here.



© Plogging Dev - Powered by Hugo Theme by Kiss