Math.cos(90 * Math.PI / 180)为什么产生6.123031769111 ...而不是零? [英] Why does Math.cos(90 * Math.PI/180) yield 6.123031769111... and not zero?

查看:1166
本文介绍了Math.cos(90 * Math.PI / 180)为什么产生6.123031769111 ...而不是零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将度数转换为弧度(degree * Math.PI / 180),但为什么会出现下列情况:

  Math.cos (90 * Math.PI / 180)

产量6.123031769111 ...而不是零?



我试图使用矩阵来执行二维旋转,结果完全没有成功。

解决方案

  Math.cos(90 * Math.PI / 180)



  6.123031769111886 e-17 

注意 e-17 最后,这意味着这个数字是6.123 x 10 -17 。这是一个非常接近0的数字,它实际上是0.它不是0的原因是由于IEEE-754双格式中的四舍五入错误,导致你无法精确地表达出π / 2,并在余弦的计算中产生细微的舍入误差。顺便说一句 - 当结果以6开始时,我感到非常惊讶!只有当我看到事情开始变得有意义了。



希望这有助于您!


I convert degrees to radians (degrees * Math.PI/180) but why does the following:

Math.cos(90 * Math.PI/180)

yield 6.123031769111... and not zero?

I'm trying to perform 2D rotations uses matrixes and the results are completely out of whack.

解决方案

The output of

Math.cos(90 * Math.PI/180)

is

6.123031769111886e-17

Notice the e-17 at the end, which means that this number is 6.123 x 10-17. This is a number so vanishingly close to 0 that it's effectively 0. The reason that it's not identically 0 is due to rounding errors in the IEEE-754 double format which prevents you from getting an exact representation of π / 2 and causes minute rounding errors in the calculation of the cosine.

By the way - I was pretty surprised as well when the result came back starting with a 6! It's only after I looked at the very end that things started to make sense.

Hope this helps!

这篇关于Math.cos(90 * Math.PI / 180)为什么产生6.123031769111 ...而不是零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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