我发现使用 R 逻辑运算符无法计算365的阶乘 factorial(365)
,我认为这个逻辑运算符的容量有限制。请问还有其他方法可以计算吗?
lgamma(x+1)
来获取阶乘的自然对数。factorial(365)
# [1] Inf
# Warning message:
# In factorial(365) : value out of range in 'gammafn'
lgamma(366)
# 1792.332
# convince yourself that this works:
x <- 2:10
format(factorial(x), scientific = FALSE) == format(exp(lgamma(x + 1)), scientific = FALSE)
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
浮点数计算有时可能会导致问题,但是 lgamma(366)
对于 ln(factorial(365))
是准确的。
lfactorial
R基本函数或lgamma
。factorial(365)
[1] Inf
Warning message:
In factorial(365) : value out of range in 'gammafn'
> lfactorial(365)
[1] 1792.332
> lgamma(365+1)
[1] 1792.332`
Factorial[365]
- NasserFactorial
从哪里来? - stevec