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?

user229044
  • 232,980
  • 40
  • 330
  • 338
quantumpotato
  • 9,637
  • 14
  • 70
  • 146

1 Answers1

1

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.

Paul
  • 139,544
  • 27
  • 275
  • 264