How does trig work in JavaScript? [closed]

你说的曾经没有我的故事 提交于 2019-12-14 03:36:05

问题


I have a value and its cosine as:

-0.2567721374914637
Cosine: -0.04253775592822526

Compare what we get in Python:

>>> import math
>>> math.cos(-0.2567721374914637)
0.9672147629178562

Is there a way I can do trig in JavaScript that isn't out to lunch?

Are there any math libraries or anything that provide working trigonometry?

--EDIT--

The code where I was getting the results reported were:

            var distance_along = (high_noon.getTime() / 1000 -
              summer_solstice.getTime() / 1000)
              / (winter_solstice.getTime() / 1000
              - summer_solstice.getTime() / 1000);
            console.log(distance_along);
            console.log('Cosine: ' + Math.cos(2 * Math.PI * distance_along));
            var added_degrees = (Math.cos(2 * Math.PI * distance_along) * 
              ANCIENT_CLOCK.earth_tilt);

That reported, for a distance along of

-0.2567721374914637
Cosine: -0.04253775592822526 

Regarding the question of looking things up: I did not do extensive looking up of how to handle a cosine. I knew that JavaScript had a Math library, and thought the logical place to look for it was in Math.cos(). However, it reported the values above.


回答1:


You can do this javascript like this:

//Return the cosine of a number:
Math.cos(3);
// -0.9899924966004454

You can get the full list of Math objects here: JavaScript Math Object




回答2:


You have the Math object:

Math.cos(-0.25)
0.9689124217106447

You can find all functions available in Math here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math



来源:https://stackoverflow.com/questions/16387316/how-does-trig-work-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!