Precision of numpy's diff()

Viewed 29

I find that numpy.diff gives an inaccurate result, which can be shown by the following code snippet.

import numpy as np

# test data
arr_a = np.linspace(10**8,10**9,500,dtype=np.double)
arr_b = np.linspace(10**9,10**10,500,dtype=np.double)
arr_c = np.linspace(10**10,10**11,500,dtype=np.double)

# results
plt.figure(figsize=(15,5))

plt.plot( np.diff(arr_c,n=2), "r", linewidth=1.0,label=r"arr_c")
plt.plot( np.diff(arr_b,n=2), "g", linewidth=2.0,label=r"arr_b" )
plt.plot( np.diff(arr_a,n=2), "b", linewidth=2.0,label=r"arr_a" )
plt.legend()

The output is shown below enter image description here From this figure, we see that np.diff(arr_b,n=2) and np.diff(arr_c,n=2) are very noisy, which is far from the theoretical value (i.e., zero). Why does this happen?

0 Answers
Related