

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
You should show more code so we can see some context. – Gabe Mar 11 '12 at 5:24
@Gabe- Actually, the above code is sufficient. Try typing it into a JS console and you will indeed get the result shown above. – templatetypedef Mar 11 '12 at 5:27

The output of

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



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!

share|improve this answer
and the fact that Math.PI != π. – Greg Hewgill Mar 11 '12 at 5:26
+1. Maybe add something like Math.cos(90 * Math.PI/180).toFixed(15) as a simple solution? – Dagg Nabbit Mar 11 '12 at 6:02
( Math.cos( 90 * Math.PI / 180 ) | 0 ) === 0 – JCM Feb 23 '13 at 20:21

is scientific notation for a very small number, close to zero. It is not exactly zero because of rounding errors and so forth.

share|improve this answer

6.123233995736766e-17 is very very close to zero. Just round the number.

share|improve this answer

Your Answer


