繁体   English   中英

带R的阶乘

[英]Factorial with R

我无法通过将Rialial与factorial(365)一起使用来计算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)您带来麻烦,但是lgamma(366)对于ln(factorial(365))是准确的

对于大量尝试使用lfactorial R.base函数代替。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM