# [R] Spreadsheet math problem (exponentiation)

David L Carlson dcarlson at tamu.edu
Fri Sep 18 17:04:33 CEST 2015

```Unfortunately the order of operations is not universal in computing. The real question is whether a program performs the way it is documented. Excel documents that unary operations take precedence over exponentiation and that within groups, the order is left to right. LibreOffice Calc behaves as Excel, but does not document the order of operations except to say */ before +-, left to right. I couldn't find any statement about the order of operations in the documentation for Gnumeric.

R documents that unary operations come after exponentiation and, within exponentiation, the order is right to left. Fortran puts unary operations with addition and subtraction after exponentiation with exponentiation right to left. C does not have an exponentiation operator, but unary operations come before multiplication and division.

When in doubt, use parentheses to make sure you get what you want.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of John McKown
Sent: Friday, September 18, 2015 9:31 AM
To: John Kane
Cc: r-help
Subject: Re: [R] Spreadsheet math problem (exponentiation)

On Fri, Sep 18, 2015 at 8:39 AM, John Kane <jrkrideau at inbox.com> wrote:

> It appears that at least three major spreadsheets, Excel, Apache
> OpenOffice Cal and gnumeric have a problem with the correct order of
> operations when dealing with exponents. The gnumeric result is very strange.
>
> This problem has probably been reported before but just in case it has
> not, it would appear to be one more serious problem with spreadsheets. It
> might be useful in warning people away from using a spreadsheet for serious
> analysis.
>
> Excel
>
> -2^2 = 4
>
> 2^2^3 = 64
>
> Apache OpenOffice
>
> -2^2 = 4
>
> 2^2^3 = 64
>

My opinion: One correct, one error!​ R agrees with me on this:
> 2^2
[1] 4
> 2^2^3
[1] 256
> 2^(2^3)
[1] 256
> -2^2
[1] -4
> (-2)^2
[1] 4
>

>
> gnumeric # note one correct, one error!
>

​My opinion: two correct!​

>
> -2^2 = 4
>
> 2^2^3 = 256
>
> John Kane
>
>
​Seems to be a bit off-topic. Unless your point to is to use R for
important work instead of some spreadsheet. A point with which I completely
agree!​

​MS-Excel, and Apache OpenOffice, appear to implement the above as
(2^2)^3==64. ​Whereas gnumeric implements appears to implement this as:
2^(2^3)==256. Which is "correct"? Depends on whom you ask.

ref: https://en.wikipedia.org/wiki/Order_of_operations
<quote>

If exponentiation is indicated by stacked symbols, the usual rule is to
work from the top down, thus:
[image: a^{b^c} = a^{(b^c)}],

which typically is not equal to [image: (a^b)^c]. However, some computer
systems may resolve the ambiguous expression differently. For example,
Microsoft
Office Excel <https://en.wikipedia.org/wiki/Microsoft_Office_Excel>
evaluates *a*^*b*^*c* as (*a*^*b*)^*c*, which is opposite of normally
accepted convention of top-down order of execution for exponentiation. If
a=4, p=3, and q=2, [image: a^{p^q}] is evaluated to 4096 in Microsoft Excel
2013, the same as [image: (a^p)^q]. The expression [image: a^{(p^q)}], on
the other hand, results in 262144 using the same program.
</quote>

​Gnumeric abides by the above definition. FWIW. BTW - MS-Excel also has
1900 as a friggin' leap year (due to Lotus 1-2-3 apparently), so I don't
consider MS-Excel (or anything else from MS for that matter) to be a
definitive source of correctness.​ Personal opinion. FSF associate member.
Penguinista.

--

Schrodinger's backup: The condition of any backup is unknown until a
restore is attempted.

Yoda of Borg, we are. Futile, resistance is, yes. Assimilated, you will be.

He's about as useful as a wax frying pan.

10 to the 12th power microphones = 1 Megaphone

Maranatha! <><
John McKown

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help