Ways to handle large numbers in R (concepts could also apply to other programming language)

This is a work-in-progress, so right now this blog may seem crude and trivial.

Use the natural logarithm

For example, I would like to calculate and plot how the result of N choose k evolves when N is fairly large (>1000).

I could just use choose(n, k), which returns $$n! / (k! (n-k)!)$$ which is the binomial coefficients.

But I could also use lchoose(n, k), which returns the natural logarithm of $$n! / (k! (n-k)!)$$ And if I want the actual result I can just exp() back the result to get that big number.

BTW there is that combn(n, k) function if you want to get all possible combinations, and ncol would be the total number of combinations.

# if a vertex in graph has 1848 edges, and I want to see the number of combinations from k=0...214
res <- c()
for (i in 0:214)
  res <- c(res, lchoose(1848,i))
plot(res)

res1.png

And choose(1848, 213) would be as large as 1.914196e+285.

Reference