Python cos(90) and cos(270) not 0

我的未来我决定 提交于 2019-11-28 12:34:44

Indeed, it is a very low number, awfully close to zero. Here's a great article which can help you understand the common challenges and pitfalls of floats: "What Every Computer Scientist Should Know About Floating-Point Arithmetic"

You are dealing with rounding errors, which are (mostly) unavoidable when working with floating point math (see the paper already linked by others to understand exactly what's going on).

In many cases you can reduce their impact (by performing the operations in "intelligent" order or reformulating your expressions in a more "floating point-friendly" way), but the simplest thing to do in your situation is to simply the results to e.g. 6 decimal places and be happy with it. Surely you won't need more precision in positioning, and you'll get the results you expect for the "canonical" angles.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!