Dear all, I am struggling to understand this. What happens when you raise a negative value to a power and the result is a very large number? B [1] 47.73092> -51^B[1] -3.190824e+81 # seems fine # now this:> x <- seq(-51,-49,length=100)> x^B[1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN <snip>> is.numeric(x^B)[1] TRUE> is.real(x^B)[1] TRUE> is.infinite(x^B)[1] FALSE FALSE FALSE FALSE FALSE I am lost, I checked the R mailing help, but could not find anything directly. I loaded package Brobdingnag and tried: as.brob(x^B) [1] NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN)> as.brob(x)^B[1] NAexp(187.67) NAexp(187.65) NAexp(187.63) NAexp(187.61) I guess I must be misunderstanding something fundamental. Any clues would be really appreciated. Willem
On 8/30/2007 11:08 AM, willem vervoort wrote:> Dear all, > I am struggling to understand this. > > What happens when you raise a negative value to a power and the result > is a very large number? > > B > [1] 47.73092 > >> -51^B > [1] -3.190824e+81You should be using parentheses. You evaluated -(51^B), not (-51)^B. The latter gives NaN.> > # seems fine > # now this: >> x <- seq(-51,-49,length=100) > >> x^B > [1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN <snip> >> is.numeric(x^B) > [1] TRUE >> is.real(x^B) > [1] TRUE >> is.infinite(x^B) > [1] FALSE FALSE FALSE FALSE FALSE > > I am lost, I checked the R mailing help, but could not find anything > directly. I loaded package Brobdingnag and tried: > as.brob(x^B) > [1] NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) >> as.brob(x)^B > [1] NAexp(187.67) NAexp(187.65) NAexp(187.63) NAexp(187.61) > > I guess I must be misunderstanding something fundamental.Two things: operator precedence (the ^ has higher precedence than the unary minus), and the mathematical definition of raising something to a fractional power. The approach R takes to the latter is to define x^B to be exp(B * ln(x)), and ln(x) is undefined for negative x. Duncan Murdoch
Hello, it seems to be an R bug. It gives strange errors for non-integer exponents:> versionplatform i686-redhat-linux-gnu version.string R version 2.4.1 (2006-12-18)> x^47.0[1] -1.802180e+80 -1.768932e+80 -1.736284e+80 -1.704227e+80 -1.672748e+80 [...]> x^47.10[1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN [...]> x[2]^b[1] NaN But:> x[2][1] -50.9798> -50.9798^b[1] -3.131003e+81 So it is not a merely numerical problem. Maybe a bug in memory allocation for vectors containing such big numbers? Scion
On 8/30/07, Gustaf Rydevik <gustaf.rydevik at gmail.com> wrote:> On 8/30/07, willem vervoort <willemvervoort at gmail.com> wrote: > > Dear all, > > I am struggling to understand this. > > > > What happens when you raise a negative value to a power and the result > > is a very large number? > > > > B > > [1] 47.73092 > > > > > -51^B > > [1] -3.190824e+81 > > > > # seems fine > > # now this: > > > x <- seq(-51,-49,length=100) > > > > > x^B > > [1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN <snip> > > > is.numeric(x^B) > > [1] TRUE > > > is.real(x^B) > > [1] TRUE > > > is.infinite(x^B) > > [1] FALSE FALSE FALSE FALSE FALSE > > > > I am lost, I checked the R mailing help, but could not find anything > > directly. I loaded package Brobdingnag and tried: > > as.brob(x^B) > > [1] NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) > > > as.brob(x)^B > > [1] NAexp(187.67) NAexp(187.65) NAexp(187.63) NAexp(187.61) > > > > I guess I must be misunderstanding something fundamental. > > > > Any clues would be really appreciated. > > Willem > > > > non-integer exponents of non-positive values are not defined, thus the NaN. > > > -1^2.5 > [1] -1 > > (-1)^2.5 > [1] NaN > > > > Best, > > Gustaf >To modify, that is if you are working with reals.> x[1] -1> x^(1/2)[1] NaN> class(x)<-"complex" > x^(1/2)[1] 0+1i> x<--51 > x^47.7[1] NaN> class(x)<-"complex" > x^47.7[1] 1.660795e+81-2.285889e+81i>Best, Gustaf -- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik
> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of willem vervoort > Sent: Thursday, August 30, 2007 8:09 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Behaviour of very large numbers > > Dear all, > I am struggling to understand this. > > What happens when you raise a negative value to a power and the result > is a very large number? > > B > [1] 47.73092 > > > -51^B > [1] -3.190824e+81 > > # seems fineIt seems fine because the precedence of the '^' operator is higher than the unary negation operator '-'. Your example is actually evaluated as -(51^B).> # now this: > > x <- seq(-51,-49,length=100) > > > x^B > [1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN > NaN NaN NaN <snip> > > is.numeric(x^B) > [1] TRUE > > is.real(x^B) > [1] TRUE > > is.infinite(x^B) > [1] FALSE FALSE FALSE FALSE FALSE > > I am lost, I checked the R mailing help, but could not find anything > directly. I loaded package Brobdingnag and tried: > as.brob(x^B) > [1] NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) > > as.brob(x)^B > [1] NAexp(187.67) NAexp(187.65) NAexp(187.63) NAexp(187.61) > > I guess I must be misunderstanding something fundamental. >Yes, you can't raise a negative number to a fractional power. Hope this is helpful, Dan Daniel J. Nordlund Research and Data Analysis Washington State Department of Social and Health Services Olympia, WA 98504-5204
willem vervoort wrote:> Dear all, > I am struggling to understand this. > > What happens when you raise a negative value to a power and the result > is a very large number? > > B > [1] 47.73092 > > >> -51^B >> > [1] -3.190824e+81 > > # seems fine >Well, this seems not to be what you intended to do, you didn't raise a negative value to a power, but you got the negative of a positive number raised to that power (operator precedence, -51^B is the same as -(51^B) and not the same as (-51)^B...). If you really want to raise a negative value to a fractional power, you may want to tell R to use complex numbers: B <- 47.73092 x <- complex(real=seq(-51,-49,length=10)) x^B [1] 2.117003e+81-2.387323e+81i 1.718701e+81-1.938163e+81i [3] 1.394063e+81-1.572071e+81i 1.129702e+81-1.273954e+81i [5] 9.146212e+80-1.031409e+81i 7.397943e+80-8.342587e+80i [7] 5.978186e+80-6.741541e+80i 4.826284e+80-5.442553e+80i [9] 3.892581e+80-4.389625e+80i 3.136461e+80-3.536955e+80i > Regards, Martin> # now this: > >> x <- seq(-51,-49,length=100) >> > > >> x^B >> > [1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN <snip> > >> is.numeric(x^B) >> > [1] TRUE > >> is.real(x^B) >> > [1] TRUE > >> is.infinite(x^B) >> > [1] FALSE FALSE FALSE FALSE FALSE > > I am lost, I checked the R mailing help, but could not find anything > directly. I loaded package Brobdingnag and tried: > as.brob(x^B) > [1] NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) > >> as.brob(x)^B >> > [1] NAexp(187.67) NAexp(187.65) NAexp(187.63) NAexp(187.61) > > I guess I must be misunderstanding something fundamental. > > Any clues would be really appreciated. > Willem > > ______________________________________________ > R-help at stat.math.ethz.ch 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 Thu, 30 Aug 2007, willem vervoort wrote:> Dear all, > I am struggling to understand this. > > What happens when you raise a negative value to a power and the result > is a very large number?Where are the 'very large numbers' here? R can cope with much larger numbers (over 10^300).> B > [1] 47.73092 > >> -51^B > [1] -3.190824e+81Yes, that is -(51^B).> # seems fine > # now this: >> x <- seq(-51,-49,length=100) > >> x^B > [1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN <snip> >> is.numeric(x^B) > [1] TRUE >> is.real(x^B) > [1] TRUE >> is.infinite(x^B) > [1] FALSE FALSE FALSE FALSE FALSE > > I am lost, I checked the R mailing help, but could not find anything > directly. I loaded package Brobdingnag and tried: > as.brob(x^B) > [1] NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) >> as.brob(x)^B > > I guess I must be misunderstanding something fundamental.You are. A negative number to a non-integer power is undefined in the real number system. Look at (x+0i)^B. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Hello On 30 Aug 2007, at 16:08, willem vervoort wrote:> > What happens when you raise a negative value to a power and the result > is a very large number? >[snip]> I loaded package Brobdingnag and tried: > as.brob(x^B) > [1] NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) NAexp(NaN) >> as.brob(x)^B > [1] NAexp(187.67) NAexp(187.65) NAexp(187.63) NAexp(187.61) > > I guess I must be misunderstanding something fundamental. >As others have said, in this case one needs complex arithmetic. In a Brobdingnagian context one would use Glubbdubdribian numbers: > x <- seq(-51,-49,length=10) > as.glub(x)^112313.3 [1] -exp(441600)-exp(441600)i -exp(441110)-exp(441110)i -exp (440610)-exp(440610)i [4] -exp(440120)-exp(440120)i -exp(439620)-exp(439620)i -exp (439120)-exp(439120)i [7] -exp(438620)-exp(438620)i -exp(438120)-exp(438120)i -exp (437610)-exp(437610)i [10] -exp(437100)-exp(437100)i > -- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743