# [R] How to handle large numbers?

david.jessop at ubs.com david.jessop at ubs.com
Wed Feb 11 14:55:14 CET 2009

```Hi

In answer to your first question is that it can be anything.  If we look
at 0 * a = 0 and let a tend to infinity, and b * Inf = Inf and let b
tend to zero then you can get both zero and infinity as an answer.  If
you say consider c * 1/c = 1 and let c tend to infinity then it can be
one too (you get the idea).

On your second point, then a bit of transformation would help:
exp (a) / (exp (b) + c) =
exp (a) / (exp (b) + exp (log (c)) =
exp (a - (b + log (c))

For example,
> exp (10) / (exp (11) - 2)
[1] 0.3678917
> exp (10 - 11 - log(2))
[1] 0.1839397

So in your case you get

exp (1000 - 1007 - log(5))
[1] 0.0001823764

Regards,

David

------------------------------

Message: 4
Date: Wed, 11 Feb 2009 11:40:14 +0100
From: Feng Li <840116 at gmail.com>
Subject: [R] How to handle large numbers?
To: r-help at r-project.org
Message-ID:
<339934530902110240y1cf64fd6u2101c3a706e1db41 at mail.gmail.com>
Content-Type: text/plain

Dear R,

I have two questions:

1, Why both R and Matlab give 0*Inf==NaN? To my knowledge, it should be
zero
mathematically. Am I right?

2, I need to calculate e.g. exp(a)/(exp(b)+c), where both a and b are
very
large numbers (>>1000, e.g a=1000, b=1007, and c=5). R gives me NaN when
I
use the following command:

> exp(1000)/(exp(1007)+5)
[1] NaN

I am pretty sure this should be close to zero. My question is whether
there
is a general way to solve this kind of question or should I do some
settings
before computing?