Open In App
Related Articles

atan2() function in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
atan2(y, x) returns value of atan(y/x) in radians. The atan2() method returns a numeric value between –\pi and \pi representing the angle \theta of a (x, y) point and positive x-axis. Syntax :
atan2(y, x)
Parameter :
(y, x) - Both must be a numeric value.
Returns :
Returns atan(y / x), in radians.
The double value is \theta from polar coordinate (r, theta). 
TypeError :
Returns a TypeError if anything other than float is passed. 
  Code #1 :
# Let's find the slope when X
# and Y co-ordinates are given 
  
# imports math 
import math
  
# X and Y are co-ordinates
X = 2; Y = 2 
  
# slope in radians 
theta1 = math.atan2(Y, X) 
  
# print the Slope in radians
print(theta1)

                    
Output :
atan2(-0.5, -0.5): -2.356194490192345
atan2(1.2, 1.5): 0.6747409422235526
atan2(1.2, -1.5): 2.4668517113662407
  Code #2 :

                    
Output :
0.16514867741462683
0.5880026035475675
0.40489178628508343
0.4636476090008061
  Code #3 : Program demonstrating the error
# Python3 program to demonstrate the  
# TypeError in atan() method 
  
# importing math 
import math
  
y, x = 3, 6
  
# when integer values are passed 
# it returns a TypeError
theta = math.atan2([y], [x]) 
print(theta)

                    
Output :
Traceback (most recent call last):
  File "/home/622586ab389561bcdbfff258aca01e65.py", line 9, in 
    theta = math.atan2([y],[x]) 
TypeError: a float is required
  Practical Application : This function is used to find the slope in radians when Y and X co-ordinates are given. Code #4 :
# Let's find the slope when X
# and Y co-ordinates are given 
  
# imports math 
import math
  
# X and Y are co-ordinates
X = 2; Y = 2 
  
# slope in radians 
theta1 = math.atan2(Y, X) 
  
# print the Slope in radians
print(theta1)

                    
Output :
0.7853981633974483


Last Updated : 14 Mar, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads