Multiplication of large integers in R

I tried to multiply 111111111*111111111 , which is the same as 111111111^2 , and got the wrong results. It should give 12345678987654321 , but instead it gives a rounding error. Do I need to use any type of variable for long numbers or is it an error with R?

+4
source share
4 answers

The gmp package allows you to perform operations with large values.

 > library(gmp) > j <- 111111111 > k <- as.bigz(j) > mul.bigz(k, k) [1] "12345678987654321" 
+18
source

This is not a limitation of R specifically, it is a limitation of double precision floating point arithmetic . The standard double-precision floating-point number has about 16 decimal digits of precision. The answer to your sum requires 17. R does not have a variable type with greater accuracy, but not many other languages.

+7
source

In addition, a new int64 package from Romain Francois.

+4
source

I built a small library for R to do a calculation exceeding 17 digits. You can even have more than 500 digits if you want. Currently, it can perform additions, including smart decimal recognition. I'm not sure if I can share a link to my article here.

0
source

Source: https://habr.com/ru/post/1381811/


All Articles