問題描述
我將度數轉換為弧度(度數 * Math.PI/180),但為什么會這樣:
I convert degrees to radians (degrees * Math.PI/180) but why does the following:
Math.cos(90 * Math.PI/180)
產量 6.123031769111... 而不是零?
yield 6.123031769111... and not zero?
我正在嘗試使用矩陣執行 2D 旋轉,結果完全不正常.
I'm trying to perform 2D rotations uses matrixes and the results are completely out of whack.
推薦答案
輸出
Math.cos(90 * Math.PI/180)
是
6.123031769111886e-17
注意末尾的e-17
,表示這個數字是6.123 x 10-17.這是一個非常接近 0 的數字,實際上它是 0.它不完全是 0 的原因是由于 IEEE-754 雙精度格式中的舍入錯誤,這會阻止您獲得 π 的精確表示./2 并在余弦計算中導致微小的舍入誤差.
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.
順便說一句 - 當結果以 6 開頭時,我也很驚訝!直到我看到最后才開始明白事情的意義.
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.
希望這會有所幫助!
這篇關于為什么 Math.cos(90 * Math.PI/180) 產生 6.123031769111... 而不是零?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!