python中的精确trig

2024-10-03 19:21:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个计算步长的函数。在

这就是我所拥有的:

def humanstep(angle):
    global human
    human[1]+=math.sin(math.radians(angle))*0.06
    human[2]+=math.cos(math.radians(angle))*0.06

所以如果角度是90,那么x值(human[1])应该等于0.06,y值等于0。在

相反,弧度和度数之间的转换不是很完美,这些值会被返回。在

^{pr2}$

有什么办法解决这个问题吗?在


Tags: 函数defmathsincosglobal角度human
3条回答

这是由于浮点运算的工作原理造成的表示错误。请参见Python文档中的下一页:Floating Point Arithmetic: Issues and Limitations。在

自由贸易协定:

Note that this is in the very nature of binary floating-point: this is not a bug in Python, and it is not a bug in your code either. You’ll see the same kind of thing in all languages that support your hardware’s floating-point arithmetic (although some languages may not display the difference by default, or in all output modes).

有关进一步阅读,请参阅以下页面:

如果你想得到准确的结果,那么你做的是正确的。在

如果您想要数学上精确的结果,比如[6, 0],那么使用符号数学库,比如sympy

请注意,这些是非常不同的目标。在

你到底想要多精确?以上精度为15dp。在

相关问题 更多 >