[R] How to handle large numbers?

david.jessop at ubs.com david.jessop at ubs.com
Thu Feb 12 08:23:44 CET 2009


Hi,

I've realised (as I'm sure have lots of others) that the second part of my answer is complete rubbish. Obviously having a bad day.

However you could work out one over your expression which then you split into two parts, calculate and then reinvert.

Apologies for version 1

David

--------------------------
David Jessop
Global Head of Quantitative Research
UBS Investment Research

+44 20 7567 9882


----- Original Message -----
From: Jessop, David
To: r-help at r-project.org <r-help at r-project.org>
Cc: 840116 at gmail.com <840116 at gmail.com>
Sent: Wed Feb 11 13:55:14 2009
Subject: How to handle large numbers?

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?


Thanks in advance!


Feng

-------------- next part --------------

Issued by UBS AG or affiliates to professional investors...{{dropped:28}}



More information about the R-help mailing list