I am performing a calculation to obtain a value in degrees, but I have come across that the result of a JavaScript operation , involving the types of trigonometric operations, returns these values in radians.
Formula
23.45 * sin(360 * ((284 + dn)/365))
So I perform in JavaScript as follows:
var miVariable = 23.45 * Math.sin(360 * ((284 + 196)/365));
console.log(miVariable);
The returned value is 19.15 , of course, rounding the final value.
The problem is that JavaScript gives the result in radians, and I want it in degrees. I searched, and the solution they give is, multiply the result in radians as follows:
miVariable * (180/Math.PI);
...but the returned value is 1097.2141776755263
Similarly, if I multiply the result but only from the calculation of the sine operation , it falls within a possible angle range between -360 and 360 .
var valorSeno = Math.sin(360 * ((284 + 196)/365));
var valorFinal = valorSeno * (180/Math.PI);
console.log(valorFinal);
Similarly, here I still do not multiply by the value 23.45 , since it returns the same value at the end.
The final value I need is 21.51
You have to play a little with the conventions to keep all the equivalences.
You well know that the Math.sin(x) function receives its parameters as radians. The result follows in radians .
From there, apply the algebra of the school to solve the sine and give a last conversion to obtain the degrees.
You can get the result in degrees, without converting from radians to degrees. Instead, I decided to make a small change to the mathematical formula.
The original formula is this:
I just changed the
360
to double pi (2*Math.PI
). Apparently, 180º is the equivalence of pi.I have programmed the function so that it adapts to the number of days in the year, that is, if it is a leap year or not.
I think the formula to convert this is wrong and it should be like this:
I assure you that this formula works:
var grados = radianes * (180 / Math.PI);