Javascript precision with math libraries

I am trying to do this calculation in javascript using Big.js

r = (a * b)/ sqrt( ( a*sin(θ) )^2 + ( b*cos(θ) )^2 )

      

I also tried math.js and got the same result.

You can see the discrepancy between Wolfram and Google's calculations since Google uses javascript. Wolfram [= 40] vs Google [= 43.4008369271]

I made this jsfiddle where you can see it working:
http://jsfiddle.net/herkulano/k1h5d4zk/

How do you solve it?

+3


source to share


1 answer


The difference stems from the fact that javascript defaults to radians, while in your example Wolfram defaults to degrees. You can convert degrees to radians with a function like this.



function degToRad(deg){
return deg*(Math.PI/180)
}

      

+2


source







All Articles