[R] cube root of a negative number

Berwin A Turlach berwin at maths.uwa.edu.au
Wed Oct 27 02:38:31 CEST 2010


G'day Bill,

On Wed, 27 Oct 2010 10:34:27 +1100
<Bill.Venables at csiro.au> wrote:

[...]
> It is no surprise that this does not work when working in the real
> domain, except "by fluke" with something like 
> 
> > -4^(1/3)
> [1] -1.587401
> > 
> 
> where the precedence of the operators is not what you might expect.
> Now that could be considered a bug, since apparently
> 
> > -4^(1/2)
> [1] -2
> 
> which comes as rather a surprise!
[...]

Mate, you must have been using Excel (or bc) too much recently if this
takes you by surprise. :)

This is exactly the behaviour that I would expect as I was always
taught that exponentiation was the operator with the highest priority.  

The discussion at
    http://en.wikipedia.org/wiki/Order_of_operations
points out that "there exist differing conventions concerning the unary
operator -" but gives only Excel and bc as examples of
programs/languages who give the unary operator the higher precedence.
Moreover,
	http://www.burns-stat.com/pages/Tutor/spreadsheet_addiction.html
points out that in Excel "the precedence of unary minus is different
than many other languages (including VBA which is often used with
Excel)".

Cheers,

	Berwin



More information about the R-help mailing list