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-helpPLEASE do read the posting guide!

http://www.R-project.org/posting-guide.html