Dear R experts, I have the logarithms of 2 values: log(a) = 1347 log(b) = 1351 And I am trying to solve this expression: exp( ln(a) ) - exp( ln(0.1) + ln(b) ) But of course every time I try to exponentiate the log(a) or log(b) values I get Inf. Are there any tricks I can use to get a real result for exp( ln(a) ) - exp( ln(0.1) + ln(b) ), either in logarithm or exponential form? Thank you very much for the help
I am sorry I have confused you, the logs are all base e: ln(a) = 1347 ln(b) = 1351 And I am trying to solve this expression: exp( ln(a) ) - exp( ln(0.1) + ln(b) ) Thank you. 2013/2/4 francesca casalino <francy.casalino at gmail.com>:> Dear R experts, > > I have the logarithms of 2 values: > > log(a) = 1347 > log(b) = 1351 > > And I am trying to solve this expression: > > exp( ln(a) ) - exp( ln(0.1) + ln(b) ) > > But of course every time I try to exponentiate the log(a) or log(b) > values I get Inf. Are there any tricks I can use to get a real result > for exp( ln(a) ) - exp( ln(0.1) + ln(b) ), either in logarithm or > exponential form? > > > Thank you very much for the help
Hello, Use a simple transfomation. Divide both t log.a <- 1347 log.b <- 1351 tmp1 <- exp(log.a - log(0.1) - log.b) - 1 result <- tmp1 * exp(0.1 + log.b) # -Inf Using base 10, the result would be -4.405386507*10^585 a number too large for R. See the help page ?.Machine Hope this helps, Rui Barradas Em 04-02-2013 15:11, francesca casalino escreveu:> Dear R experts, > > I have the logarithms of 2 values: > > log(a) = 1347 > log(b) = 1351 > > And I am trying to solve this expression: > > exp( ln(a) ) - exp( ln(0.1) + ln(b) ) > > But of course every time I try to exponentiate the log(a) or log(b) > values I get Inf. Are there any tricks I can use to get a real result > for exp( ln(a) ) - exp( ln(0.1) + ln(b) ), either in logarithm or > exponential form? > > > Thank you very much for the help > > ______________________________________________ > 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. >
Sorry, it should be: Divide both terms by exp(log(0.1) + log.b) to give tmp1 <- exp(log.a - log(0.1) - log.b) - 1 result <- tmp1 * exp(log(0.1) + log.b) Rui Barradas Em 04-02-2013 18:53, Rui Barradas escreveu:> Hello, > > Use a simple transfomation. Divide both t > > log.a <- 1347 > log.b <- 1351 > tmp1 <- exp(log.a - log(0.1) - log.b) - 1 > result <- tmp1 * exp(0.1 + log.b) # -Inf > > Using base 10, the result would be > > -4.405386507*10^585 > > a number too large for R. See the help page ?.Machine > > > Hope this helps, > > Rui Barradas > > > Em 04-02-2013 15:11, francesca casalino escreveu: >> Dear R experts, >> >> I have the logarithms of 2 values: >> >> log(a) = 1347 >> log(b) = 1351 >> >> And I am trying to solve this expression: >> >> exp( ln(a) ) - exp( ln(0.1) + ln(b) ) >> >> But of course every time I try to exponentiate the log(a) or log(b) >> values I get Inf. Are there any tricks I can use to get a real result >> for exp( ln(a) ) - exp( ln(0.1) + ln(b) ), either in logarithm or >> exponential form? >> >> >> Thank you very much for the help >> >> ______________________________________________ >> 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. >> > > ______________________________________________ > 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.
> Are there any tricks I can use to get a real result > for exp( ln(a) ) - exp( ln(0.1) + ln(b) ), either in logarithm or > exponential form?log.a <- 1347 log.b <- 1351 f0 <- function(log.a, log.b) exp(log.a) - exp(log(0.1) + log.b) will not work because f0(1347,1351) is too big to represent as a double precision number (abs(result)>10^308)). If you are satisfied with computing log(result) you can do it with some helper function: addOnLogScale <- function (e1, e2) { # log(exp(e1) + exp(e2)) small <- pmin(e1, e2) big <- pmax(e1, e2) log1p(exp(small - big)) + big } subtractOnLogScale <- function (e1, e2) { # log(abs(exp(e1) - exp(e2))) small <- pmin(e1, e2) big <- pmax(e1, e2) structure(log1p(-exp(small - big)) + big, isPositive = e1 > e2) } as f1 <- function(log.a, log.b) { # log(abs(exp(log.a) - exp( log(0.1) + log.b))), avoiding overflow subtractOnLogScale( log.a, log(0.1) + log.b ) } E.g., > f0(7,3) [1] 1094.625 > exp(f1(7,3)) [1] 1094.625 attr(,"isPositive") [1] TRUE With your log.a and log.b we get > f1(1347, 1351) [1] 1348.495 attr(,"isPositive") [1] FALSE You can use the Rmpfr package to compute the results to any desired precision. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of francesca casalino > Sent: Monday, February 04, 2013 8:00 AM > To: r-help at r-project.org > Subject: Re: [R] Exponentiate very large numbers > > I am sorry I have confused you, the logs are all base e: > > ln(a) = 1347 > ln(b) = 1351 > > And I am trying to solve this expression: > > exp( ln(a) ) - exp( ln(0.1) + ln(b) ) > > > Thank you. > > 2013/2/4 francesca casalino <francy.casalino at gmail.com>: > > Dear R experts, > > > > I have the logarithms of 2 values: > > > > log(a) = 1347 > > log(b) = 1351 > > > > And I am trying to solve this expression: > > > > exp( ln(a) ) - exp( ln(0.1) + ln(b) ) > > > > But of course every time I try to exponentiate the log(a) or log(b) > > values I get Inf. Are there any tricks I can use to get a real result > > for exp( ln(a) ) - exp( ln(0.1) + ln(b) ), either in logarithm or > > exponential form? > > > > > > Thank you very much for the help > > ______________________________________________ > 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.
On Mon, Feb 4, 2013 at 10:11 AM, francesca casalino <francy.casalino at gmail.com> wrote:> Dear R experts, > > I have the logarithms of 2 values: > > log(a) = 1347 > log(b) = 1351 > > And I am trying to solve this expression: > > exp( ln(a) ) - exp( ln(0.1) + ln(b) ) > > But of course every time I try to exponentiate the log(a) or log(b) > values I get Inf. Are there any tricks I can use to get a real result > for exp( ln(a) ) - exp( ln(0.1) + ln(b) ), either in logarithm or > exponential form? >Assuming you have bc, try this using the r-bc package available at http://r-bc.googlecode.com> source("http://r-bc.googlecode.com/svn/trunk/R/bc.R") > bc("e(1347) - e(l(0.1) + 1351)")[1] "-4405386005870716963250677607096464495107208188262037831496629962604389947022370210116563260536709412765244230596649428637325731073087741577278759185783174516835759767642509461583320513769415050345878341068570238633056331967056945630593558411688354769032035897813566786044573424610037424319481107653164601301363052343211549433260783299929388275371544460247043814951038945784700209474446687259093777751098627790391356643931274009579136611476739316800835499749286852733758497967615615441909946879410903730244743058347457562287380518284874370438753598667230357006574194406623712901009806747.7875941440505465257638722719393870571587463674102678572599172110259394653657558768303852737288352763" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Presumably you wish to *calculate* exp(1347) - exp(eps + 1351) where eps = ln(0.1) (rather than *solve* anything). This is equal to exp(1347)*(1 - exp(eps + 4)) = - exp(1347 + log(exp(eps+4) - 1) = -exp(1347 + 1.495107) = - exp(1348.495107) This is, to all intents and purposes, minus infinity. cheers, Rolf Turner On 02/05/2013 04:59 AM, francesca casalino wrote:> I am sorry I have confused you, the logs are all base e: > > ln(a) = 1347 > ln(b) = 1351 > > And I am trying to solve this expression: > > exp( ln(a) ) - exp( ln(0.1) + ln(b) ) > > > Thank you. > > 2013/2/4 francesca casalino <francy.casalino at gmail.com>: >> Dear R experts, >> >> I have the logarithms of 2 values: >> >> log(a) = 1347 >> log(b) = 1351 >> >> And I am trying to solve this expression: >> >> exp( ln(a) ) - exp( ln(0.1) + ln(b) ) >> >> But of course every time I try to exponentiate the log(a) or log(b) >> values I get Inf. Are there any tricks I can use to get a real result >> for exp( ln(a) ) - exp( ln(0.1) + ln(b) ), either in logarithm or >> exponential form? >> >> >> Thank you very much for the help > ______________________________________________ > 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. >
On Tue, Feb 5, 2013 at 3:35 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On Mon, Feb 4, 2013 at 10:11 AM, francesca casalino > <francy.casalino at gmail.com> wrote: >> Dear R experts, >> >> I have the logarithms of 2 values: >> >> log(a) = 1347 >> log(b) = 1351 >> >> And I am trying to solve this expression: >> >> exp( ln(a) ) - exp( ln(0.1) + ln(b) ) >> >> But of course every time I try to exponentiate the log(a) or log(b) >> values I get Inf. Are there any tricks I can use to get a real result >> for exp( ln(a) ) - exp( ln(0.1) + ln(b) ), either in logarithm or >> exponential form? >> > > Assuming you have bc, try this using the r-bc package available at > http://r-bc.googlecode.com > >> source("http://r-bc.googlecode.com/svn/trunk/R/bc.R") >> bc("e(1347) - e(l(0.1) + 1351)") > [1] "-4405386005870716963250677607096464495107208188262037831496629962604389947022370210116563260536709412765244230596649428637325731073087741577278759185783174516835759767642509461583320513769415050345878341068570238633056331967056945630593558411688354769032035897813566786044573424610037424319481107653164601301363052343211549433260783299929388275371544460247043814951038945784700209474446687259093777751098627790391356643931274009579136611476739316800835499749286852733758497967615615441909946879410903730244743058347457562287380518284874370438753598667230357006574194406623712901009806747.7875941440505465257638722719393870571587463674102678572599172110259394653657558768303852737288352763" > >Just continuing from where we left off:> # log of expression > bc("l(- e(1347) + e(l(0.1) + 1351))")[1] "1348.4951072860942085476545300380241005507587854108159740610940322438151104344740271849332074211018350282" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com