MS Office Forum / Excel / New Users / January 2008
Inconsistent formulas
|
|
Thread rating:  |
Piotrek - 04 Jan 2008 10:15 GMT 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
Wigi - 04 Jan 2008 10:33 GMT Hi
You should use
=-(10^2) + 5
as the square pertains to -10, not to 10.
 Signature Wigi http://www.wimgielis.be = Excel/VBA, soccer and music
> Hi! > [quoted text clipped - 6 lines] > Thanks > Piotrek Gary''s Student - 04 Jan 2008 10:33 GMT 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.
 Signature Gary''s Student - gsnu2007c
> Hi! > [quoted text clipped - 6 lines] > Thanks > Piotrek Stan Brown - 05 Jan 2008 02:38 GMT Fri, 4 Jan 2008 02:33:01 -0800 from Gary''s Student <GarysStudent@discussions.microsoft.com>:
> 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.
 Signature Stan Brown, Oak Road Systems, Tompkins County, New York, USA http://OakRoadSystems.com/ "If there's one thing I know, it's men. I ought to: it's been my life work." -- Marie Dressler, in /Dinner at Eight/
Stan Brown - 05 Jan 2008 02:45 GMT Fri, 4 Jan 2008 21:38:11 -0500 from Stan Brown <the_stan_brown@fastmail.fm>:
> Fri, 4 Jan 2008 02:33:01 -0800 from Gary''s Student > <GarysStudent@discussions.microsoft.com>: > > 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++. 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.
 Signature Stan Brown, Oak Road Systems, Tompkins County, New York, USA http://OakRoadSystems.com/ "If there's one thing I know, it's men. I ought to: it's been my life work." -- Marie Dressler, in /Dinner at Eight/
MartinW - 04 Jan 2008 13:09 GMT Hi Piotrek,
-10^2 = -10*-10 = 100
-10*10 = -10*10 = -95
That's mathematics the way I was taught.
Regards Martin
> Hi! > [quoted text clipped - 7 lines] > Thanks > Piotrek MartinW - 04 Jan 2008 13:17 GMT Correction!! That should read.
-10^2 = -10*-10 = 100
-10*10 = -10*+10 = -100
Regards Martin
CLR - 04 Jan 2008 13:27 GMT I'm still a little "coffee challanged" this morning...........
-10*10 = -95 escapes me...........
Vaya con Dios, Chuck, CABGx3
> Hi Piotrek, > [quoted text clipped - 18 lines] > > Thanks > > Piotrek MartinW - 04 Jan 2008 13:34 GMT Check the correction. I'm a little "late night challenged" here, it's close to midnight. <g>
Regards Martin
> I'm still a little "coffee challanged" this morning........... > [quoted text clipped - 26 lines] >> > Thanks >> > Piotrek CLR - 04 Jan 2008 13:44 GMT 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
> Check the correction. I'm a little "late night challenged" here, > it's close to midnight. <g> [quoted text clipped - 32 lines] > >> > Thanks > >> > Piotrek JE McGimpsey - 04 Jan 2008 15:18 GMT 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.
> Hi! > [quoted text clipped - 6 lines] > Thanks > Piotrek joeu2004 - 04 Jan 2008 16:31 GMT > 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 ;-)
JE McGimpsey - 04 Jan 2008 17:18 GMT 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...
In article <e5271c96-79a4-47be-9b34-ab3acfc877b4@s27g2000prg.googlegroups.com>,
> By the way, that precedence order is nearly universal for modern > computer languages. (I know of no exceptions.) Rick Rothstein (MVP - VB) - 04 Jan 2008 17:40 GMT 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
> Well, not to be snide, but in the VBE Immediate window, enter > [quoted text clipped - 12 lines] >> By the way, that precedence order is nearly universal for modern >> computer languages. (I know of no exceptions.) Dana DeLouis - 04 Jan 2008 23:44 GMT > 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
 Signature Dana DeLouis
>> During development of my school project I have found a bug (in my >> opinion). [quoted text clipped - 22 lines] > 12.20). (And both are different from 12.22 - 12.20 - 0.02 -- which is > another story altogether ;-) joeu2004 - 05 Jan 2008 05:58 GMT > 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. [quoted text clipped - 6 lines] > 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.
Jerry W. Lewis - 05 Jan 2008 12:36 GMT 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
> > higher precedence than subtraction (also "-"). By the way, that > > precedence order is nearly universal for modern computer languages. [quoted text clipped - 77 lines] > > 12.20). (And both are different from 12.22 - 12.20 - 0.02 -- which is > > another story altogether ;-) Tyro - 04 Jan 2008 17:00 GMT 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.
> Hi! > [quoted text clipped - 7 lines] > Thanks > Piotrek Jerry W. Lewis - 05 Jan 2008 13:03 GMT > ... 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
joeu2004 - 05 Jan 2008 14:49 GMT > 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. I have looked at a lot of mathematical literature in my lifetime, and I do not recall any that uses "^" for exponentiation. In fact, "^" is not even used universally in programming languages to mean exponentiation. Of course, "^" means bitwise XOR in C and its derivatives; it means "pointer to" in Pascal. In the first IBM Mathematical FORmula TRANSlating System (FORTRAN), "**" is used for exponentiation. In the original Dartmouth BASIC, an up-arrow was used for exponential. (Up-arrow was actually a key -- shift-N -- on ASR-33 teletypes, which did not have "^".)
(Admittedly, I am not familiar with specifically mathematical programming languages and libraries and their associated documentation, which might be what Jerry intended to refer to.)
In mathematics, exponentiation is represented by a superscripted expression. Of course, superscripting disambiguates the meaning of -x (superscript) y because of the convention in mathematics of applying the superscripted expression to the constant or variable immediately to the left. And I am sure that __that__ is the "thinking behind" giving exponentiation higher precedence than unary minus in those programming languages that do and that use "^" (or up-arrow), presumably being interpreted as "superscript".
Arguably, another possible explanation is that -x^y is mathematically equivalent to 0 - x^y. In that case, "^" almost universally has higher precedence over the binary "-", at least in programming languages that have precedence. But that reasoning would not readily lead to a reasonable interpretation of x^-y. On the contrary, using similar reasoning, that would have to be explained as x^(0 - y); and by extension, -x^y might then be explained as (0 - x)^y, which leads to the Excel interpretation. Klunk! Ergo, advocates of -(x^y) as the only "right" interpretation would do well to stick with the first (hieroglyphical) explanation above.
FYI, in Dartmouth BASIC, x^y^z (using "^" here to mean up-arrow) was interpreted as (x^y)^z, just as it is in Excel. (This comment is really in reference to a comment that Dana DeLouis made.) Interestingly, in Dartmouth BASIC, exponentiation was defined for only non-negative values. What I really mean is: x^y was defined to mean abs(x)^y. I'm sure the reason has more to do with the ease of implementation of the BASIC interpreter than with any mathematical principle ;-).
Stan Brown - 05 Jan 2008 02:33 GMT Fri, 4 Jan 2008 11:15:31 +0100 from Piotrek <pi_trek@wp.pl>:
> 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!
 Signature Stan Brown, Oak Road Systems, Tompkins County, New York, USA http://OakRoadSystems.com/ "If there's one thing I know, it's men. I ought to: it's been my life work." -- Marie Dressler, in /Dinner at Eight/
|
|
|