From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>

Date: Thu 23 Feb 2006 - 03:59:19 EST

> [1] -0.6471718

*>
*

> [1] NaN NaN NaN

*>
*

*> am I missing something important in my basic math?
*

E-Mail: (Ted Harding) <Ted.Harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Feb 23 04:23:46 2006

Date: Thu 23 Feb 2006 - 03:59:19 EST

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

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) <Ted.Harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861

Date: 22-Feb-06 Time: 16:59:17 ------------------------------ XFMail ------------------------------ ______________________________________________R-help@stat.math.ethz.ch mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Feb 23 04:23:46 2006

*
This archive was generated by hypermail 2.1.8
: Thu 23 Feb 2006 - 06:23:11 EST
*