Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert radians to degrees with Python?

Tags:

python

math

In the math module, I could only find math.cos(x), 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) 30.9570417874 

My calculator, on deg, gives me:

cos(1) 0.9998476... 
like image 304
tkbx Avatar asked Mar 26 '12 16:03

tkbx


People also ask

How do you convert from radians to degrees?

From the latter, we obtain the equation 1 radian = (180π)o . This leads us to the rule to convert radian measure to degree measure. To convert from radians to degrees, multiply the radians by 180°π radians .

Which function is used to convert an angle from radians into degrees in Python?

degrees() to convert an angle from radians to degrees.

How do you calculate degrees in Python?

The mmath. degrees() method converts an angle from radians to degrees. Tip: PI (3.14..) radians are equal to 180 degrees, which means that 1 radian is equal to 57.2957795 degrees.

Does Python math work in radians or degrees?

In Python, you can use math. radians to convert from degrees to radians. Note that it is not just Python that defaults to radians, it is usually the standard in mathematics to talk about angles in radians.


1 Answers

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)) 0.9998476951563913 

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.

like image 153
Mark Ransom Avatar answered Sep 19 '22 22:09

Mark Ransom