This math.atan2() method is used to calculate the arctangent of y/x, in radians, where x and y are coordinates of a point (x,y) in the cartesian plane.
The value returned by this function is between
-πandπ.
math.atan2(y, x)
This method accepts positive and negative numeric values.
y: the first parameterx: the second parameterThis method returns a primitive float value, representing the arc tangent x/y in radians, between -π and π.
This angle
θreturned by this method is from polar coordinate(r,θ).
import math# Two positive coordinatesprint(math.atan2(3, 4))# Two negative coordinatesprint(math.atan2(-3, -4))# One positive and One negative coordinateprint(math.atan2(3.4, -3.1))
TypeErrorThis method will generate an error when we pass integer arguments.
It’s also Python version-dependent.
Python 3: TypeError: a float is required.
Python 3.8: TypeError: must be a real number, not a list.
TypeErrorimport math# It will generate type error on integer argumenty, x = 2, 3theeta = math.atan2([y], [x])print(theeta)
TypeErrorimport math# It will generate type error on integer argumenty, x = 2, 3theeta = math.atan2([y], [x])print(theeta)