1

I'm seeing many small negative numbers, below is a minimal reproducible example, i.e. -1.77635684e-15 is the small negative number produced.

In [1]: import numpy as np

In [2]: np.cumsum(np.array([11.9,  32.6, -32.6, -11.9]))
Out[2]: array([ 1.19000000e+01,  4.45000000e+01,  1.19000000e+01, -1.77635684e-15])

However, if I reorder the numbers, -1.77635684e-15 will be gone, e.g.

In [3]: np.cumsum(np.array([11.9,  32.6, -11.9, -32.6]))
Out[3]: array([11.9, 44.5, 32.6,  0. ])

I understand it likely has something to do with the floating-point arithmetic issue, but I wonder if someone can explain exactly how -1.77635684e-15 is produced in this example or with a more trivial example if one can be found?

I'm using python-3.8 and my number version is

In [4]: np.__version__
Out[4]: '1.19.1'
zyxue
  • 7,904
  • 5
  • 48
  • 74

0 Answers0