python math.lgamma()方法
lgamma()语法
math.lgamma(x, /)
python源码对lgamma()方法的介绍:
Natural logarithm of absolute value of Gamma function at x.
即,math.lgamma()方法可以用于求取参数x的伽玛函数值的自然对数值,可以通过math.gamma()先获取伽玛函数值,然后作为参数传入math.log()方法中求取自然对数之,具体可参考下方的实例。
参数
参数 | 描述 |
---|---|
x | 必须参数,大于0,为python的real number,如int或float等类型 |
提示:如果参数x不是real number,python将抛出TypeError,如果小于或等于0,将抛出ValueError。
返回值
Python浮点数float的类型值。
math.lgamma()实例代码
>>> import math
>>> math.lgamma(5)
3.178053830347945
>>> math.log(math.gamma(5)) #math.log()和math.gamma()方法的返回值与math.lgamma()返回值的对比
3.1780538303479458
>>> math.lgamma(-1) #参数小于0,python抛出ValueError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> math.lgamma(0) #参数等于0,python抛出ValueError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
>>> math.lgamma([]) #参数不是real number,python抛出TypeError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be real number, not list