Issue 75620 - Order of Operations Error
Summary: Order of Operations Error
Status: CLOSED DUPLICATE of issue 24271
Alias: None
Product: Calc
Classification: Application
Component: programming (show other issues)
Version: OOo 2.0.4
Hardware: PC Linux, all
: P3 Trivial (vote)
Target Milestone: ---
Assignee: spreadsheet
QA Contact: issues@sc
URL:
Keywords: oooqa
Depends on:
Blocks:
 
Reported: 2007-03-22 01:48 UTC by mikkl
Modified: 2007-03-22 23:22 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
Latest Confirmation in: ---
Developer Difficulty: ---


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description mikkl 2007-03-22 01:48:42 UTC
The order of mathematical operations is incorrectly implemented in calc.  The
sequence should be Parenthesis, Exponents, Multiplication & Division, Addition &
Subtraction, all taken left to right.  By this order, -x^2 and -(x^2) are
equivalent expressions.  If x = -1, both terms should evaluate to -1.  In Calc,
the first term evaluates as 1 and the second term evaluates to -1.  The
fundamental error in this implementation is evident with the equation "-x^2 +
42".  By the commutative property of addition and subtraction "42 - x^2" and
"-x^2 + 42" should give the same results.  In calc, they do not.  In order to
assure the correct answer, the equation must be written as -(x^2) + 42.  These
additional parenthesis should not be required.

I would note that this incorrect handling of the order of operations is
consistent with Microsoft Excel--this is /not/ a product benefit (though it may
be appropriate to make this inaccurate behavior a switch for compatibility
reasons).  The implication of this error is that subtle mathematical errors will
propagate through complex worksheets unless the user/programmer takes great care
to heavily use parenthesis throughout all of their calculations.
Comment 1 andreschnabel 2007-03-22 10:05:27 UTC
closing invalid, as the given list of operators is missing unary operators, like
the minus sign. These have to be the first in the sequence of calculation.

So: 
so -x^2 is equivalent to (-x)^2 as the - is a unary operator.
but y-x^2 is equivalent to y-(x^2) as the - is a subtraction operator.


Comment 2 andreschnabel 2007-03-22 10:05:52 UTC
closed
Comment 3 frank 2007-03-22 10:34:35 UTC
You may have a look at Issue 24271.

A single minus sign in front of a number is nothing else than a negation of this
number as it can't be an operator. The BODMAS rule just applys to operators not
notations.

Frank
Comment 4 mikkl 2007-03-22 12:08:39 UTC
I respectfully disagree with the responses.  Negation is the multiplication by
-1. In programming terms, it is a unary operator. Precedence rules in many of
the high level languages (Fortran, C, C++, C#, Perl) are common (see
ECMA-334:14.2.1) and would provide calculations more consistent with the
everyday user while being less likely to result in subtle misunderstandings and
incorrect calculations.

Using a precedence order different than most common programming languages and
different from users expectations and training (high school Algebra) is
unnecessary in my opinion and detracts from Calc.  Again, I understand the need
to be Excel compatible.  In the 'best' world, this *feature* could be turned off
and a more standard precedence order could be used for calculations.
Comment 5 andreschnabel 2007-03-22 12:28:43 UTC
reopen, as this should rather be closed as duplicate to issue 24271
Comment 6 andreschnabel 2007-03-22 12:29:37 UTC
set duplicate

*** This issue has been marked as a duplicate of 24271 ***
Comment 7 frank 2007-03-22 12:34:29 UTC
closed double
Comment 8 andreschnabel 2007-03-22 12:37:15 UTC
closed again.

@mikkl: can you read through Issue 24271 and issue 66735 and see, if some of the
proposals (esp. 667359 are usefull? If yes, pleas vote for those issue(s).
Comment 9 mikkl 2007-03-22 23:22:50 UTC
@andreschnabel - thank you for pointing out the other two reports.  I had
searched for cases before posting my issue but was not successful in finding
them.  I agree that a default insertion of the necessary parenthesis would help
clarify the math that is being performed and help to avoid problems debugging
spreadsheets while maintaining compatibility with Excel.  I will go over and
vote for those issues.