Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to store a large number precisely in R?

Tags:

r

biginteger

Is there a way to store a large number precicely in R?

double is stored as a binary fraction and its precision varies with the value, and integer has limited range of 4 bytes.

What if I wanted to store a very large number precisely?

like image 861
Leonid Avatar asked Feb 23 '23 15:02

Leonid


2 Answers

You can try the bigz class from the gmppackage:

> library("gmp")
> 2^10000
[1] Inf
> 2^(as.bigz(10000))
[1] "199506.... and a LOT of more numbers!

It basically stores the number as a string and so avoiding the integer/double limits.

like image 84
Sacha Epskamp Avatar answered Feb 26 '23 21:02

Sacha Epskamp


It depends on what you mean by large number:

  • If you want numbers above the top end of double precision arithmetic, there is the Brobdingnag package

  • If you want more precision there are the gmp and related Rmpfr packages.

like image 28
Henry Avatar answered Feb 26 '23 22:02

Henry