Post Reply 
Unary minus precedence preference
07-24-2014, 01:35 PM
Post: #25
RE: Unary minus precedence preference
(07-24-2014 07:31 AM)Wes Loewer Wrote:  
(07-23-2014 08:27 PM)Wes Loewer Wrote:  Well, except for that pesky Excel again. It evaluates 2^3^2 to be 64 instead of 512.

In fact, the only spreadsheets that I've found so far that give 512 are Google Sheets and Gnumeric on Linux. Gnumeric is even kind enough to automatically insert parentheses to explicitly show 2^(3^2) as well as (-2)^2.

So, a few spreadsheets do -2^2 correctly and a few do 2^3^2 correctly, but I'm not aware of any spreadsheets that do both of these operations correctly.

It's sad that developers are so careless.
Here's a quote from the XLS format specification:
Quote:With the exception of control tokens (section 2.2.2.3), display tokens (section 2.2.2.4), and mem
tokens (section 2.2.2.5) that are described in subsequent sections, parsed expressions are stored in
Rgce (section 2.5.198.104) using Reverse-Polish notation.

So it's clear that Excel uses RPN to store and evaluate expressions. This means that OpenOffice, Gnumeric, etc. could choose the other convention without breaking compatibility with existing spreadsheets (since existing expressions are pre-compiled to RPN, there's no ambiguity!!). They could simply open an old spreadsheet written with Excel and the same formula would display (-2)^2 under the "correct" convention, but will always give the same result of 4. When the user types -2^2 on a new formula, give -4. When you go back to Excel and open that file, Excel will display -(2^2) (under the "wrong" convention) and keep giving the -4 result you got before. Nothing really breaks, so I don't understand their reasoning.
Same thing for the associativity of exponents.

Claudio
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Unary minus precedence preference - Claudio L. - 07-24-2014 01:35 PM



User(s) browsing this thread: 1 Guest(s)