Multiplication (PR#8466)

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Multiplication (PR#8466)

casella
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: [hidden email]
P.O. Box 118545
Gainesville, FL 32611-8545

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Multiplication (PR#8466)

Thomas Lumley
On Fri, 6 Jan 2006, [hidden email] 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
[hidden email] University of Washington, Seattle

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Multiplication (PR#8466)

Gabor Grothendieck
On 1/6/06, Thomas Lumley <[hidden email]> wrote:

> On Fri, 6 Jan 2006, [hidden email] 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Multiplication (PR#8466)

Gabor Grothendieck
In reply to this post by casella
On 1/6/06, Thomas Lumley <[hidden email]> wrote:

> On Fri, 6 Jan 2006, [hidden email] 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Multiplication (PR#8466)

Roger J. Bos
How do people even notice stuff like this.  You would never hard-coding
(-2)^2 or -2^2 anyway. The part being squared would be a variable, in which
case it works correctly:

> a<- -2
> a
[1] -2
> a^2
[1] 4

Sometimes it seems that people go looking for bugs... and therefore see bugs
all around them.

On 1/6/06, [hidden email] <[hidden email]> wrote:

>
> On 1/6/06, Thomas Lumley <[hidden email]> wrote:
> > On Fri, 6 Jan 2006, [hidden email] 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Multiplication (PR#8466)

Gabor Grothendieck
Precedence rules are tricky, in general, and the usual
advice with most programming languages is to liberally use
parentheses when in doubt.  Its actually not that surprising
in this case but consider 0-1:3 and -1:3 which give different
results since one uses binary minus and the other uses
unary minus and the order of precedence from highest to
lowest is unary minus, : and binary minus.  If one used
parentheses in these cases it would be clear even without
detailed knowledge of the precedence rules (which likely
no one can remember anyways).

On 1/6/06, roger bos <[hidden email]> wrote:

> How do people even notice stuff like this.  You would never hard-coding
> (-2)^2 or -2^2 anyway. The part being squared would be a variable, in which
> case it works correctly:
>
> > a<- -2
> > a
> [1] -2
> > a^2
> [1] 4
>
> Sometimes it seems that people go looking for bugs... and therefore see bugs
> all around them.
>
> On 1/6/06, [hidden email] <[hidden email]> wrote:
> >
> > On 1/6/06, Thomas Lumley <[hidden email]> wrote:
> > > On Fri, 6 Jan 2006, [hidden email] 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
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Multiplication (PR#8466)

Peter Dalgaard
In reply to this post by Thomas Lumley
Thomas Lumley <[hidden email]> writes:

> On Fri, 6 Jan 2006, [hidden email] 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
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Multiplication (PR#8466)

Peter Dalgaard
In reply to this post by casella
Thomas Lumley <[hidden email]> writes:

> On Fri, 6 Jan 2006, [hidden email] 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
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Multiplication (PR#8466)

Brahm, David
In reply to this post by casella
While we're swapping precedence tales, can you guess what this gives:

> q <- TRUE
> 2 + !q + 3

It's "2", not "5".  Bit me in the arse just the other day :-)

-- David Brahm ([hidden email])

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Multiplication (PR#8466)

Hervé Pagès
In reply to this post by Thomas Lumley
Thomas Lumley wrote:

>On Fri, 6 Jan 2006, [hidden email] 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: [hidden email]
 Phone: (206) 667-5791
   Fax: (206) 667-1319

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Multiplication (PR#8466)

Hervé Pagès
In reply to this post by casella
Thomas Lumley wrote:

>On Fri, 6 Jan 2006, [hidden email] 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: [hidden email]
 Phone: (206) 667-5791
   Fax: (206) 667-1319

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Multiplication (PR#8466)

casella
In reply to this post by Hervé Pagès
Thanks

Herve Pages wrote:

> Thomas Lumley wrote:
>
>> On Fri, 6 Jan 2006, [hidden email] 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: [hidden email]
P.O. Box 118545
Gainesville, FL 32611-8545

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Multiplication (PR#8466)

casella
In reply to this post by casella
Thanks

Herve Pages wrote:

> Thomas Lumley wrote:
>
>> On Fri, 6 Jan 2006, [hidden email] 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: [hidden email]
P.O. Box 118545
Gainesville, FL 32611-8545

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel