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

Python math.atanh() 方法

math.atanh()方法数学模块的一种库方法,用于获取以弧度为单位的给定数字的双曲反正切,它接受一个数字并返回双曲反正切。

注意: math.atanh()方法仅接受数字,如果提供的数字不在范围内,则返回ValueError“ ValueError:math domain error”;如果提供数字以外的其他信息,则返回错误TypeError“ TypeError:需要浮点数“

它的语法 math.atanh() 方法:

    math.atanh(x)

Parameter(s): x –是要计算其双曲线反正切的数字。

返回值: float-它返回一个浮点值,该浮点值是数字x的双曲线反正切值。

示例

    Input:
    x = .99

    # 函数调用
    print(math.atanh(x))

    Output:
    2.6466524123622457

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

# Python代码演示示例 
# math.atanh() method

# 导入数学模块
import math 

# 数 
x = -0.23
print("math.atanh(",x,"): ", math.atanh(x))

x = 0
print("math.atanh(",x,"): ", math.atanh(x))

x = .99
print("math.atanh(",x,"): ", math.atanh(x))

输出结果

math.atanh( -0.23 ):  -0.2341894667593668
math.atanh( 0 ):  0.0
math.atanh( 0.99 ):  2.6466524123622457

ValueError示例

# Python代码演示示例 
# math.atanh() method with exception

# 导入数学模块
import math 

# 数 
x = -1
print("math.atanh(",x,"): ", math.atanh(x))

输出结果

Traceback (most recent call last):
  File "/home/main.py", line 9, in <module>
    print("math.atanh(",x,"): ", math.atanh(x))
ValueError: math domain error

TypeError示例

# Python代码演示示例 
# math.atanh() method with exception

# 导入数学模块
import math 

# 数 
x = "2"
print("math.atanh(",x,"): ", math.atanh(x))

输出结果

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