Search code examples
rnanarbitrary-precision

What if a number is divided by another too small number in R


I have tried that 1e-324 is basically zero in R. So, what should I do when I want to do some calculation like this:

1e-2000/1e-2000
#[1] NaN
#But the real value should be 1

So, any assistance is appreciated.


Solution

  • You need arbitrary-precision arithmetic. Take a look at gmp:

    library(gmp)
    divide_by_zero <- function(n) {10^n / 10^n}
    divide_by_nonzero <- function(n) {pow.bigz(10, n) / pow.bigz(10, n)}
    divide_by_zero(-320:-330)
    # [1]   1   1   1   1 NaN NaN NaN NaN NaN NaN NaN
    divide_by_nonzero(-320:-330)
    #Big Rational ('bigq') object of length 11:
    # [1] 1 1 1 1 1 1 1 1 1 1 1