Unusual math with wrong results?

My python interpreter is funky when I use the math.cos () and math.sin () functions. 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 it in 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 is this happening?

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

+2
python math trigonometry sin cos
source share
3 answers

This is due to the fact that you are using degrees.
and trigonometric functions expect radians as input:
sin(radians)

Description of sin :

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

In Python, you can convert degrees to radians using 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.

+9
source share

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 

A strange calculator ... takes degrees, but returns radians.

+1
source share
 cos(35)[degree] = 0.819 * 15 + 9 = 21.285 <-- Your calculator sin(35)[radian] = -0.428 * 15 + 9 = 2.577 <-- Python 
0
source share

All Articles