9
>>> import math
>>> math.sin(68)
-0.897927680689

But

sin(68) = 0.927 (3 decimal places)

Any ideas about why I am getting this result?
Thanks.

Archeg
  • 8,364
  • 7
  • 43
  • 90
Strigoides
  • 4,329
  • 5
  • 23
  • 25

2 Answers2

43
>>> import math
>>> print math.sin.__doc__
sin(x)

Return the sine of x (measured in radians).

math.sin expects its argument to be in radians, not degrees, so:

>>> import math
>>> print math.sin(math.radians(68))
0.927183854567
mhawke
  • 84,695
  • 9
  • 117
  • 138
  • The docstring is poorly written. It says: "Returns the sign of x, the return value is measured in radians.". – too much php Aug 06 '09 at 05:24
  • 3
    @too much php: that's a matter of interpretation. The return value is a ratio, not a measurement in any particular unit, so "(measured in radians)" clearly refers to x - the argument. – mhawke Aug 06 '09 at 05:41
  • 5
    `@too much php`: No it doesn't. It says: `'sin(x)\n\nReturn the sine of x (measured in radians).'` sine(angle) produces number (not angle) ranging from -1 to 1. All angles in everybody's trig library are measured in radians. Your interpretation is unjustifiable. – John Machin Aug 06 '09 at 06:12
  • 1
    just fyi. help(math.sin) also provides the docstring on math.sin. – monkut Aug 06 '09 at 06:30
  • @toomuchphp you're reading it wrong, and your restatement of the sentence amplifies your misunderstanding. If you just look at the substring "x (measured in radians)" it should be clear. – Mark Ransom Sep 30 '20 at 02:52
1

by default angle in Python is calculated in radians. So, you can try to multiply the angle ( degrees ) by 0.01745 - to convert it to degrees and input the values. print(math.sin(60*0.01745)) 0.8659266112878228

Deepak
  • 153
  • 1
  • 2
  • This is very inaccurate: the correct value is 0.8660254037844386, so your result is wrong from the 4th decimal on. This gets even worse with large values of the angle: `math.sin(100*360*0.01745)` returns -0.118.., while it clearly should return 0. So don't do that, use the `math.radians` function to do the conversion, as explained in the accepted answer. – Thierry Lathuille Apr 19 '21 at 18:57