Introduction Python math.atan2() Method with Examples

math.atan2() method is a library method of math module, it is used to get the arc tangent value of “y/x” (i.e. atan(y/x)), it accepts two numbers and returns arc tangent of “y/x”.

Note: math.atan2() method accepts the only number, if we provide anything else except the number, it returns error TypeError - “TypeError: a float is required”.

Python math.atan2()

Syntax


 math.atan2(y, x)

Parameter(s):

  • It takes two parameters out of which both are of numeric datatype; any other data type is not acceptable if any other data type is passed as a parameter; it throws a type error.

Return Value

  • It returns the arc tangent value of the number in the float datatype.

  • X: It can be a number or a valid numerical expression which represent Cartesian X – Coordinate.

  • Y: It can be a number or a valid numerical expression which represent Cartesian Y – Coordinate.

Examples of Python math.atan2() method

Example 1


import math
x = 0.35
y = 3.25
print(math.atan2(y, x))

Output


1.4635174711464938

Example 2:

Write a program to show the working of the atan2() method in Python.


import math

x1 = 0.32
y1 = 4.33

x2 = 33.52
y2 = 55.34

x3 = 23.22
y3 = 33.11

print("x1= ", x1, "y1= ", y1, "arc= ", math.atan2(y1, x1))
print("x2= ", x2, "y2= ", y2, "arc= ", math.atan2(y2, x2))
print("x3= ", x3, "y3= ", y3, "arc= ", math.atan2(y3, x3))


Output


x1= 0.32 y1= 4.33 arc= 1.4970274295011163
x2= 33.52 y2= 55.34 arc= 1.0261887579183147
x3= 23.22 y3= 33.11 arc= 0.9591992824866347

In this example, we have seen that by passing two parameters (x,y) for different examples, get the desired atan2() method solution, which is the arc.

Example 3:

Write a program to pass a value of any other datatype in the atan2() function and display the output.


import math
x1 = 0.32
y1 = 'a'

print("arc= ", math.atan2(y1, x1))


Output


TypeError: must be real number, not str


In this example, we tried passing a parameter, which is not a real number, and because of that, the program throws an error stating that the number must be real and not string.

Example 4:

Python atan2() with list and tuple


# app.py

import math

Tup = (11, 21, 3, -46, 5)
Lis = [-11, 21, -3.5, -46, 5]

print('Tangent value of Positive Number = %.2f' % math.atan2(2, 4))
print('Tangent value of Negative Number = %.2f' % math.atan2(-1, 6))

print('Tangent value of Tuple Item = %.2f' % math.atan2(Tup[3], Tup[2]))
print('Tangent value of List Item = %.2f' % math.atan2(Lis[2], Lis[4]))

print('Tangent value of Multiple Number = %.2f' % math.atan2(2 + 7 - 4, 9-5))

print('Tangent value of String Number = %.2f', math.atan2('Hello', 'Python'))


Output


python app.py
Tangent value of Positive Number = 0.46
Tangent value of Negative Number = -0.17
Tangent value of Tuple Item = -1.51
Tangent value of List Item = -0.61
Tangent value of Multiple Number = 0.90
Traceback (most recent call last):
  File "app.py", line 14, in <module>
    print('Tangent value of String Number = %.2f', math.atan2('Hello', 'Python'))
TypeError: a float is required


If you pass the string to the atan2() function, then it will give the TypeError, which says: a float is required.

Thanks for reading !

#python #programming

Introduction Python math.atan2() Method with Examples
4.05 GEEK