Python atan2() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Python atan2() method is an equivalent of the mathematical value of tan1tan^{-1}. It is called an arctangent which belongs to inverse trigonometric functions. The result of arctangent is the angle between the axis in a 2D plane and a line drawn from the origin to a coordinate (x, y) in that plane.

Python atan2() Function

Python atan2() method is used to calculate the arctangent of y/x in radians, where x and y are the coordinates of the point (x, y) in a plane. In simple terms, atan2() calculates the inverse of the tangent value. The atan2() method is provided by the math package.

For tan(θ)=(yx)tan(\theta) = (\frac{y}{x}), the arctangent would be θ=tan1(yx)\theta = tan^{-1}(\frac{y}{x})


ATAN TAN

Syntax of Python atan2() Function

The syntax of Python atan2() function is:

Parameters of Python atan2() Function

The Python atan2() method accepts two parameters x and y, which are the coordinates of the point (x, y)

Return Value of Python atan2() Function

The Python atan2() method returns a float value which is the arctangent of y/x in radians. The output of the atan2() value ranges from to π

Exceptions of Python atan2() Function

Python atan2() method throws the TypeError exception if the value passed for x or y is not a real number.

The above method throws the exception

Working of atan2() Function in Python

Let's understand the working of the atan2 method with an example. Consider a point (3, 3) in a 2D plane and connect the point to the origin and the x-axis using a line. Now, we will see a right-angled triangle drawn on the plane as below.

TAN ATAN The triangle will have the base and height of size 3 units. Since it is a right-angled triangle, the angle (θ) between the triangle's base and the hypotenuse is 4545^\circ. Let's find the tan value using the below well-known formula:

tan(θ)=(oppositesideadjacentside)tan(45)=(33)=1tan(θ) = (\frac{opposite\,side}{adjacent\,side}) \Rightarrow tan(45^\circ)=(\frac{3}{3})=1

Now we know that tan(45)=1tan(45^\circ)=1. If we want to find the arctangent (i.e.) tan1tan^{-1}, the formula will be transformed into

45=tan1(1)45=tan1(33)θ=tan1(yx)45^\circ=tan^{-1}(1)\Rightarrow45^\circ=tan^{-1}(\frac{3}{3})\Rightarrow\theta=tan^{-1}(\frac{y}{x})

The value of tan1(yx)tan^{-1}(\frac{y}{x}) can be computed using Python atan2(y,x) method. We will get the value 0.78539816 when we compute arctangent of (3, 3) using Python atan2(3, 3) method. 0.78539816 is the radians equivalent of 4545^\circ.

Let's confirm whether we get the above value using Python atan2() method.

The output of the above code is:

Practical Applications of atan2() Function

One simple real-world application of the atan2() function is to find the angle of elevation and angle of depression. The angle of elevation is the angle between the horizontal plane and diagonal line from the observer’s eye to an object above the eye. In contrast, the angle of depression is the angle between the horizontal plane and diagonal line from the observer's eye to an object below the eye.

We can find the angle (θ\theta) of elevation and depression if we know the coordinates (x, y) of the objects with respect to the eye level (x-axis).

Python atan2() Function examples

Python atan2() for Positive Numbers

In this example, we pass two positive numbers for the value of x and y

Output

Python atan2() for Negative Numbers

In this example, we pass two negative numbers for the value of x and y

Output

Python atan2() for Irrational Numbers

In this example, we pass two irrational numbers (5\sqrt{5} and 3\sqrt{3}) for the value of x and y

Output

Conclusion

  • Python atan2() method is used to calculate the arctangent of y/x in radians.
  • Python atan2() calculates the inverse of the tangent value.
  • Python atan2() method is provided by the math package.
  • atan2() can be calculated for positive, negative, rational, and irrational numbers.
  • atan2() is used in the real world to calculate the angle of elevation and depression.