hi - in version 2.1 the command >-2^2 gives -4 as the answer. (-2)^2 is evaluated correctly. Cheers, George Casella -- George Casella Phone: (352) 392-1941 Ext. 204 Distinguished Professor and Chair Cell: (352) 682-7210 Department of Statistics Fax: (352) 392-5175 University of Florida Email: casella at stat.ufl.edu P.O. Box 118545 Gainesville, FL 32611-8545
On Fri, 6 Jan 2006, casella at stat.ufl.edu wrote:> hi - in version 2.1 the command > > >-2^2 > > gives > > -4 > > as the answer. (-2)^2 is evaluated correctly.So is -2^2. The precedence of ^ is higher than that of unary minus. It may be surprising, but it *is* documented and has been in S for a long time. -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle
On 1/6/06, Thomas Lumley <tlumley at u.washington.edu> wrote:> On Fri, 6 Jan 2006, casella at stat.ufl.edu wrote: > > > hi - in version 2.1 the command > > > > >-2^2 > > > > gives > > > > -4 > > > > as the answer. (-2)^2 is evaluated correctly. > > So is -2^2. The precedence of ^ is higher than that of unary minus. It > may be surprising, but it *is* documented and has been in S for a long > time.See ?Syntax
On 1/6/06, Thomas Lumley <tlumley at u.washington.edu> wrote:> On Fri, 6 Jan 2006, casella at stat.ufl.edu wrote: > > > hi - in version 2.1 the command > > > > >-2^2 > > > > gives > > > > -4 > > > > as the answer. (-2)^2 is evaluated correctly. > > So is -2^2. The precedence of ^ is higher than that of unary minus. It > may be surprising, but it *is* documented and has been in S for a long > time.See ?Syntax
Thomas Lumley <tlumley at u.washington.edu> writes:> On Fri, 6 Jan 2006, casella at stat.ufl.edu wrote: > > > hi - in version 2.1 the command > > > > >-2^2 > > > > gives > > > > -4 > > > > as the answer. (-2)^2 is evaluated correctly. > > So is -2^2. The precedence of ^ is higher than that of unary minus. It > may be surprising, but it *is* documented and has been in S for a long > time.Pretty much standard too, for languages that have an exponentiation operator. AFAICS Fortran, Perl, SAS all have ** at higher precedence than unary minus (or equal, but evaluate right to left). Stata seems like it might be the exception. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
While we're swapping precedence tales, can you guess what this gives:> q <- TRUE > 2 + !q + 3It's "2", not "5". Bit me in the arse just the other day :-) -- David Brahm (brahm at alum.mit.edu)
Thomas Lumley wrote:>On Fri, 6 Jan 2006, casella at stat.ufl.edu wrote: > > > >>hi - in version 2.1 the command >> >> >> >>>-2^2 >>> >>> >>gives >> >>-4 >> >>as the answer. (-2)^2 is evaluated correctly. >> >> > >So is -2^2. The precedence of ^ is higher than that of unary minus. It >may be surprising, but it *is* documented and has been in S for a long >time. > > > -thomas > >No, it's not surprising. At least to me... In the country where I grew up, I've been teached that -x^2 means -(x^2) not (-x)^2 ;-) H. -- ------------------------ Herv? Pag?s E-mail: hpages at fhcrc.org Phone: (206) 667-5791 Fax: (206) 667-1319
Thanks Herve Pages wrote:> Thomas Lumley wrote: > >> On Fri, 6 Jan 2006, casella at stat.ufl.edu wrote: >> >> >> >>> hi - in version 2.1 the command >>> >>> >>> >>>> -2^2 >>>> >>> >>> gives >>> >>> -4 >>> >>> as the answer. (-2)^2 is evaluated correctly. >>> >> >> >> So is -2^2. The precedence of ^ is higher than that of unary minus. >> It may be surprising, but it *is* documented and has been in S for a >> long time. >> >> >> -thomas >> >> > No, it's not surprising. At least to me... > In the country where I grew up, I've been teached that -x^2 means -(x^2) > not (-x)^2 ;-) > > H. >-- George Casella Phone: (352) 392-1941 Ext. 204 Distinguished Professor and Chair Cell: (352) 682-7210 Department of Statistics Fax: (352) 392-5175 University of Florida Email: casella at stat.ufl.edu P.O. Box 118545 Gainesville, FL 32611-8545