The cosine of 90 degrees is 0.
I understand Javascript's Math.cos
takes radians, so I've tried:
Math.cos(90 * Math.PI / 180)
Why does this yield 6.123233995736766e-17
instead of 0?
The cosine of 90 degrees is 0.
I understand Javascript's Math.cos
takes radians, so I've tried:
Math.cos(90 * Math.PI / 180)
Why does this yield 6.123233995736766e-17
instead of 0?
6.123233995736766e-17
is essentially 0
. It's 0.00000000000000006123233995736766
. That kind of minor error is completely normal when working with IEEE floating point numbers.
The solution is to never compare numbers exactly, but compare if they are within a range that you expect. EG.
var result = Math.cos(90 * Math.PI / 180);
if ( Math.abs( result - 0 ) < 1e-16 ) {
// Test passed, result is effectively 0
}
The - 0
does nothing, but more generally - x
is how you would compare to x
. You are trying to compare to 0
, so I used - 0
, but you could leave that off and get the same result.