SoFunction
Updated on 2025-03-01

A brief discussion on the problem of zero value generated by numpy array

Today, when I was writing the sinc function using numpy, I accidentally found that the function could actually get 1 when x=0. It was incredible. Logically, the function was meaningless when x=0. I studied it and found that numpy automatically replaced 0 with a very small number when generating an array.

In[2]: import numpy as np
In[3]: (-1, 1, 0.1)
Out[3]: 
array([ -1.00000000e+00, -9.00000000e-01, -8.00000000e-01,
    -7.00000000e-01, -6.00000000e-01, -5.00000000e-01,
    -4.00000000e-01, -3.00000000e-01, -2.00000000e-01,
    -1.00000000e-01, -2.22044605e-16,  1.00000000e-01,
     2.00000000e-01,  3.00000000e-01,  4.00000000e-01,
     5.00000000e-01,  6.00000000e-01,  7.00000000e-01,
     8.00000000e-01,  9.00000000e-01])
In[4]: (-1, 0.9, 20)
Out[4]: 
array([ -1.00000000e+00, -9.00000000e-01, -8.00000000e-01,
    -7.00000000e-01, -6.00000000e-01, -5.00000000e-01,
    -4.00000000e-01, -3.00000000e-01, -2.00000000e-01,
    -1.00000000e-01, -1.11022302e-16,  1.00000000e-01,
     2.00000000e-01,  3.00000000e-01,  4.00000000e-01,
     5.00000000e-01,  6.00000000e-01,  7.00000000e-01,
     8.00000000e-01,  9.00000000e-01])

The two functions aparte and linspace are replaced by a very small number of e-16 where they should be 0.

The above article briefly discusses the zero value issue of numpy generating arrays is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.