I had a question about the basic power functions in R. For example from the R console I enter: -1 ^ 2 [1] -1 but also -1^3 [1] -1 -0.1^2 [1] -0.01 Normally pow(-1, 2) return either -Infinity or NaN. Has R taken over the math functions? If so I would think that -1^2 is 1 not -1 and -0.1^2 is 0.01 not -0.01. Thank you. Kevin
Please post to only one of r-help at r-project.org r-help at stat.math.ethz.ch not to both, as the same list then got two copies. On Sat, 3 Jan 2009, rkevinburton at charter.net wrote:> I had a question about the basic power functions in R. > > For example from the R console I enter: > > -1 ^ 2 > [1] -1 > > but also > > -1^3 > [1] -1 > > -0.1^2 > [1] -0.01 > > Normally pow(-1, 2) return either -Infinity or NaN. Has R taken over > the math functions? If so I would think that -1^2 is 1 not -1 and -0.1^2 > is 0.01 not -0.01.See ?Syntax, linked from ?`^` : The following unary and binary operators are defined. They are listed in precedence groups, from highest to lowest. '[ [[' indexing ':: :::' access variables in a name space '$ @' component / slot extraction '^' exponentiation (right to left) '- +' unary minus and plus so -1^2 is -(1^2) not (-1)^2. -- 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
Watch the operator precedences. In R (and many other languages) -1^2 == -(1^2) == -1 Perhaps you intended: (-1)^2 == 1 On Sat, Jan 3, 2009 at 3:32 PM, <rkevinburton at charter.net> wrote:> I had a question about the basic power functions in R. > > For example from the R console I enter: > > -1 ^ 2 > [1] -1 > > but also > > -1^3 > [1] -1 > > -0.1^2 > [1] -0.01 > > Normally pow(-1, 2) return either -Infinity or NaN. Has R taken over the math functions? If so I would think that -1^2 is 1 not -1 and -0.1^2 is 0.01 not -0.01. > > Thank you. > > Kevin > > ______________________________________________ > 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. >
(-1)^2 Or a <- -1 a^2 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of rkevinburton at charter.net Sent: January 3, 2009 3:33 PM To: r-help at r-project.org; r-help at stat.math.ethz.ch Subject: [R] Power functions? I had a question about the basic power functions in R. For example from the R console I enter: -1 ^ 2 [1] -1 but also -1^3 [1] -1 -0.1^2 [1] -0.01 Normally pow(-1, 2) return either -Infinity or NaN. Has R taken over the math functions? If so I would think that -1^2 is 1 not -1 and -0.1^2 is 0.01 not -0.01. Thank you. Kevin ______________________________________________ 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 01/03/2009 02:32 PM rkevinburton at charter.net wrote:> I had a question about the basic power functions in R. > > For example from the R console I enter: > > -1 ^ 2 [1] -1 > > but also > > -1^3 [1] -1 > > -0.1^2 [1] -0.01 > > Normally pow(-1, 2) return either -Infinity or NaN. Has R taken over > the math functions? If so I would think that -1^2 is 1 not -1 and > -0.1^2 is 0.01 not -0.01. > > Thank you. > > KevinKevin, See R FAQ 7.33 Why are powers of negative numbers wrong? http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-are-powers-of-negative-numbers-wrong_003f For the issue of returning -Inf or NaN, I suspect that you are thinking about negative numbers being raised to non-integer powers. For example, taking into consideration the enlightenment in the above FAQ:> (-1) ^ (1 / 2)[1] NaN> (-2) ^ (1/2)[1] NaN> sqrt(-2)[1] NaN Warning message: In sqrt(-2) : NaNs produced> (-2) ^ 2.5[1] NaN HTH, Marc Schwartz
rkevinburton at charter.net wrote:> I had a question about the basic power functions in R. > > For example from the R console I enter: > > -1 ^ 2 > [1] -1 > > but also > > -1^3 > [1] -1 > > -0.1^2 > [1] -0.01 > > Normally pow(-1, 2) return either -Infinity or NaN. Has R taken over the math functions? If so I would think that -1^2 is 1 not -1 and -0.1^2 is 0.01 not -0.01. >(-1)^2 # 1, not -1 it's similar to the case 1:3-1 1:(3-1) it's about operator precedence. vQ