Python – Cancel in numpy array operations that include scalars

Cancel in numpy array operations that include scalars… here is a solution to the problem.

Cancel in numpy array operations that include scalars

I’m using NumPy version 1.7.1.
Now I’ve come across a strange cancellation that I don’t understand :

>>> import numpy as np
>>> a = np.array([ 883,  931,  874], dtype=np.float32)

Mathematically, a+0.1-a should be 0.1.
Now let’s calculate the value
This expression along with absolute and relative errors:

>>> a+0.1-a
array([ 0.09997559,  0.09997559,  0.09997559], dtype=float32)
>>> (a+0.1-a)-0.1
array([ -2.44155526e-05,  -2.44155526e-05,  -2.44155526e-05], dtype=float32)
>>> ((a+0.1-a)-0.1) / 0.1
array([-0.00024416, -0.00024416, -0.00024416], dtype=float32)

First question: this is a fairly high absolute and relative error, and this is just a catastrophic cancellation, isn’t it?

Second question: NumPy is able to calculate more precisely when I use arrays instead of scalars, see relative error:

>>> a+np.array((0.1,)*3)-a
array([ 0.1,  0.1,  0.1])
>>> (a+np.array((0.1,)*3)-a)-0.1
array([  2.27318164e-14,   2.27318164e-14,   2.27318164e-14])

I’m guessing it’s just a numerical representation of 0.1.

But if using scalars instead of arrays like in a+0.1-a, why can’t NumPy handle this in the same way?

Solution

If you use double, things change. What you get is the expected result of single precision (np.float32):

a = np.array([ 883,  931,  874], dtype=np.float64)

a+0.1-a
# array([ 0.1,  0.1,  0.1])

((a+0.1-a)-0.1) / 0.1
# array([  2.27318164e-13,   2.27318164e-13,   2.27318164e-13])

Using np.array((0.1,)*3) in the middle of the expression to convert everything to float64 shows that the second result is more precise.

Related Problems and Solutions