Javascript Math.cos和Math.sin不准确。有什么解决方案吗? [英] Javascript Math.cos and Math.sin are inaccurate. Is there any solution?

查看:139
本文介绍了Javascript Math.cos和Math.sin不准确。有什么解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Javascript Math trigonomerial methos返回错误的结果。
尝试

Javascript Math trigonomerial methos return wrong results. Try

alert(Math.sin(Math.PI));

它不会返回0.

也许问题是javascript十进制数精度。是否有任何工作量来获得正确的结果?

Maybe problem is with javascript decimal number precision. Is there any workoround to get correct results?

推荐答案

尽管如此,它非常接近于零。 (~10 ^ -16)

It's very, very close to zero, though. (~ 10^-16)

alert(Math.sin(Math.PI / 2))返回 1

这只是处理浮点运算时必须注意的事项之一。舍入错误会在整个地方弹出。

It's just one of things you have to be careful of when dealing with floating point arithmetic. Rounding errors pop up all over the place.

这篇关于Javascript Math.cos和Math.sin不准确。有什么解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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