[Tutor] trigonometry in python

Yigal Duppen yduppen@xs4all.nl
Fri Nov 1 07:23:01 2002


> when i import math module to count the value of for example sin(30), why
the value is not 0.5 ? i used this syntax :
> the result value is not 0.5, what happened ? anyone can help me ?

Hi,

There are two reasons you get a different value than you expected.
The first is that there are two(*) ways to look at trig. You can count
angles in degrees and you can count them in radians.

The trig functions in math work in radians, not in degrees. So the correct
approach is to first convert your angle in degrees to radians.

>>> def toRadians(degrees):
>>>     return degrees * ((2 * math.pi) / 360)
...
>>> math.sin(toRadians(30))
0.49999999999999994

Much better!

However, we're still not getting 0.5
This is where the second reason comes into play. As you might know, there is
an infinite amount of real numbers. However, due to computer constraints
there is only a limited amount of real numbers that can be represented on a
computer. Therefore, Python often gives you an /approximation/ of the
correct value.

I hope this helps.

YDD
--
http://yduppen.xs4all.nl