Arnoneel Sinha
Arnoneel Sinha

Reputation: 434

Large exponent values in R with 'Rmpfr'

I am trying to calculate the value of exp(1000) or more in R. I looked at the solution provided in Calculate the exponentials of big negative value and followed it to replicate the code for negative values first, but I have the following output.

a <- mpfr(exp(-1000),precBits = 64)
a
1 'mpfr' number of precision  64   bits 
[1] 0

I do not understand why my output would be different from the provided solution. I understand this particular solution is for negative values and that I am looking for positive exponents. Regardless, it should work for both ways.

Upvotes: 1

Views: 234

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226182

You need to convert to extended precision before you exponentiate, otherwise R will try to compute exp(-1000) with its usual precision and underflow will occur (as you found out).

> a <- exp(mpfr(-1000, precBits = 64))
> a
1 'mpfr' number of precision  64   bits 
[1] 5.07595889754945676548e-435

Upvotes: 1

Related Questions