I am trying to get sin and cos of an angle that is 9.590124942537262.
In Javascript I would do it like that:
var a = Math.sin(9.590124942537262);
var b = Math.cos(9.590124942537262);
The results would be:
a == -0.16459459028073223
b == -0.9863613034027227
If I do the same operation in PHP like this:
$a = sin(9.590124942537262 * M_PI / 180); // ≈ 0.16737916966136 rad
$b = cos(9.590124942537262 * M_PI / 180);
I get these results:
$a == 0.16659953926461
$b == 0.9860246414349
Question is: how do I make these operations uniform, so that PHP returns correct value with a minus sign? I tried rounding my angle but that didn't help.
Both PHP and JavaScript's trigonometry functions work in radians
In your PHP example, you are trying to convert from degrees to radians, because you're actually supplying radians to be converted into radians, you're just adding Pi radians, or 180 degrees, hence why you get the negative result.
You should be able to do the code in each language with the same parameter:
Math.sin(9.590124942537262);
Math.cos(9.590124942537262);
sin(9.590124942537262);
cos(9.590124942537262);
Related
I have equation to find angles of triangle, after the calculation the i get to convert the end results to inverse of Cos function to obtain the angle.I can get the results on a scientific calculator when i input the values, all the steps have same values until the inverse cos function is called. Here are my values. I use the cos formula to calculate the angles of triangle.
calculate(60,100,80)
calculateAngle( a,b,c){
var cosC = (this.getSquare(a)+this.getSquare(b)-this.getSquare(c))/(2*a*b)
console.log(a,b,c,cosC,Math.acos(this.Todegrees(cosC)))
return Math.acos(this.Todegrees(cosC))
}
getSquare(num){
return num*num
}
Todegrees(degrees){
var pi = Math.PI;
return degrees * (180/pi);
}
The cosC is 0.6 and the the angle result should be 53 degrees. But the angle returned from Math.acos seems not correct.Is my degree to radians function wrong?
You are applying the conversion to degrees on the wrong side. acos takes a ratio of side lengths or areas and returns an angle in radians. This result you then convert into degrees
return toDegrees(Math.acos(cosC));
Here is the excel function and it works fine in excel.
lng = mod(lng1-asin(sin(tc1)*sin(d)/cos(lat))+pi,2*pi)-pi
I am trying to convert it to JavaScript, I have this so far but it is not working.
x = lng1-Math.asin(Math.sin(tc1)*Math.sin(d));
y = Math.asin(Math.cos(lat)+Math.PI,2*Math.PI);
n = (x % y)-Math.PI ;
I am passing the coordinates in radians (and verified they are correct as in the example below) but just can't figure out why its giving me NaN
TIA for the help
Steve
This is the worked example ( from here https://edwilliams.org/avform.htm#Intermediate)
= mod(2.066470- asin(sin(1.150035)*sin(0.0290888)/cos(0.604180))+pi,2*pi)-pi
= mod(2.034206+pi,2*pi)-pi radians
= 2.034206 radians
= 116 degrees 33min```
There's some minor conversion mistakes you've made in your conversion. In order to allow the code to correctly work, below is my solution:
const result = lng1 - Math.asin((Math.sin(tc1) * Math.sin(d)) / Math.cos(lat)) + Math.PI % (2 * Math.PI) - Math.PI;
The solution may look a bit long, but it functions just as normal and completes the operation in the exact order.
Given that:
let lng1 = 2.06647;
let tc1 = 1.150035;
let d = 0.0290888;
let lat = 0.60418;
The code will execute and the final constant named result will have a value of 2.0342057088546213, or around 2.034206, in radians. If there is any need of conversions to degrees, simply multiply the result by a factor of 57.295779513082 since 1 radian is equal to 57.295779513082 degrees.
I'm working on a project to solve triangles, but I cant seem to figure out how to get the inverse of sine, I've already set up switching from radians to degrees in my program I just need inverse operators.
Simply use Math.asin:
Math.asin(opposite / hypotenuse);
Math.sin(x) takes a radian and outputs a range [-1 , 1].
Math.asin(x) takes a range [-1 , 1] and outputs a radian value.
To convert these radian values, use these two functions:
degreeToRadian = d => d * Math.PI * 180 ** -1
radianToDegree = r => r * 180 * Math.PI ** -1
For example:
Math.sin(degreeToRadian(30)).toFixed(3) * 1
results -> 0.5
or:
radianToDegree(Math.asin(0.5)).toFixed(3) * 1
results -> 30
So I'm making a small game using the canvas API and and I'm fairly new to Javascript. But while I was working on making the charecter be able to shoot in a full 360 degrees I came across a error in the code below (this function should return the angle between the mouse position and player position nothing else)
calculateAngle : function(x,y){
var opp = 0; //opposite
var adj = 0; //adjecent
var rad = 0; //radian
var ang = 0; //angle
//side lengths
var x1 = gfx.player_center_x //the player x position
var x2 = mouse_x; //the mouse x position
var y1 = gfx.player_center_y; //the player y position
var y2 = mouse_y; //the mouse y position
//find 2 lengths of the triangle
opp = (y2 - y1);
adj = (x2 - x1);
//find the missing angle between the adjecent and hypotenuse
rad = Math.atan2(adj, opp);
ang = rad * 180 / Math.PI;
//-------------------------//
console.log(ang); //prints: NaN
console.log(typeof ang); //prints: number
//------------------------//
return ang;
}
When executed its returns NaN but ang is a number!
Why does javascript think the variable ang not a number? Its declared as 0 which typeof returns number, please help!
UPDATE: Careless error made, x1 and y1 ect changed location and I forgot to implement it, thanks for you answers though
NaN is a special value. Internally it's a number, but it's a specific value that's given the special meaning of "this value is mathematically undefined". Any operation that results in an undefined number (such as 0/0) results in NaN, but it's still a number! Just not a defined number.
Math.atan2 is implemented in such a way that it cannot return NaN (because it handles the "edge cases" where y/x would be Infinity - another "number" that isn't a defined number) so it seems almost certain that your NaN is coming from the calculation of opp and/or adj. You should log these values and see what's up.
NaN is a IEEE 754 “Not-a-Number” value. In javascript it belongs to the number type even it is known as NaN(Not a Number). It's a quirks of javascript. Even according to
IEEE Standard for Floating-Point Arithmetic (IEEE 754):
arithmetic formats: sets of binary and decimal floating-point data, which consist of finite numbers (including signed zeros and subnormal numbers), infinities, and special "not a number" values (NaNs).
To start I'm not that well versed in javascript and I'm trying to convert this complex excel formula to javascript without any luck.
=DEGREES(ASIN(SIN(RADIANS(59.036))*SIN(RADIANS(150))))/2
Here's what I have so far
var x = DEGREES(Math.asin(Math.sin(RADIANS(59.036))*Math.sin(RADIANS(150))))/2
Obviously, DEGREES and RADIANS are wrong and I can't figure what the javascript equivalent would be.
(BTW the correct answer is 5.831)
Well, you could write your own DEGREES and RADIANS functions, which only involve a little math:
function degrees(x) { return x * 180 / Math.PI; }
function radians(x) { return x * Math.PI / 180; }
var x = degrees(Math.asin(Math.sin(radians(59.036))*Math.sin(radians(150))))/2;
Define both functions yourself. They are both trivial to implement.
function DEGREES(radians){
return (radians * 180 / Math.PI);
}
function RADIANS(degrees){
return (degrees / 180 * Math.PI);
}