Javascript Math trigonomerial methos return wrong results. Try
alert(Math.sin(Math.PI));
it doesn't return 0.
Maybe problem is with javascript decimal number precision. Is there any workoround to get correct results?
sin() The Math. sin() function returns the sine of a number in radians.
The Math. cos() method returns the cosine of a number. The Math. cos() method returns a number between -1 and 1.
The cosine is equal to the ratio of the side adjacent to the angle and the hypotenuse of the right-angled triangle they define. The angle must be measured in radians.
The Math. cos() method is used to return the cosine of a number. The Math. cos() method returns a numeric value between -1 and 1, which represents the cosine of the angle given in radians.
It's very, very close to zero, though. (~ 10^-16)
And alert(Math.sin(Math.PI/2))
does return 1
.
It's just one of things you have to be careful of when dealing with floating point arithmetic. Rounding errors pop up all over the place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With