This is a test version of Biostars. For the public version, visit https://www.biostars.org.
factorial for bigger number in R

Hi if factorial(1536) is used in R, output is inf or nan How to find 1536! in R?

r

This is a questions for CrossValidated, however as your question intrigued me, I had a look. If you run the code you suggest:

> factorial(1536)
[1] Inf
Warning message:
In factorial(1536) : value out of range in 'gammafn'

Any factorial >170 is too large a number, and it won't be computed.

Perl to the rescue:

perl -MMath::BigInt -e 'print Math::BigInt->bfac(1536)'

factorial(1536) would be more than a google, so you couldn't even represent it in a 64 bit floating point number (or even close).

There are similar questions asked on the R mailing list and other forums. Try googling "r large factorial" or "r factorial inf". (If you can work in log space you can use lfactorial(x) instead)

Hello vimlakany!

We believe that this post does not fit the main topic of this site.

Not a bioinformatics question.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

There were many questions related to python and R in Biostars? Thats the reason I posted the above question here.

Hi vimlakany, Biostars is a bioinformatics QA and certainly in bioinformatics we use R and python a lot. That is why you find a lot questions on this topic. However, we require that each question is specifically related to bioinformatics or computational biology, and that this relation has to be apparent and detailed in the question itself. Your question looks like a question on numeric analysis or computation.

Btw: you can use lfactorial to use lgamma instead of gamma and get the natural logarithm:

> exp(lfactorial( 170 ))
[1] 7.257416e+306
> factorial(170)
[1] 7.257416e+306
> factorial(1536)
[1] Inf
Warning message:
In factorial(1536) : value out of range in 'gammafn'
> lfactorial(1536)
[1] 9738.123
>

0 answers

No answers yet.

Log in to answer this question.