Awkward behaviour of LO Calc

Hello to all,

I have found a fancy behavior of Calc but I cannot decide whether it is
a bug or a "feature". Hence I was looking for your advice before posting
a bug report (if required).

  1. Open LO Calc
  2. In A1 enter formula "=1-2^2" and press enter key
     The result shown is -3 which is ok since
     1-2² = 1-(2²) = 1-4 = -3
  3. In A2 enter formula "=-2^2" and press enter key
     I would expect this to be treated as -2² = -(2²) = -4
     But Calc considers it as (-2)² = +4
     Quite obviously, a work around is to define the formula as
     "=-(2^2)" which indeed returns -4

I've checked the wiki page dedicated to operators [1] but it does not
give much information on operator precedence.

For me, either we should have both 1-2^2 = 1+4 = 5 and -2^2 = +4
Or we should have both 1-2^2 = 1-4 = -3 and -2^2 = -4

However, perhaps some of you have a different view point on the matter ?
In other words, is that a bug or is it perfectly normal ?

Of course changing such behavior will cause a hell lot of trouble in
existing documents… but perhaps the operator precedence should be
explicitly specified on the wiki ?

Tested both on LO 5.1.0.3 and on LO 4.2.8.2 (build ID 420m0(Build:2)).

Cheers,

Gaël Lorieul

[1] https://help.libreoffice.org/Calc/Operators_in_Calc

It looks as though someone has been following a bad example.

A quick search brings up
https://en.wikipedia.org/wiki/Order_of_operations

which says

"There exist differing conventions concerning the unary operator − (usually read "minus"). In written or printed mathematics, the expression −3^2 is interpreted to mean 0 − (3^2) = −9 .... but in .... Microsoft Office Excel (and other spreadsheet applications) and the programming language bc, unary operators have a higher priority than binary operators"

Contrary to, eg, perl.

Ouch! Quite contrary to expectation. Here lie monsters :slight_smile:

Hi,

lorieul schrieb:

Hello to all,

I have found a fancy behavior of Calc but I cannot decide whether it is
a bug or a "feature". Hence I was looking for your advice before posting
a bug report (if required).

That is good. It is https://bugs.documentfoundation.org/show_bug.cgi?id=37271

   1. Open LO Calc
   2. In A1 enter formula "=1-2^2" and press enter key
      The result shown is -3 which is ok since
      1-2² = 1-(2²) = 1-4 = -3
   3. In A2 enter formula "=-2^2" and press enter key
      I would expect this to be treated as -2² = -(2²) = -4
      But Calc considers it as (-2)² = +4
      Quite obviously, a work around is to define the formula as
      "=-(2^2)" which indeed returns -4

I've checked the wiki page dedicated to operators [1] but it does not
give much information on operator precedence.

From my standpoint, both examples show an inconsistent behavior of Calc.
For me, either we should have both 1-2^2 = 1+4 = 5 and -2^2 = +4
Or we should have both 1-2^2 = 1-4 = -3 and -2^2 = -4

A minus can be a binary operator subtraction or a unary operator sign.
In "=1-2^2" there is a operand left and right of the minus, so it is a binary operator. This means calculate its operands and then subtract.
In "=-2^2" there is no operand left. Therefore the minus can only be a unary operator, which means, it is a sign of the number. So Calc takes the number -2 and puts it to the power 2.

However, perhaps some of you have a different view point on the matter ?
In other words, is that a bug or is it perfectly normal ?

It is no bug, but a bad UI, because pupils are thought, that -2² is -4. One solution to fit common understanding to the formal term interpretation you can see in Gnumeric. It simple does not allow it. When the user enters =-2^2 it automatically adds brackets, so that it becomes (-2)^2 and the users knows the meaning unambiguous.

To force a result of -4 you need to tell Calc, that the minus is a binary operator. The formula "=0-2^2" would do that.

Of course changing such behavior will cause a hell lot of trouble in
existing documents… but perhaps the operator precedence should be
explicitly specified on the wiki ?

The precedence is defined in ODF http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part2.html#Operators

Kind regards
Regina

Mike Scott wrote:
>
> A quick search brings up
> https://en.wikipedia.org/wiki/Order_of_operations
>
> which says
>
> "There exist differing conventions concerning the unary operator −
> (usually read "minus"). In written or printed mathematics, the
> expression −3^2 is interpreted to mean 0 − (3^2) = −9 .... but in ....
> Microsoft Office Excel (and other spreadsheet applications) and the
> programming language bc, unary operators have a higher priority than
> binary operators"
>
> Contrary to, eg, perl.
>
> Ouch! Quite contrary to expectation. Here lie monsters :slight_smile:

The ODF specification explicitly says that the unary - has higher precedence than ^, which is compatible with Excel. So it is not a bug.

...

To force a result of -4 you need to tell Calc, that the minus is a
binary operator. The formula "=0-2^2" would do that.

Of course changing such behavior will cause a hell lot of trouble in
existing documents… but perhaps the operator precedence should be
explicitly specified on the wiki ?

The precedence is defined in ODF
http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part2.html#Operators

Doubtless so.

But of little comfort to the hapless user, expecting a more mathematical convention:

mike@spock> perl -e 'print -3**2, "\n"'
-9

Oh well. Caveat programmer, I suppose.

Unless I'm misremembering my maths -2² would be +4 and -(2²) would be -4

So in my mind no bug.

Alex

Unless I'm misremembering my maths -2² would be +4 and -(2²) would be -4

So in my mind no bug.

Just a matter of convention. Unfortunately where there's one, there's another.......

It seems that perl, python and php differ. Perl and python I've tried and it's so, php's manual puts exponentiation at higher precedence than unary - (http://php.net/manual/en/language.operators.precedence.php) although the ** operator is new in php 5.6 so I can't check offhand.

eg
mike@spock> python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print -3 ** 2
-9

Excel is documented as Calc's order, ie the opposite.

Javascript seems to agree with Excel.
(https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Operator_Precedence)

Fortran (remember that?) puts - below **
(https://www.fortran.com/F77_std/rjcnf0001-sh-6.html)
that page says explicitly
"For example, in the expression

- A ** 2

the exponentiation operator (**) has precedence over the negation operator (-) .... The interpretation of the above expression is the same as the interpretation of the expression

- (A ** 2)
"

Oddly, I can't find any strictly /mathematical/ guideline! Anyone have the maths students' guide to operator precedence?

It looks as though opinion is divided.

I'd suggest always using brackets to be sure and clear. In any case, it really needs documenting for Calc.

....

Excel is documented as Calc's order, ie the opposite.

And MS are inconsistent - VB puts exponentiation above unary -
(https://msdn.microsoft.com/en-us/library/fw84t893.aspx)
opposite to Excel. Oh well :slight_smile: