用数学模块计算sin、cos、tan?

Calculate the sin, cos, tan with math module?

我的问题是我在 python 上的计算结果与我的计算器不同:

度数模式计算器上的计算:

cos(270)

这给了我:

0

当我在 python 中进行相同的计算时:

print(math.cos(math.radians(270)))

我得到了完全不同的东西:

-1.8369701987210297e-16

我想这是我犯的一个非常简单的错误,请帮助我....

就像评论中描述的那样,这只是一个舍入误差。 -1.8369701987210297e-16 非常接近 0,可以这样看。

如果您想要一个数字,例如您的预期行为,请使用 round(value, decimalplaces) 将结果四舍五入。让我们四舍五入到小数点后两位:

>>> print('cos(270) is', round(math.cos(math.radians(270)), 2))
cos(270) is -0.0