How to work with large numbers in R?

I would like to change the accuracy of the calculation of R. For example, I would like to calculate x^6with x = c(-2.5e+59, -5.6e+60). To calculate it, I have to change the accuracy in R, otherwise the result will be Inf, and I do not know how to do it.

+5
source share
1 answer

As Livy notes in his comment, this is a problem with R (and, in fact, most programming languages), with the way numbers are represented in binary format.

To work with extremely large / small floating point numbers, you can use the library Rmpfr:

install.packages("Rmpfr")
library("Rmpfr")
x <- c(-2.5e+59, -5.6e+60)
y <- mpfr(x, 6)  # the second number is how many decimal places you want
y^6
# 2 'mpfr' numbers of precision  6   bits 
# [1] 2.50e356 3.14e364

, , R (, exp(1800)), "Brobdingnag":

install.packages("Brobdingnag")
library("Brobdingnag")

## An example of a single number too large for R: 
10^1000.7
# [1] Inf

## Now using the Brobdingnag package:
10^as.brob(1000.7)
# [1] +exp(2304.2)
+6

All Articles