Unusual Math with incorrect results? [duplicate]

十年热恋 提交于 2019-12-17 16:53:11

问题


My python interpreter is acting funky when I use the math.cos() and math.sin() function. For example, if I do this on my calculator:

cos(35)*15+9 = 21.28728066
sin(35)*15+9 = 17.60364655

But when I do this on python (both 3.2 and 2.7)

>>> import math
>>> math.cos(35)*15+9
-4.5553830763726015

>>> import math
>>> math.sin(35)*15+9
2.577259957557734

Why does this happen?

EDIT: How do you change the Radian in Python to degrees, just in case?


回答1:


This is being caused by the fact that you are using degrees
and the trigonometric functions expect radians as input:
sin(radians)

The description for sin is:

sin(x)
    Return the sine of x (measured in radians).

In Python, you can convert degrees to radians with the math.radians function.


So if you do this with your input:

>>> math.sin(math.radians(35)) * 15 + 9
17.60364654526569

it gives the same result as your calculator.




回答2:


The following Python methods can be used to convert radians to degrees or vice versa:

math.degrees
math.radians

Using your example:

>>> from math import cos, sin, radians
>>> sin(radians(35)) * 15 + 9
17.60364654526569
>>> cos(radians(35)) * 15 + 9
21.28728066433488

Weird calculator... takes degrees but returns radians.




回答3:


cos(35)[degree] = 0.819 * 15 + 9 = 21.285  <-- Your calculator

sin(35)[radian] = -0.428 * 15 + 9 = 2.577  <-- Python


来源:https://stackoverflow.com/questions/8691800/unusual-math-with-incorrect-results

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