Inconsistent formulas

P

Piotrek

Hi!

During development of my school project I have found a bug (in my opinion).
Writing formula 5-10^2 I am getting as expected -95, however changing order
of summation to -10^2 + 5 I am getting 105 (which is equivalent to (-10)^2 +
5 not as intended -(10^2) + 5). I think it should be unified - solving this
problem is not easy, becuase both formulas should give the same value.

Thanks
Piotrek
 
G

Gary''s Student

This is just a matter of convention. Excel follows algebraic parsing. This
means that exponentiation is performed first. Other possible conventions are
right-to-left or left-to-right. None of the conventions are right or wrong.
It is just a matter of picking a convention and following the rules.


By the way, most programming languages follow the same rules as Excel.
 
M

MartinW

Hi Piotrek,

-10^2 = -10*-10 = 100

-10*10 = -10*10 = -95

That's mathematics the way I was taught.

Regards
Martin
 
M

MartinW

Correction!! That should read.

-10^2 = -10*-10 = 100

-10*10 = -10*+10 = -100

Regards
Martin
 
C

CLR

I'm still a little "coffee challanged" this morning...........

-10*10 = -95 escapes me...........

Vaya con Dios,
Chuck, CABGx3
 
M

MartinW

Check the correction. I'm a little "late night challenged" here,
it's close to midnight. <g>

Regards
Martin
 
C

CLR

Right on Martin.........I was out getting another cup of coffee and missed
the correction.........Whew!......I thought I had missed something during the
"New Math" phase........lol

Vaya con Dios,
Chuck, CABGx3
 
J

JE McGimpsey

Conventions are rarely right or wrong, but they can be confusing.

In this case you're actually using two different operators, with two
different precedence (See "The order in which Microsoft Excel performs
operations in formulas" in XL Help).

In

=5-10^2

the "-" is the subtraction operator, which has two arguments (5, 10^2),
and a lower precedence than the exponentiation operator (^)

In

= -10^2 + 5

the "-" is the negation operator, which has one argument (10), and a
higher precedence than the exponentiation operator.
 
J

joeu2004

During development of my school project I have found a bug (in my opinion)..
Writing formula 5-10^2 I am getting as expected -95, however changing order
of summation to -10^2 + 5 I am getting 105 (which is equivalent to (-10)^2+
5 not as intended -(10^2) + 5).

Definitely not a bug! Use Excel Help to find to search for
"precedence", then click on "About calculation operators">"The order
in which Excel performs operations". You will see that unary minus
("-") has higher precedence than exponentiation ("^"), which has
higher precedence than subtraction (also "-"). By the way, that
precedence order is nearly universal for modern computer languages.
(I know of no exceptions.) Thus, it is correct for Excel to parse the
first form as 5 - (10^2) and the second form as (-10)^2 + 5.

Although I try to avoid excessive parentheses, when in doubt,
parenthesize your sub-expressions the way you intend the order of
operations to be performed.

Caveat: Beware of how the use of parentheses affects Excel's attempt
to mask some of the side-effects of binary computer arithmetic. For
example, 12.22 - 0.02 - 12.20 is different from (12.22 - 0.02 -
12.20). (And both are different from 12.22 - 12.20 - 0.02 -- which is
another story altogether ;-)
 
T

Tyro

The formulas are not at all inconsistent; they are producing the correct
results. 5 - 10 ^ 2 = -95. This is the same as saying (5) - (10^2). The
minus sign here is the subtraction operator. So, 5 - 100 = -95. The second
formula is -10 ^ 2 + 5 = 105. This is the same as saying ((-10) ^ 2) + 5.
The minus sign here is the unary operator, making the 10 negative. So 100 +
5 = 105. The unary operator in mathematics and computer languages has higher
precedence than exponentiation. You might want to review elementary algebra.
If you want the result of the second formula to be -95, you must override
the precedence order with parenthesis, as in:
-(10 ^ 2 ) + 5 which is -100 + 5 = -95.
 
J

JE McGimpsey

Well, not to be snide, but in the VBE Immediate window, enter

? -10^2

....

That's not a bug, either (see the "Operator Precedence" entry in VBA
Help).

Nobody ever said MS worried about consistency...
 
R

Rick Rothstein \(MVP - VB\)

I just got to this thread and was about to post the same thing. A similar
question about operator precedence was asked just yesterday over in one of
the "compiled VB" newsgroups and the fact that exponentiation (in VB/VBA)
has a higher precedence than the negation was posted back as an answer to
the OP there. It would not have occurred to me that in an Excel spreadsheet,
a different operator precedence might be in use. Nice going Microsoft.<g>

Rick
 
D

Dana DeLouis

higher precedence than subtraction (also "-"). By the way, that
precedence order is nearly universal for modern computer languages.

Hi. Just two cents. It has been discussed many times over the years in the
math & science groups that the proper interpretation of -10^2 is -100.
Almost all real math programs that I know return -10^2 as -100, and there
are certain reasons to do so.
It has also been discussed over the years, and the consensus is that in
Excel, = -10^2 is mathematically incorrect to return +100.
Most real Math programs interpret it as -1 * 10^2

I think one of the main problems is that Excel's interpreter can only read
Left to Right. It is not sophisticated enough to read Right to Left.
Excel's Help system doesn't help much when it says
- Negation (as in -1)

Negation is a term used with Logical values. Ie Not(True) = False
That term is also used in binary to switch 1 / 0.

What Excel's Help says of precedence is that when an equation starts out
with a minus sign ( - ) then that comes first. It's up to us to know that
Excel is not sophisticated enough, and that it does not follow correct
mathematical interpretation.

There are a few examples that show that Excel is very limited.
For example, suppose you were following a math book, and entered
=2^3^4.

We know that Excel can only read left to right, and should return the wrong
answer of 4096.
Which it does....
=2^3^4
4096

However, most real math programs will read this Right to left, and return
the mathematically correct answer:

2^3^4

2,417,851,639,229,258,349,412,352

Out of habit when using Excel for awhile, you will probably start changing
equations like
=-A1+B1
to
=B1-A1
just to avoid starting an equation with a minus sign.

Using ( ) is a good habit as others mentioned. When equations get to
complicated, using Power() is another good option:
Math programs uses this internally for clarity, and is probably a good
habit.

=-POWER(10,2)+5
 
S

Stan Brown

of summation to -10^2 + 5 I am getting 105 (which is equivalent to (-10)^2 +
5 not as intended -(10^2) + 5).

I agree with you, but Excel is working as designed. In the help topic
"About calculation operators" it is documented that unary minus has
higher priority than exponentiation. For example, -10^2 is equivalent
to (-10)^2.

Again, I think Excel is wrong about this, but strictly speaking it's
not a bug because the behavior is documented.

I knew that Microsoft was hostile to Internet standards and standard
programming languages, but this is the first time I've realized that
Microsoft also wants to overthrow the standard rules of arithmetic!
 
S

Stan Brown

Fri, 4 Jan 2008 02:33:01 -0800 from Gary''s Student
Excel follows algebraic parsing. This
means that exponentiation is performed first.

Gary, check the help topic "About calculation operators".
Exponentiation is *not* first: it comes after unary minus.

This is different from calculators such as the TI-83, and it's also
different from the ordinary conventions of algebra.
By the way, most programming languages follow the same rules as Excel.

I don't know about "most programming languages", but VBA doesn't
match Excel, and neither do C and C++. This little VBA macro:

Sub zonk()
Dim aa As Integer
aa = -10 ^ 2 + 5
MsgBox aa
End Sub

displays the algebraically correct -95, not the Excel worksheet's
silly 105.
 
S

Stan Brown

Fri, 4 Jan 2008 21:38:11 -0500 from Stan Brown
Fri, 4 Jan 2008 02:33:01 -0800 from Gary''s Student


I don't know about "most programming languages", but VBA doesn't
match Excel, and neither do C and C++.

Whoops! C and C++ don't have an exponentiation operator. I've been
away from programming longer than I thought!

But again, VBA gives exponentiation higher priority than unary minus,
and that matches algebraic convention. It's too bad that Excel is
different.
 
J

joeu2004

It has been discussed many times over the years in the
math & science groups that the proper interpretation of -10^2 is -100.
Almost all real math programs that I know return -10^2 as -100

In the first "real math program"ming language, IBM Mathematical
FORmula TRANslating System (FORTRAN), unary minus has precedence over
exponentiation.
I think one of the main problems is that Excel's interpreter can only
read Left to Right. It is not sophisticated enough to read Right to Left.

Precedence is not determined by the order in which an expression is
"read" (parsed). Operators associate either left or right; and any
parser can do either. For example, Excel has no problem interpreting
1 + 2 * 3 as 1 + (2*3). Likewise, Excel could just as easily
interpret - 10 ^ 2 as -(10^2) if it wanted to.

It is simply the choice of the MS Excel designers that unary minus has
higher precedence than exponentiation. Actually, it is probably the
designers of Visicalc who made that decision; the designers of Lotus
followed suit for compatibility reasons; and the Excel designers
followed Lotus for the same reason.
Excel's Help system doesn't help much when it says
- Negation (as in -1)

Negation is a term used with Logical values. Ie Not(True) = False
That term is also used in binary to switch 1 / 0.

Don't confuse a tech writer's use of terminology with the canonical
use. In the good ol' days, computer tech writers were experts in the
disciplines that they wrote about. Nowadays, the tech writer is
usually an English major who often has no in-depth knowledge of --
sometimes not even much experience with -- the subject. I have first-
hand knowledge of that fact.

I do not believe that "negation" or "complement" is used exclusively
in one discipline or the other. I don't know of any engineer who
describes the signal x-bar (that is, x with a line over it) as "the
negation of x"; it is the complement (or inverse) of x. On the
flipside, mathematicians universally refer to -x as "negative x". So
I can understand why someone might call the "-" symbol in that context
as "negation".

On the other hand, I would never refer to the binary "-" and "+" as
"subtraction" and "addition". Those are their operation; but the
symbols are "minus" and "plus". Similarly, I call the unary "-"
simply "unary minus".
For example, suppose you were following a math book, and entered
=2^3^4.

We know that Excel can only read left to right, and should return the
wrong answer of 4096.
[....]
However, most real math programs will read this Right to left, and return
the mathematically correct answer:
2^3^4
2,417,851,639,229,258,349,412,352

In a math text, you would find 2 (superscript) 3 (superscript) 4,
which is unambiguous. If you interpreted that as 2^3^4 instead of
2^(3^4), the error would be yours, not Excel's.

It is the same with any translation between two languages -- say
English and Chinese. If the translated text does not match the
intended meaning of the original text, the error is in the
translation. No one would say that one or the other language is
"wrong".

What you do not seem to grasp is the difference between ambiguous and
unambiguous representations. Computer language representation of
mathematical formulas -- at least what we are discussing here -- is
inherently ambiguous. We rely on rules and special syntax (e.g.
parentheses) to resolve those ambiguities. No one set of rules is
right or wrong.
 
J

Jerry W. Lewis

For computer programs, assumptions about operator precedence have always been
subject to "caveat emptor" ("let the buyer beware"), and parentheses should
be used liberally unless the user is certain of the order that particular
program will perform the operations. I recently used a programming language
that, consistent with its rules, evaluated 2*3+4*5 to be 50=((2*3)+4)*5
instead of 26=(2*3)+(4*5); if I had read its documentation on order of
operations, I would not have been caught by surprise.

Excel performs consistently with its published documentation about operator
precedence, so calling it "wrong" is questionable, although I agree that
giving unary negation a higher precedence than exponentiation is unusual (and
has resulting in some unwarranted published criticisms about Excel not being
able to handle some standard nonlinear least squares problems using Solver).

I can't recall ever seeing in mathematical literature an expression as
ambiguous as 2^3^4; it would usually be disambiguated by parentheses.
Waterloo Maple (a real Math program) will not evaluate it at all; version 11
returns a message "Error, ambiguous use of '^', please use parentheses",
earlier versions just said "Error, '^' unexpected".

Jerry
 
J

Jerry W. Lewis

Tyro said:
... The unary operator in mathematics and computer languages has higher
precedence than exponentiation.

If you look at a broader range of mathematical literature and computer
languages, I think you will find this to be very unusual operator precedence.
Unless you consider complex numbers, (-x)^y is not defined if y is not an
integer, and the '-' accomplishes nothing if y is even. That may be part of
the thinking behind the more common precedence (not followed by Excel) where
unary minus has lower priority than exponentiation.
... You might want to review elementary algebra.

For many reasons, it is best avoid snide remarks; not the least because of
the possibility that you might subsequently have to eat your own words.

Jerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top