[英]Factorial with R
I can't calculate the factorial of 365 by using factorial(365)
with the R logial, I think the capacity of this logicial don't allow it. 我无法通过将Rialial与factorial(365)
一起使用来计算365的factorial(365)
,我认为此逻辑阶乘的容量不允许这样做。 How can I do with an other method? 如何使用其他方法?
You can use lgamma(x+1)
to get the natural log of factorial. 您可以使用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
floating point math can get you into trouble at times, but lgamma(366)
is accurate for ln(factorial(365))
浮点数学有时lgamma(366)
您带来麻烦,但是lgamma(366)
对于ln(factorial(365))
是准确的
For large number try to use lfactorial
R.base function instead. 对于大量尝试使用lfactorial
R.base函数代替。 or lgamma
. 或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`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.