2

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?

Edward
  • 1,062
  • 1
  • 17
  • 39
Ripspace
  • 551
  • 6
  • 21
  • 4
    Your calculator is probably using degrees (35 degrees). Change it to use radiants and it will behave the same as the Python math module. Mathematecians prefer radiants and not the (arbitrary 1/360 of the cycle) degree. – ypercubeᵀᴹ Jan 01 '12 at 04:14
  • 2
    I'll give you +1 though as you find this behaviour funky. Math can be cool :) – ypercubeᵀᴹ Jan 01 '12 at 04:19
  • 4
    @ypercube: [It's explicit in the documentation](http://docs.python.org/library/math.html#trigonometric-functions) that all of the trigonometric methods are in radians, though. – Makoto Jan 01 '12 at 04:25
  • 1
    @Makoto: did I say otherwise? – ypercubeᵀᴹ Jan 01 '12 at 04:27
  • 3
    Real mathematicians will laugh derisively at those who use degrees in trigonometry :-) – paxdiablo Jan 01 '12 at 05:04
  • 1
    It's worth noting that as in radians a complete circle is 2 * PI (a little over 6), any angle measure greater than 10 is almost certainly in degrees. Furthermore, almost all measures in radians can't be written out exactly as numbers, so any angle measure that's a nice whole number is almost certainly in degrees. Angle measures that are small and written as "not nice" fractions, might be radians. Angle measures written as multiples of PI (e.g. PI / 4) are almost certainly in radians. – Ben Jan 01 '12 at 05:43
  • This happens due to the fact that your calculator is in degree mode where are python uses radian values. – Shawn Janas Jan 01 '12 at 04:15

3 Answers3

9

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.

Edward
  • 1,062
  • 1
  • 17
  • 39
nye17
  • 12,857
  • 11
  • 58
  • 68
1

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.

surj
  • 4,706
  • 2
  • 25
  • 34
  • 2
    There's no angle value being returned from anything that could be measured in degrees or radians. – David Z Jan 01 '12 at 05:29
0
cos(35)[degree] = 0.819 * 15 + 9 = 21.285  <-- Your calculator

sin(35)[radian] = -0.428 * 15 + 9 = 2.577  <-- Python
Steve V.
  • 463
  • 4
  • 14