最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript Math.cos shows different answer compare to scientific calculator - Stack Overflow

programmeradmin1浏览0评论

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);

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);
Share Improve this question edited Aug 27, 2011 at 15:50 Mahesh 34.6k21 gold badges92 silver badges116 bronze badges asked Aug 27, 2011 at 15:48 philipphilip 1,3023 gold badges24 silver badges44 bronze badges 2
  • 2 Welcome to Stackoverflow. Please use {} present on the editor window to format your code. You can always have a preview of what it looks like down under the editor window even before posting. – Mahesh Commented Aug 27, 2011 at 15:51
  • 1 0.1864 is in radians, 0.8 is in degrees. – user555045 Commented Aug 27, 2011 at 15:52
Add a comment  | 

2 Answers 2

Reset to default 17

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)

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

发布评论

评论列表(0)

  1. 暂无评论