Math.cos不准确 [英] Math.cos inaccurancy

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

问题描述

(Math.cos(Math.PI / 2));



为什么结果不是确切的零?这是不正确的,或者一些实现的错误?

解决方案

Math.PI / 2 pi / 2 的实际值的近似值。取这个近似值的精确余弦不会产生零。你得到的值是这个确切值的近似值,直到基础浮点数据类型的精度为止。使用一些任意的精度库,你可以计算出它们之间的差异。 code> pi / 2 以双精度计算,精确值为

  0.0000000000000000612323399573676588613032966137500529104874722961 .. 。

由于余弦接近于零的斜率为1,所以期望余弦近似值 pi / 2 近似等于这个差值,实际上是这样。


alert(Math.cos(Math.PI/2));

Why the result is not exact zero? Is this inaccurancy, or some implementation error?

解决方案

Math.PI/2 is an approximation of the real value of pi/2. Taking the exact cosine of this approximated value won't yield zero. The value you get is an approximation of this exact value up to the precision of the underlying floating point datatype.

Using some arbitrary precision library, you can evaluate the difference between pi/2 in double precision and the exact value to

 0.0000000000000000612323399573676588613032966137500529104874722961...

Since the slope of the cosine close to its zeros is 1, you would expect the cosine of the approximation of pi/2 to be approximately equal to this difference, and indeed it is.

这篇关于Math.cos不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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