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
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
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
In cases like this it really is worth while writing
(a:b)-1 or a:(b-1)
according to which you mean, since (a:b-1) really is
> please excuse me if this ones a basic error
>  -0.6471718
>  NaN NaN NaN
> am I missing something important in my basic math?
Non-integer powers of negative numbers don't work (well, they are complex
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.
Arithmetically this makes perfect sense, syntactically I'm not sure it does.
> z == -0.7
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.
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
 NaN NaN NaN
am I missing something important in my basic math?