Tobias
Tobias

Reputation: 574

Calculations with integers with more than 16 digits

I have two big integers. Both have more than 16 digits (20 to be exactly) and I know that due to the double-precision floating-point arithmetic I have a limitation in using those numbers for calculations or even storing them in variables (independent from the programming language).

However, I though that maybe gmp library should handle them but unfortunately it does not. One can caclulate bigger integers but the used input for the calculation should not exceed 16 digits.

Here is the example:

library(gmp)
x <- as.bigz(99899999999996937503)
y <- as.bigz(99899999999996936542)
z <- sub.bigz(x,y)

In both cases they are stored as 99899999999996936192 and zis equal to 0.

Is there any library which accepts integers with more than 16 digits as input?

Upvotes: 2

Views: 198

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18445

As explained in the help for as.bigz, you need to put quotes round these big integers (i.e. input as characters), otherwise R converts them to ordinary integers before passing to as.bigz...

library(gmp)
x <- as.bigz("99899999999996937503")
y <- as.bigz("99899999999996936542")
z <- sub.bigz(x,y)

z
Big Integer ('bigz') :
[1] 961

Upvotes: 4

Related Questions