123

In the math module, I could only find cos/sin/tan/acos/asin/atan. These take the argument, respectively return the answer, in radians. How can I use degrees instead?

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...
4
  • 20
    You have things backward, cos takes an angle as input, not output. Commented Mar 26, 2012 at 17:08
  • 1
    @Mark Ransom How does that make sense?
    – tkbx
    Commented Mar 26, 2012 at 17:57
  • 5
    @lucase.62, Mark is correct. The cos function operates on an angle as the input, 1 in your example. On your calculator, this angle is in degress, in Python, this angle must be given in radians. The return value, x in your example, is a dimensionless number. On your calculator you have calculated the cos of 1 degree. In your Python example, you have calculated the cos of 1 radian, which is equivalent to 57.296 degrees.
    – Stewbob
    Commented Mar 26, 2012 at 18:03
  • 5
    cos takes an angle as input and produces a ratio as output. Trying to convert the output to degrees as you've done in your example doesn't make sense at all. You need to convert the input 1 from degrees to radians instead. If you were using acos it would be the other way around, the input is a ratio and the output is radians. Commented Mar 26, 2012 at 18:04

7 Answers 7

232

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.

1
  • 6
    If anyone wonders, numpy also have those functions, to convert entire arrays in one go. It also has them under the more explicit names rad2deg and deg2rad.
    – P-Gn
    Commented May 24, 2018 at 9:31
64

What are radians and what problem do they solve?:

Radians and degrees are two separate units of measure that help people express and communicate precise changes in direction. Wikipedia has some great intuition with their infographics on how one Radian is defined relative to degrees:

https://en.wikipedia.org/wiki/Radian

Conversion from radians to degrees

Python examples using libraries calculating degrees from radians:

>>> import math
>>> math.degrees(0)                       #0 radians == 0 degrees
0.0
>>> math.degrees(math.pi/2)               #pi/2 radians is 90 degrees
90.0
>>> math.degrees(math.pi)                 #pi radians is 180 degrees
180.0      
>>> math.degrees(math.pi+(math.pi/2))     #pi+pi/2 radians is 270 degrees
270.0 
>>> math.degrees(math.pi+math.pi)         #2*pi radians is 360 degrees
360.0      

Python examples using libraries calculating radians from degrees:

>>> import math
>>> math.radians(0)           #0 degrees == 0 radians
0.0
>>> math.radians(90)          #90 degrees is pi/2 radians
1.5707963267948966
>>> math.radians(180)         #180 degrees is pi radians
3.141592653589793
>>> math.radians(270)         #270 degrees is pi+(pi/2) radians
4.71238898038469
>>> math.radians(360)         #360 degrees is 2*pi radians
6.283185307179586

Source: https://docs.python.org/3/library/math.html#angular-conversion

The mathematical notation:

Mathematical notation of degrees and radians

You can do degree/radian conversion without Python libraries:

If you roll your own degree/radian converter, you have to write your own code to handle edge cases.

Mistakes here are easy to make and will hurt just like they hurt the developers of the 1999 Mars orbiter, who sunk $125,000,000 crashing it into Mars because of an unintuitive edge case here.

>>> 0 * 180.0 / math.pi                         #0 radians is 0 degrees
0.0
>>> (math.pi/2) * 180.0 / math.pi               #pi/2 radians is 90 degrees
90.0
>>> (math.pi) * 180.0 / math.pi                 #pi radians is 180 degrees
180.0
>>> (math.pi+(math.pi/2)) * 180.0 / math.pi     #pi+(pi/2) radians is 270 degrees
270.0
>>> (2 * math.pi) * 180.0 / math.pi             #2*pi radians is 360 degrees
360.0

Degrees to radians:

>>> 0 * math.pi / 180.0              #0 degrees in radians
0.0
>>> 90 * math.pi / 180.0             #90 degrees in radians
1.5707963267948966
>>> 180 * math.pi / 180.0            #180 degrees in radians
3.141592653589793
>>> 270 * math.pi / 180.0            #270 degrees in radians
4.71238898038469
>>> 360 * math.pi / 180.0            #360 degrees in radians
6.283185307179586

Expressing multiple rotations with degrees and radians

Single rotation valid radian values are between 0 and 2*pi. Single rotation degree values are between 0 and 360. However, if you want to express multiple rotations, valid radian and degree values are between 0 and infinity.

>>> import math
>>> math.radians(360)                 #one complete rotation
6.283185307179586
>>> math.radians(360+360)             #two rotations
12.566370614359172
>>> math.degrees(12.566370614359172)  #math.degrees and math.radians preserve the
720.0                                 #number of rotations

Collapsing multiple rotations:

You can collapse multiple degree/radian rotations into a single rotation by modding against the value of one rotation. For degrees you mod by 360, for radians you modulus by 2*pi.

>>> import math
>>> math.radians(720+90)        #2 whole rotations plus 90 is 14.14 radians
14.137166941154069
>>> math.radians((720+90)%360)  #14.1 radians brings you to 
1.5707963267948966              #the endpoint as 1.57 radians.

>>> math.degrees((2*math.pi)+(math.pi/2))            #one rotation plus a quarter 
450.0                                                #rotation is 450 degrees.
>>> math.degrees(((2*math.pi)+(math.pi/2))%(2*math.pi)) #one rotation plus a quarter
90.0                                                    #rotation brings you to 90.

Fundamental education on Radians and Degrees

5-minute refresher using Trigonometry and expression of rotation to convert radians to degrees and back: https://youtu.be/ovLbCvq7FNA?t=31

Khan Academy refresher on trigonometry, unit circle, and angular mathematics to use sine, cosine, and tangent to describe rotation and changes in rotation. https://www.khanacademy.org/math/algebra2/x2ec2f6f830c9fb89:trig/x2ec2f6f830c9fb89:unit-circle/v/unit-circle-definition-of-trig-functions-1

21

You can simply convert your radian result to degrees by using math.degrees() and rounding appropriately to the required decimal places.

For example:

>>> round(math.degrees(math.asin(0.5)), 2)
30.0
2
  • 1
    To match the example from the question use math.cos(math.radians(1)) Commented Mar 26, 2012 at 18:05
  • Why are you passing the radians into asin before passing it to math.degrees? What you have causes ValueError: math domain error for valid radian values. Commented Sep 16, 2017 at 0:16
5

Radians can also be converted to degrees using NumPy's rad2deg(). Like this:

>>> import numpy as np
>>> print(np.rad2deg(1))
57.29577951308232

If needed to round the result, then use NumPy's round(). In the example below, I rounded to 6 decimal places.

>>> print(np.round(np.rad2deg(1), 6)
57.29578
0
3

I also like to define my own functions that take and return arguments in degrees rather than radians. I am sure there some capitalization purest who don't like my names, but I just use a capital first letter for my custom functions. The definitions and testing code are below.

#Definitions for trig functions using degrees.
def Cos(a):
    return cos(radians(a))
def Sin(a):
    return sin(radians(a))
def Tan(a):
    return tan(radians(a))
def ArcTan(a):
    return degrees(arctan(a))
def ArcSin(a):
    return degrees(arcsin(a))
def ArcCos(a):
    return degrees(arccos(a))

#Testing Code
print(Cos(90))
print(Sin(90))
print(Tan(45))
print(ArcTan(1))
print(ArcSin(1))
print(ArcCos(0))

Note that I have imported math (or numpy) into the namespace with

from math import *

Also note, that my functions are in the namespace in which they were defined. For instance,

math.Cos(45)

does not exist.

1

-fix- because you want to change from radians to degrees, it is actually rad=deg * math.pi /180 and not deg*180/math.pi

import math
x=1                # in deg
x = x*math.pi/180  # convert to rad
y = math.cos(x)    # calculate in rad

print y

in 1 line it can be like this

y=math.cos(1*math.pi/180)
1
  • 1
    Never ever divide by an integer (in a setting such as this one), it will be rare cases where this is useful and even then there is ways of writing it more explicit.
    – fuesika
    Commented Apr 25, 2015 at 7:53
0

To convert radians to degrees in Python, you can use the degrees() function from the math module. Here's how you can do it:

# Define the angle in radians
angle_in_radians = 1.5708  # Replace this with your radian value

# Use the degrees() function to convert to degrees
angle_in_degrees = math.degrees(angle_in_radians)

# Print the result
print(f"{angle_in_radians} radians is equal to {angle_in_degrees} degrees.")

In this example, we first import the math module, which provides mathematical functions and constants in Python. Then, we define the angle in radians (replace 1.5708 with your radian value). Next, we use the math.degrees() function to convert the radian value to degrees. Finally, we print the result, displaying the angle in both radians and degrees.

Not the answer you're looking for? Browse other questions tagged or ask your own question.