在python编程语言中,有一些内置函数是在math模块中定义的–它们可以用于双曲计算(这些函数是基于双曲线而不是圆的三角函数的类似物)。python有以下双曲函数,用于各种用途。
双曲函数 | 描述 | 例子 |
---|---|---|
math.acosh() | 它返回给定数字的双曲弧余弦。 | math.acosh(x) |
math.asinh() | 它返回给定数字的双曲正弦波。 | math.asinh(x) |
math.atanh() | 它返回给定数字的双曲正切值。 | math.atanh(x) |
math.cosh() | 它返回给定数字的双曲余弦。 | math.cosh(x) |
math.sinh() | 它返回给定数字的双曲正弦。 | math.sinh(x) |
math.tanh() | 它返回给定数字的双曲正切值。 | math.tanh(x) |
#Python代码演示示例 #所有双曲函数 #导入数学模块 import math # number x = 1.25 # math.acosh()print("math.acosh(",x,"): ", math.acosh(x)) # math.asinh()print("math.asinh(",x,"): ", math.asinh(x)) # math.atanh()x = 0.56 print("math.atanh(",x,"): ", math.atanh(x)) # math.cosh()print("math.cosh (",x,"): ", math.cosh(x)) x = 1.25 # math.sinh()print("math.sinh (",x,"): ", math.sinh(x)) # math.tanh()print("math.tanh (",x,"): ", math.tanh(x))
输出
math.acosh( 1.25 ): 0.6931471805599453 math.asinh( 1.25 ): 1.0475930126492587 math.atanh( 0.56 ): 0.632833186665638 math.cosh ( 0.56 ): 1.160940782072458 math.sinh ( 1.25 ): 1.6019190803008256 math.tanh ( 1.25 ): 0.8482836399575129
有两种类型的异常发生,
ValueError
当我们提供无效值(Number)时,会发生此异常。
TypeError
当我们提供了一个不同类型的值,除了一个数字时,这个异常发生。
ValueError 示例
#Python代码演示示例 # 数学.atanh()方法异常 #导入数学模块 import math # number x = -1 print("math.atanh(",x,"): ", math.atanh(x))
输出
Traceback (most recent call last): File "/home/main.py", line 9, in <odule> print("math.atanh(",x,"): ", math.atanh(x)) ValueError: math domain error
TypeError 示例
#python代码以演示。 #math.atanh()方法异常。 #导入数学模块 import math # number 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