带有Python示例的math.atan2()方法

Python math.atan2()方法

math.atan2()方法数学模块的库方法,用于获取“ y / x”的反正切值(即atan(y / x)),它接受两个数字并返回“ y”的反正切/ x”

注: math.atan2()方法接受的唯一的号码,如果我们提供任何其他除外数量,则返回错误类型错误- “类型错误:需要浮动”

math.atan2()方法的语法:

    math.atan2(y, x)

Parameter(s): y,x –是要计算其反正切值的数字(即atan(y / x))。

返回值: float-返回一个浮点值,该值是数字y / x的反正切值。

示例

    Input:
    y = 0.2345
    x = 1.234

    # 函数调用
    print(math.atan2(y, x))

    Output:
    0.18779323183177443

Python代码演示math.atan2()方法的示例

# python代码演示示例 
# math.atan2()方法 

# 导入数学模块
import math

# 数字
x = -1
y = 1
print("atan2(",x,",",y,") is = ", math.atan2(x,y))

x = 0.2345
y = 1.234
print("atan2(",x,",",y,") is = ", math.atan2(x,y))

x = -5
y = 5
print("atan2(",x,",",y,") is = ", math.atan2(x,y))

x = 10
y = 20.23
print("atan2(",x,",",y,") is = ", math.atan2(x,y))

输出结果

atan2( -1 , 1 ) is =  -0.7853981633974483
atan2( 0.2345 , 1.234 ) is =  0.18779323183177443
atan2( -5 , 5 ) is =  -0.7853981633974483
atan2( 10 , 20.23 ) is =  0.45908957477179374

TypeError示例

# python代码演示示例 
# math.atan2()方法 with an exception

# 导入数学模块
import math

# 数字
x = "2"
y = "34"
print("atan2(",x,",",y,") is = ", math.atan2(x,y))

输出结果

Traceback (most recent call last):
  File "/home/main.py", line 10, in <module>
    print("atan2(",x,",",y,") is = ", math.atan2(x,y))
TypeError: a float is required