Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript Math.cos shows different answer compare to scientific calculator

Tags:

javascript

i created a program in javascript that computes vector coordinates, everything was smooth since i have the correct formula, but when i try to conpute for the cosine of 143.1301 using Math.cos in javascript it returns 0.1864 instead of 0.7999 from the scientific calculator why is that? can anyone explain to me why? and also please give me the solution for this problem... thanks in advance... :) here;s a sample of my code

function cyltoxec(a)  
{
    ans = Math.cos(a);  
    return ans.toFixed(4);
}
var = x;
return cyltoxec(x);
like image 431
philip Avatar asked Aug 27 '11 15:08

philip


People also ask

Does Math cos return radians or degrees?

The Math. cos() function returns the cosine of a number in radians.

What is math Cos in Javascript?

The Math. cos() method is used to return the cosine of a number. The Math. cos() method returns a numeric value between -1 and 1, which represents the cosine of the angle given in radians.

How do you find cosine on a calculator?

Press the "Cos" button, generally found in the middle of the calculator. "Cos" is short for cosine. Your calculator should display "cos(." Enter the measure of the angle you want to know the cosine ratio of.


2 Answers

Trigonometric functions in JavaScript (and indeed, in most mathematical parlance and programming) use radians as the angular unit, not degrees.

There are 2 * Pi radians in 360 Degrees. Thus, the cosine of a degrees is

Math.cos(a * Math.PI/180)
like image 167
Adam Wright Avatar answered Nov 14 '22 21:11

Adam Wright


Math.cos expects its argument to be in radians, not degrees. Try Math.cos(a * Math.PI/180) instead.

like image 41
WReach Avatar answered Nov 14 '22 20:11

WReach