Python math.cosh()方法

Pythonmath.cosh() 方法是计算一个数字的双曲余弦的有效方法,x ,单位是弧度。

语法

math.cosh(x)

参数

x 任何正值或负值。

返回值

此方法的返回类型是一个float 值,代表x 的双曲余弦。

例子

使用Pythonmath.cosh() 方法获得以弧度表示的x 的双曲余弦。

代码摘录:

import math
x=-0.9
value=math.cosh(x)
print(f"The hyperbolic cosine of {x} is {value}.")
x=0
value=math.cosh(x)
print(f"The hyperbolic cosine of {x} is {value}.")
x=0.0086
value=math.cosh(x)
print(f"The hyperbolic cosine of {x} is {value}.")
x=math.inf
value=math.cosh(x)
print(f"The hyperbolic cosine of {x} is {value}.")

输出:

The hyperbolic cosine of -0.9 is 1.4330863854487745.
The hyperbolic cosine of 0 is 1.0.
The hyperbolic cosine of 0.0086 is 1.0000369802279205.
The hyperbolic cosine of inf is inf.

注意,参数应该是一个有效的数字。数值可以是正数,也可以是负数。

TypeError 使用Pythonmath.cosh() 方法

代码片段:

import math
##entering a string
x="Hi"
value=math.cosh(x)
print(f"The hyperbolic cosine of {x} is {value}.")
##entering a list
x=[1,2,3]
value=math.cosh(x)
print(f"The hyperbolic cosine of {x} is {value}.")
##entering complex numbers
x=1+5j
value=math.cosh(x)
print(f"The hyperbolic cosine of {x} is {value}.")

输出:

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    value=math.cosh(x)
TypeError: must be a real number, not str
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    value=math.cosh(x)
TypeError: must be a real number, not a list
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    value=math.cosh(x)
TypeError: can't convert complex to float

上面的代码片段显示了使用math.cosh 方法时所有可能的语法错误。