numpy.sin(pi) returns negative value

后端 未结 4 741
面向向阳花
面向向阳花 2020-12-19 20:51

The following code:

a = numpy.sin(2. * numpy.pi)
print(a < 0)

return \"True\". But in reality a = 0. How could I fix that? In addition,

4条回答
  •  盖世英雄少女心
    2020-12-19 21:26

    In reality a <> 0 because in reality numpy.pi is not Pi (what is Pi in reality anyway?) - it is just its approximation and numpy.sin is not sine - it is its approximation as well. So you have to take some error into account, for example

    print( -0.0000001 < a < 0.0000001 )
    

    or use some other tricks (representing Pi differently - not as a float number ).

提交回复
热议问题