numpy.sin(pi) 返回负值 [英] numpy.sin(pi) returns negative value

查看:61
本文介绍了numpy.sin(pi) 返回负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

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

返回真".但实际上 a = 0.我该如何解决?另外,我有一个矩阵有很多像a"这样的值,我想确保我的矩阵包含非负值.

return "True". But in reality a = 0. How could I fix that? In addition, I have a matrix with a lot of value like "a" and I want to make sure that my matrix contains non-negative value.

推荐答案

这是因为浮点运算和精度的原因.结果是 sin 的最佳近似值,可以表示为浮点数.通常你解决的问题几乎为零:

This because of floating point arithmetic, and accuracy reasons. The result is the best approximation of sin one can get to be representable as a floating point number. Usually you solve near-zero problems like this:

a = numpy.sin(2. * numpy.pi)
print(abs(a) < 1e-10)

您可能还想阅读这篇.

这篇关于numpy.sin(pi) 返回负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆