Python atan2

Python 的 atan2 函数返回从 X 轴到指定点 (y, x) 的角度(以弧度为单位)。在本节中,我们将通过一个示例讨论 atan2 函数的用法。

Python 编程语言中 atan2 函数的语法是

math.atan2(y, x);
  • X:可以是数字或代表笛卡尔 X 坐标的有效数字表达式。
  • Y:可以是数字或代表笛卡尔 Y 坐标的有效数字表达式。

提示:如果我们将任何非数字值传递给 atan2 函数,它将返回 TypeError 作为输出。

Python atan2 函数示例

此语言中的 atan2 函数返回从 X 轴到指定点 (y, x) 的角度(以弧度为单位)。在此 atan2 示例中,我们将使用不同的数据类型找到相同的值并显示输出。

提示:请参阅 tan 文章以了解 Python 正切函数。

# Example

import math

Tup = (1, 2, 3, -4 , 5)
Lis = [-1, 2, -3.5, -4 , 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'))
Python ATAN2 Function

首先,我们将 atan2 函数直接应用于正整数和负整数。以下语句找到相应值的角度(以弧度为单位)。

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

接下来,我们在 元组列表项上使用了 atan2 函数。如果您查看上面的截图,它在它们上工作得很好。

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]))

接下来,我们将 math 函数应用于多个值。

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

在最后一个语句中,我们尝试将 atan2 函数应用于字符串值,它返回 TypeError 作为输出。

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