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 -- Feng Li Department of Statistics Stockholm University 106 91 Stockholm, Sweden [[alternative HTML version deleted]]
On Wed, 11 Feb 2009, Feng Li wrote:> 1, Why both R and Matlab give 0*Inf==NaN? To my knowledge, it should be zero > mathematically. Am I right?No. 0*Inf is NaN according to the floating point arithmetic standards that R depends on. It's true that 0*x==0 for all finite x, but it's also true that x*Inf==Inf for all non-zero x, and you can't preserve both of these with 0*Inf.> 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.No. It should be close to 1. Try 1000/(1000+5) for a simpler example.> My question is whether there > is a general way to solve this kind of question or should I do some settings > before computing? >exp(1000)/(exp(1007)+5) is 1/(exp(7)+5*exp(-1000)), which is the same as 1/exp(7) to more than 400 digits accuracy. -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
Feng checkout the Brobdingnag package: > library(Brobdingnag) > exp(1000)/(exp(1007)+5) [1] NaN > as.numeric(exp(as.brob(1000))/(exp(as.brob(1007))+5)) [1] 0.000911882 > Feng Li wrote:> 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 > > > >-- Robin K. S. Hankin Uncertainty Analyst University of Cambridge 19 Silver Street Cambridge CB3 9EP 01223-764877
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 Issued by UBS AG or affiliates to professional investors...{{dropped:27}}
Try Ryacas. N means numerically evaluate in yacas (as we don't want it evaluated on the R side where we already know we get NaN).> library(Ryacas) > Eval(yacas("N(Exp(1000)/(Exp(1007)+5))")[1] 0.000911882 You can explore it somewhat in R via: curve(exp(x)/(exp(x+7)+5), 1, 500) On Wed, Feb 11, 2009 at 5:40 AM, Feng Li <840116 at gmail.com> wrote:> 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 > > > > -- > Feng Li > Department of Statistics > Stockholm University > 106 91 Stockholm, Sweden > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
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}}