-2
Math.sin(75 * (180/3.14)); -> 0.9956310419973837

Math.sin(75 * (180/Math.PI)); -> -0.4927842805101025

The above is copied from js debugger in acrobat. I am not understanding why. The first value (.995....) is correct. Any help/explanations would be appreciated.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
  • `75*(180/3.14) - 75*(180/Math.PI)`: `2.1795938436662254` - so what do you expect? – Bergi May 13 '15 at 03:18
  • 2
    You do know that pi is not 3.14, right? – Ruan Mendes May 13 '15 at 03:18
  • 1
    Multiplying your inaccurate representation of PI by a big number will skew the results, obviously. What's "not calculating out like it should" is really 3.14. – Ja͢ck May 13 '15 at 03:19
  • @JuanMendes Math.PI value comes as 3.141592653589793 which i agree is not exactly same as 3.140000 but still the difference that is mentioned is too huge for this to matter, isnt it? Just a curious question on what you meant! – wallop May 13 '15 at 03:21
  • @wishy you're dividing a number by that difference, that's like multiplying by around 60, then you're multiplying it by 75, then you're getting the sin of that number. I'm not really not sure how you could possibly expect the result to be close. Ethan's answer explains it well – Ruan Mendes May 13 '15 at 03:26
  • I apologize for the inaccurate "representation". The answer that should come up (or at least be acceptable is .966). I'm having trouble with a formula: Math.sin((15 * 90) / 18)), which is then converted from radians to degrees. Converting from radians to degrees is 180/pi? – user2028106 May 13 '15 at 03:32

1 Answers1

2

Your problem is scale combined with precision. 75*180 is approximately 13,500, which is what you're multiplying your approximation of pi. At this scale, even small multiplicative variations will result in a large difference in the range. Combine that with the fact that you're using a crude approximation of pi (3.14) vs a more precise approximation (Math.PI) gives you a lot of variability around this point. Try both of these calculations on Wolfram Alpha, and you'll see that JavaScript is giving consistent and correct answers.

The sine function will return a result that corresponds to the y-value of the sine function. So at this scale, a very small multiplicative error (Math.PI - 3.14) will be magnified. Consider:

1 * (Math.PI - 3.14) = 0.0015926535897929917
13500 * (Math.PI - 3.14) = 21.50082346220539

(This is not a full calculation of error, just a demonstration that scale matters.)

This suggests that, around 1, the difference between 3.14 and Math.PI doesn't really move you that far (0.00159 radians or 0.09 degrees). Around 4298, it moves you 21.5 radians (or 1231.9 degrees), which gives you a completely different location on the sine wave!

Ethan Brown
  • 26,892
  • 4
  • 80
  • 92
  • Notice that the difference is not multiplied by `4298`. Since we are dividing, the error in `13500 / (π - e)` turns out to be a bit different: `13500 (1 - e) / (π - e) π` – Bergi May 13 '15 at 03:30
  • Ooops, `13500/π - 13500/(π-e)` = `-13500 e / (π - e) π` of course. To tired for maths. – Bergi May 13 '15 at 03:36
  • Thank you. Answer is useful. The problem is an error in my math, not js. Answer is .966, I'm just not sure how to get there. – user2028106 May 13 '15 at 03:53
  • Understand, user2028106. If you know the correct answer is 0.966, and what you need to do is work backwards from that, you can take the inverse sine (sin^-1 or asin) of of 0.966, and that will tell you the angle (in radians) that corresponds to that value of the sine function. asin(0.966) = 1.30928. If you want to convert that to degrees, multiply by 180 and divide by pi to get 75.02°. – Ethan Brown May 13 '15 at 16:00