exponent confusion

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

exponent confusion

Tom Wright-9
please excuse me if this ones a basic error

> y<-c(-0.7,-0.6,-0.5)
> -0.7^1.22
[1] -0.6471718

> y^1.22
[1] NaN NaN NaN

am I missing something important in my basic math?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: exponent confusion

Prof Brian Ripley
On Wed, 22 Feb 2006, tom wright wrote:

> please excuse me if this ones a basic error
>
>> y<-c(-0.7,-0.6,-0.5)
>> -0.7^1.22
> [1] -0.6471718

?Syntax shows ^ has a higher precedance than -, so that is

-(0.7^1.22)

>
>> y^1.22
> [1] NaN NaN NaN
>
> am I missing something important in my basic math?
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

--
Brian D. Ripley,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: exponent confusion

Ted.Harding
In reply to this post by Tom Wright-9
On 22-Feb-06 tom wright wrote:

> please excuse me if this ones a basic error
>
>> y<-c(-0.7,-0.6,-0.5)
>> -0.7^1.22
> [1] -0.6471718
>
>> y^1.22
> [1] NaN NaN NaN
>
> am I missing something important in my basic math?

Ummm, not sure ... it depends where the explanation fits in.
It's certainly important, but whether it's "basic math" is
another question. And you're certainly missing it!

See

?Syntax

which outputs (initally)

 The following unary and binary operators are defined.  They are
     listed in precedence groups, from highest to lowest.

       '[ [['             indexing
       '::'               name space/variable name separator
       '$ @'              component / slot extraction
       '^'                exponentiation (right to left)
       '- +'              unary minus and plus
       ':'                sequence operator
       '%any%'            special operators
       '* /'              multiply, divide
       '+ -'              (binary) add, subtract
       '< > <= >= == !='  ordering and comparison
       '!'                negation
       '&  &&'            and
       '| ||'             or
       '~'                as in formulae
       '-> ->>'           rightwards assignment
       '='                assignment (right to left)
       '<- <<-'           assignment (right to left)
       '?'                help (unary and binary)

     Within an expression operators of equal precedence are evaluated
     from left to right except where indicated.

Therefore the binary unary operator "^" has precedence over the
unary (sign) operator "-" .

Hence your expression

  -0.7^1.22

is evaluated as

  -(0.7^1.22)

i.e. first do 0.7^1.22, then apply "-".

On the other hand, once you have created

  y<-c(-0.7,-0.6,-0.5)

then each element is already a negative number before you do
anything with it. Hence

  y^1.22 = c( (-0.7)^1.22, (-0.6)^1.22, (-0.5)^1.22 )
         = c(NaN, NaN, NaN)

As an alternative example, unary "-" has precedence over
binary "+" or "-", so that -2+3 is not -(2+3) = 5 but
(-2) + 3 (first apply unary "-", then do binary "+").

Computer languages always embody precedence rules such
as the above to resolve ambiguites in expressions such
as "-a^b" written without parenetheses; but then, in order
to get what you want you need to know the rules in order
to write such expressions correctly.

When in doubt use paraentheses!

A case which has often trapped  people in R (see many
places in the r-help archive) is a sequence expression
such as

  a<-1
  b<-10
  (a:b-1)

which for many is an optical illusion tempting them to
think it is a:(b-1) whereas is is in fact (a:b)-1, i.e.

  0, 1, 2, 3, 4, 5, 6, 7, 8, 9

and not

  1, 2, 3, 4, 5, 6, 7, 8, 9

(see the precedences of binary "-" and the sequence
operator ":" above -- i.e. first do ":" and then the
binary "-").

In cases like this it really is worth while writing
the parantheses:

  (a:b)-1   or   a:(b-1)

according to which you mean, since (a:b-1) really is
visually deceptive.

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 22-Feb-06                                       Time: 16:59:17
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: exponent confusion

Thomas Lumley
In reply to this post by Tom Wright-9
On Wed, 22 Feb 2006, tom wright wrote:

> please excuse me if this ones a basic error
>
>> y<-c(-0.7,-0.6,-0.5)
>> -0.7^1.22
> [1] -0.6471718
>
>> y^1.22
> [1] NaN NaN NaN
>
> am I missing something important in my basic math?
>

Yes.

Non-integer powers of negative numbers don't work (well, they are complex
numbers)

The first example appears to work but only because -0.7^1.22 is
-(0.7^1.22) not (-0.7)^1.22. See help(Syntax) for operator precedence in
R.  This does keep confusing people, and perhaps should be a FAQ, but it
is fairly standard in programming languages.


  -thomas

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: exponent confusion

Phineas Campbell
In reply to this post by Tom Wright-9
> -0.7^1.22
[1] -0.6471718

(-0.7)^1.22
>NaN

Arithmetically this makes perfect sense, syntactically I'm not sure it does.

>z<-c(-0.7)
> z == -0.7
[1] TRUE
> z^1.22
[1] NaN


I remember a programming homily: if you are unsure of the operator
precedence then you shouldn't assume the person who has to maintain your
code has any better knowledge so you should make the order in which you want
expressions to be evaluated explicit.

Phineas



-----Original Message-----
From: [hidden email]
[mailto:[hidden email]]On Behalf Of tom wright
Sent: Wednesday, February 22, 2006 10:14 AM
To: R-Stat Help
Subject: [R] exponent confusion


please excuse me if this ones a basic error

> y<-c(-0.7,-0.6,-0.5)
> -0.7^1.22
[1] -0.6471718

> y^1.22
[1] NaN NaN NaN

am I missing something important in my basic math?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html