how to prevent R from showing exp and log values inf and zero

Viewed 577

I am going to calculate matrix normal densities to use them in loglikelihood formula.
in my calculations, I need to calculate exponential of large numbers ( tens of million ). I realized, R gives back infinity for exp(i), when i >=710.
is there anyway I can force R manually not to show infinity, or its log not to be 0 accordingly?


thanks for correcting my typo
2 Answers

You should know that the max double number in R is .Machine$double.xmax. If you have numbers great than this, it will definitely give you Inf

> log(.Machine$double.xmax)  
[1] 709.7827

There is also contributed package Brobdingnag that can handle numbers much bigger than exp(710).

library(Brobdingnag)

exp(as.brob(710))
#[1] +exp(710)

exp(as.brob(10^seq(100, 310, by = 5)))
# [1] +exp(1e+100) +exp(1e+105) +exp(1e+110) +exp(1e+115) +exp(1e+120)
# [6] +exp(1e+125) +exp(1e+130) +exp(1e+135) +exp(1e+140) +exp(1e+145)
#[11] +exp(1e+150) +exp(1e+155) +exp(1e+160) +exp(1e+165) +exp(1e+170)
#[16] +exp(1e+175) +exp(1e+180) +exp(1e+185) +exp(1e+190) +exp(1e+195)
#[21] +exp(1e+200) +exp(1e+205) +exp(1e+210) +exp(1e+215) +exp(1e+220)
#[26] +exp(1e+225) +exp(1e+230) +exp(1e+235) +exp(1e+240) +exp(1e+245)
#[31] +exp(1e+250) +exp(1e+255) +exp(1e+260) +exp(1e+265) +exp(1e+270)
#[36] +exp(1e+275) +exp(1e+280) +exp(1e+285) +exp(1e+290) +exp(1e+295)
#[41] +exp(1e+300) +exp(1e+305) +exp(Inf)       

And 10^305 is much bigger than 710. See the package vignette.

Related