[R] How to handle large numbers?

Stavros Macrakis macrakis at alum.mit.edu
Wed Feb 11 23:06:10 CET 2009


On Wed, Feb 11, 2009 at 4:38 PM, Wacek Kusnierczyk
<Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
> Stavros Macrakis wrote:
>> For example:
>>> x<-40; log(exp(x)+1)-x
>> [1] 0
>>> x<-as.brob(40); log(exp(x)+1)-x
>> [1] -exp(-Inf)
>> The correct answer is about 4e-18.  Perhaps Ryacas or some other tool gets this right,
>
> bc gets it arbitrarily right:...

Yes, any arbitrary-precision systems should get it right if you
specify the right number of digits to calculate. But why use expensive
multiple-precision when you don't need it?

The 'right' way to calculate things like log(exp(x)+1)-x and
exp(x)/(exp(x+k)+a) for large values of x is to understand their
asymptotic behavior, easy to do by hand or in a CAS like Maxima (and
presumably yacas, though I don't have any experience with it) -- for
large x, the first looks like exp(-x)-exp(-2*x)/2+...; the second
looks like exp(-k)-a*exp(-(x+k)) + ...  No need for
arbitrary-precision arithmetic once you know this.

         -s




More information about the R-help mailing list