Can someone please enlighten me as to why the following happens?> -2.7^8.6[1] -5125.407> p<- -2.7 > q<- 8.6 > p^q[1] NaN R seems perfectly able to calculate -2.7^8.6, but fails when the exact same values are assigned to variables and then the computation is repeated. Thanks in advance for any suggetsions. Kris.
On Tue, Jul 01, 2008 at 11:15:58AM -0700, poolloopus at yahoo.com wrote:> Can someone please enlighten me as to why the following happens? > > -2.7^8.6 > [1] -5125.407 > > > p<- -2.7 > > q<- 8.6 > > p^q > [1] NaN > R seems perfectly able to calculate -2.7^8.6, but fails when the exact same values are assigned to variables and then the computation is repeated. > Thanks in advance for any suggetsions.Try -(p^q) Dirk -- Three out of two people have difficulties with fractions.
poolloopus <at> yahoo.com <poolloopus <at> yahoo.com> writes:> > Can someone please enlighten me as to why the following happens? > > -2.7^8.6 > [1] -5125.407 > > > p<- -2.7 > > q<- 8.6 > > p^q > [1] NaN > R seems perfectly able to calculate -2.7^8.6, but fails when the exact samevalues are assigned to> variables and then the computation is repeated. > Thanks in advance for any suggetsions. > Kris.The problem is the grouping is not what you are expecting it is. Try (-2.7)^8.6 and p <- 2.7 q <- 8.6 p^q Mark
on 07/01/2008 01:15 PM poolloopus at yahoo.com wrote:> Can someone please enlighten me as to why the following happens? >> -2.7^8.6 > [1] -5125.407 > >> p<- -2.7 q<- 8.6 p^q > [1] NaN> R seems perfectly able to calculate -2.7^8.6, but fails when > the exact same values are assigned to variables and then the > computation is repeated. Thanks in advance for any suggetsions. Kris.You are not seeing what you think you are seeing in the first result. > -2.7^8.6 [1] -5125.407 is parsed in the same way as: > -(2.7^8.6) [1] -5125.407 In other words, it is parsed as: > 2.7^8.6 [1] 5125.407 and then negated. If you were to 'properly' define the precedence of operation, you would use: > (-2.7)^8.6 [1] NaN which is the same result you get when you use the vectors. 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 HTH, Marc Schwartz
Hi, the problem is what you want! With the -2.7^8.6 you are doing -(2.7^8.6) it exists... But, if you try (-2.7)^8.6 the R gives you NaN. When you define p=-2.7 and q=8.6 and do p^q you are doing that (-2.7)^8.6. If you write p=2.7 and q=8.6 and use the -p^q it will work! Leandro Marino www.leandromarino.com.br -----Mensagem original----- De: r-help-bounces em r-project.org [mailto:r-help-bounces em r-project.org]Em nome de Dirk Eddelbuettel Enviada em: terca-feira, 1 de julho de 2008 15:19 Para: poolloopus em yahoo.com Cc: r-help em r-project.org Assunto: Re: [R] WIERD: Basic computing in R On Tue, Jul 01, 2008 at 11:15:58AM -0700, poolloopus em yahoo.com wrote:> Can someone please enlighten me as to why the following happens? > > -2.7^8.6 > [1] -5125.407 > > > p<- -2.7 > > q<- 8.6 > > p^q > [1] NaN > R seems perfectly able to calculate -2.7^8.6, but fails when the exactsame values are assigned to variables and then the computation is repeated.> Thanks in advance for any suggetsions.Try -(p^q) Dirk -- Three out of two people have difficulties with fractions. ______________________________________________ R-help em 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.
This is FAQ 7.33 -thomas On Tue, 1 Jul 2008, poolloopus at yahoo.com wrote:> Can someone please enlighten me as to why the following happens? >> -2.7^8.6 > [1] -5125.407 > >> p<- -2.7 >> q<- 8.6 >> p^q > [1] NaN > R seems perfectly able to calculate -2.7^8.6, but fails when the exact same values are assigned to variables and then the computation is repeated. > Thanks in advance for any suggetsions. > Kris. > > ______________________________________________ > 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. >Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle