当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python math.atan2()用法及代码示例


Python math.atan2() 方法

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

注意: math.atan2() 方法接受唯一的数字,如果我们提供除数字以外的任何其他内容,则返回错误 TypeError -“TypeError:a float is required”。

math.atan2() 方法的语法:

    math.atan2(y, x)

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

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

例:

    Input:
    y = 0.2345
    x = 1.234

    # function call
    print(math.atan2(y, x))

    Output:
    0.18779323183177443

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

# python code to demonstrate example of 
# math.atan2() method 

# importing math module
import math

# numbers
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

类型错误示例

# python code to demonstrate example of 
# math.atan2() method with an exception

# importing math module
import math

# numbers
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


相关用法


注:本文由纯净天空筛选整理自 math.atan2() method with example in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。