tkbx tkbx - 4 months ago 61
Python Question

Python: converting radians to degrees

In the math library, I could only find

, with cos/sin/tan/acos/asin/atan. This returns the answer in radians. How can I get the answer in degrees?

Here's my code:

import math

x = math.cos(1)
y = x * 180 / math.pi
print y

My calculator, on deg, gives me:



Python includes two functions in the math package; radians converts degrees to radians, and degrees converts radians to degrees.

To match the output of your calculator you need:

>>> math.cos(math.radians(1))

Note that all of the trig functions convert between an angle and the ratio of two sides of a triangle. cos, sin, and tan take an angle in radians as input and return the ratio; acos, asin, and atan take a ratio as input and return an angle in radians. You only convert the angles, never the ratios.