10

When I use Math.sin(90) for calculating Sine of 90 degrees in javascript it returns 0.8939966636005565, but sin(90) is 1. Is there a way to fix that? I need accurate values for any angle.

<!DOCTYPE html>
<html>
<body>
    <p id="demo">Click the button calculate value of 90 degrees.</p>
    <button onclick="myFunction()">Try it</button>
<script>
function myFunction(){
    document.getElementById("demo").innerHTML=Math.sin(90);
}
</script>

JaSamSale
  • 169
  • 1
  • 2
  • 11

2 Answers2

15

Math.sin expects the input to be in radian, but you are expecting the result of 90 degree. Convert it to radian, like this

console.log(Math.sin(90 * Math.PI / 180.0));
# 1

As per the wikipedia's Radian to Degree conversion formula,

Angle in Radian = Angle in Degree * Math.PI / 180
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • 3
    How come `console.log(Math.sin(180 * Math.PI / 180));` returns `1.2246467991473532e-16` instead of `0`? – nils Jul 28 '15 at 08:20
  • 3
    @nils - Notice that the number `1.2246467991473532e-16` is very very small, and It is equivalent to `0.00000000000000012246467991473532` (notice the `e-16`) at the end of the number, which is a scientific form of writing a number. – Greeso Nov 27 '17 at 21:20
6

The sin function in Javascript takes radians, not degrees. You need to convert 90 to radians to get the correct answer:

Math.sin(90 * (Math.PI / 180))
sjf
  • 785
  • 1
  • 8
  • 19