I tried to calculate the sine of 30 degrees, just for testing and JS returns 0.49999999999999994. The correct answer is 0.5. Can someone explain why sometimes the calculation in JS going in such way?
Asked
Active
Viewed 36 times
0
-
2It depends on how PI is defined. You are just seeing the result of the accuracy of the numbers on computers. PI has an infinite number of digits, but computer processors do not :) – iCollect.it Ltd Jan 05 '16 at 11:29
-
So i must always use Math.round or something like that to correct such numbers? – Ashe23 Jan 05 '16 at 11:40
-
2No need to round. Just learn to live with the inaccuracies. – Daniel Darabos Jan 05 '16 at 12:16