intel / webapps-scientific-calculator

Apache License 2.0
27 stars 33 forks source link

Floating point precision issue with sinus^-1 #18

Closed HageMaster3108 closed 9 years ago

HageMaster3108 commented 10 years ago

When I calculate sin^-1 0.5 i end up with 30.000000000000004 but it should be just exact 30

I guess this is down to floating point precision?

davidmaxwaterman commented 10 years ago

yes, I imagine you're correct.

davidmaxwaterman commented 9 years ago

Using the javascript console in chrome, I see that :

Math.asin(0.5) * 180/Math.PI 30.000000000000004

Also :

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

Note that the trigonometric functions (sin(), cos(), tan(), asin(), acos(), atan(), atan2()) expect or return angles in radians. To convert radians to degrees, divide by (Math.PI / 180), and multiply by this to convert the other way. Note a lot of the math functions have a precision that's implementation-dependent. This means that different browsers can give a different result, and even the same JS engine on a different OS or architecture can give different results.

So, I'm not sure what can be done about this apart from adding a set of special cases with known and precise answers.

davidmaxwaterman commented 9 years ago

I'm going to close this - please re-open if you have a suggestion (prefer a PR, but hey).