Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Order of Operations Conventions

Status
Not open for further replies.
DerStrom8
How does that solve the conundrum that PEMDAS presents? As I currently understand it, applying PEMDAS, -2^2 = -4
However, (a-b)^2 when expanded is a^2 -2ab + b^2. "0-b" is of the same form as (a-b), so (0-b)^2 = b^2, not -b^2.

John
 
Unary operators are applied first. PEMDAS or BODMAS in UK is a guide for use in school. -2 squared is very different to 0-2 squared.

Edit, another way to think of it is that -2 is an member of the integers, a number in it's own right and not 0-2.

Mike.
 
Libre Office does it the Excel way -2^2 is = -4 . Some programming languages evaluate strictly right to left, No parens. I don't have an example off the top of my head.
 
DerStrom8
How does that solve the conundrum that PEMDAS presents? As I currently understand it, applying PEMDAS, -2^2 = -4
However, (a-b)^2 when expanded is a^2 -2ab + b^2. "0-b" is of the same form as (a-b), so (0-b)^2 = b^2, not -b^2.

John

The argument I am making, which is supported by the mathematics professor who wrote in the link I posted, is that -2^2 does not equal (0-2)^2. It equals 0-2^2.

Unary operators are applied first. PEMDAS or BODMAS in UK is a guide for use in school. -2 squared is very different to 0-2 squared.

Edit, another way to think of it is that -2 is an member of the integers, a number in it's own right and not 0-2.

Mike.

The statement in your quote that I made bold is exactly what I am contesting. In general mathematics, unary operators should not be applied first.
 
The number -2 is not 2 with a unary operator, it is an integer in it's own right and can't be treated as anything else.

Out of curiosity, using PEMDAS, how would 25/5*2 be worked out? As 25/10 or as 12.5*2?

And, as appears on facebook, what is 3-3*6+2?

Mike.
Edit, I now see what you mean, -x^2 is -(x^2) and if x=2 will equal -4. However, -2 is not the same as -x. It's kinda weird that I never saw the difference before and just automatically did it without further thought. The more I read the more it appears that parentheses are needed or the answer is -4. Looks like this has been adopted since I left school, I guess for clarity. I wonder how 5+-2^2 is calculated these days as the -2 is a negative number?
 
Last edited:
Graph says 4. EXCEL on the other hand says -4!! So does Matlab, so as AUS Dave would say 'A trap for young players!'.
Interesting stuff!
Can you show your work for that?

Here is my test on Excel 2007:
upload_2016-2-24_4-22-25.png

The J column simply has a -2 entered as shown in the fx line.

That version of Excel follows the old rules, -2^2 = 4, regardless of whether the negative number is calculated (presuming Excel might add parentheses) or entered directly.

John
 
The number -2 is not 2 with a unary operator, it is an integer in it's own right and can't be treated as anything else.

I do see your point of view, and I suppose there is no valid argument against your view or mine. I always learned that negation was a unary operator, and thus -2 is a 2 with negation applied. That being said, -2 is also its own integer, though it is defined as 2 below 0. I can see it both ways.

Out of curiosity, using PEMDAS, how would 25/5*2 be worked out? As 25/10 or as 12.5*2?

Multiplication and division are applied left-to-right, as are addition and subtraction. 25/5*2, you do the division first because it is the first operation you reach. The answer to 25/5*2 would be 10. A problem such as 4+12/2*5 would be 4+(12/2)*5 --> 4+(6*5) = 34.

And, as appears on facebook, what is 3-3*6+2?

Any mathematician would agree that 3*6 must be applied first, then the subtraction (it is furthest to the left), then addition. The answer to 3-3*6+2 is definitely -13: 3-18+2 = -15+2 = -13

Edit, I now see what you mean, -x^2 is -(x^2) and if x=2 will equal -4. However, -2 is not the same as -x. It's kinda weird that I never saw the difference before and just automatically did it without further thought. The more I read the more it appears that parentheses are needed or the answer is -4. Looks like this has been adopted since I left school, I guess for clarity. I wonder how 5+-2^2 is calculated these days as the -2 is a negative number?

It is strange, for sure. I suppose the takeaway for this would be to use as many parentheses as you need to make your question clear. Like I said, I see -2 as being the same as -x where x is 2, but I can also see it from the point of view that the -2 is its own integer. I think it comes down to the definition, but I believe the commonly accepted rule is the one I stated earlier. Negation should be applied before multiplication but certainly not before exponentiation.
 
I think we have wondered a smidge off topic! how can you get from a how do you in latex to a discussion on maths! I only have one thing to say about this in my thread :D

getout.gif
 
Can you show your work for that?

Here is my test on Excel 2007:
View attachment 97703
The J column simply has a -2 entered as shown in the fx line.

That version of Excel follows the old rules, -2^2 = 4, regardless of whether the negative number is calculated (presuming Excel might add parentheses) or entered directly.

John

That is what I was saying though, Excel does not follow the standard conventions for negative numbers. Microsoft has acknowledged that fact.
 
You were quick lol, real quick! You eating all your veg up ;)
 
Only if "minus 2" is defined as "-2" with parentheses. Once again, use the order of operations (PEMDAS).

PEMDAS is an acronym for the order of arithmetic operations with binary operators (binary meaning quantity of 2; not as in base 2). It does not apply to unary operators. There are special rules in that case, as I mentioned in post #6. Google " unary operations" for more information.

Note that the negative sign in -2 is a unary operator, as it has only one operand. In Boolean logic, NOT is also a unary operator.
 
PEMDAS is an acronym for the order of arithmetic operations with binary operators (binary meaning quantity of 2; not as in base 2). It does not apply to unary operators. There are special rules in that case, as I mentioned in post #6. Google " unary operations" for more information.

Note that the negative sign in -2 is a unary operator, as it has only one operand. In Boolean logic, NOT is also a unary operator.

See my later posts. I discuss the priority of unary operators compared to the binary operators used in PEMDAS.
 
For that reason I don't put much trust in what is returned from Excel.
From time to time I have had some really weird results in Excel which I could not explain. Maybe it was the order of operations thing.

One lesson to take away from this discussion is that it is important to remove all ambiguity when writing mathematical expressions which have to be interpreted by others.

On a similar theme, units, be aware to make sure that units are correctly stated.

Speaking of units, I bought a car and on the way home with it I thought "this has some strange handling characteristics". Later that day when checking it over and looking at the tyre pressures which should have been 29psi (2.0 Bar), the tyres on one side of the car were 20psi and on the other side 42psi (2.9 Bar).
Between the initial test drive and me taking the car home, it had been "serviced". Luckily the rest of the work had been done correctly.

JimB
 
Hello again,

Note i said hello 'again' even though i did not reply to this thread yet, and that is because this topic comes up now and then so it's sort of a repeat :) I dont mind that but just thought i would mention it.

There are some fine details we have to recognize, and these can vary depending in the context.
For example, in pure mathematics -2^2 would be taken as -(2)^2 which of course equals -4.. That's because the exponential operation has priority over subtraction, and there is simply no unary operator that negates a number with high priority. The unary operator really came in with programming and calculators. So the discussion of any unary operator significance should really be limited to calculators and programs because in pure mathematics -2^2 should be taken as 0-2^2 or say A-2^2 where A goes to zero. Note we have no problem with 5-2^2, it's only when the -2 stands alone.
There are negative numbers like: -2, -3, -456, etc., and we also have no problem with that, but these are taken to stand independently as a single entity not a combination of operations. For example, for the variables a, b, c, either can be 3,4,6, or -3, -4, -6 for example, and a^2 will be the square of whatever is held by a. No problem there either.
If you think about it, if it was not this way then we would have no end to the ambiguity. For example, we interpret -2^2 as -(2)^2 but do parens really clear this up? If the minus sign has high priority, who is to say that the parens have precedence? This would be the same with -(((2)))^2. If the parens or exponent have lower priority than the minus sign, then we might still end up with (-2)^2=4. And the only way to make -2^2 equal 4 is to force the exponential sign have less priority than the minus sign.

So pure mathematics would tell us that -2^2 is really 0-2^2 which would be 0-4, but context has a lot to do with it which is next up.

Context changes things because we see a flow of operations and how they evolve. We dont see that in a single statement like -2^2.
For example, in a fictitious problem we are solving we have the following...
Statement #1: "We have 5-7=-2"
Statement #2: "We then have to square that: -2^2=4"
In this fictitious problem we were able to follow the evolution of the result from start to finish so we see that it does make sense even though not mathematically perfectly correct.
Also, who here has not seen the completely ridiculous, "cos wt" instead of the correct, "cos(w*t)".


Next up is programming (and programs).

In programming there are a number of possibilities depending on the language and even who write the compiler. It's almost impossible to know what convention they followed without doing some experiments. Try 0-2^2 and then try -2^2 and see what becomes of it. Note that the compiler has to 'interpret' the intention and that comes from what the programmer of the compiler though was right.

Next up is the calculator.

These things vary even more. I have had calculators that do -2^2 one way, and some that do it the other way. So depending on what calculator i use, i either get 4 or -4. This is just like many other things in life where technology does not follow the law of the land that was established long before it got there.
Another interesting note is that when calculators use a unary minus sign as well as a subtraction sign, the unary one is usually smaller and sometimes even a little higher up graphically in the line. This isnt always the case however, as in Windows 7 the calculator does have a plus and minus key, but does not show a different graphical symbol than that for the subtraction sign.

In my personal experience i had to write a number of number cruncher algorithms, especially before around year 1995 or thereabouts. Some where done in high level languages like C, C++, Pascal, and others in raw ASM. In either case i noted that the only way to handle the minus sign was to have two different versions: one for subtraction and one for the unary operator. That solved any ambiguities right off the bat.
Many modern calculators have this 'new' key now too, one for actual subtraction and one for negative numbers like -2. If you use the unary minus sign for -2 and square it, you get 4. If you do a subtraction such as 5-2^2 you get 5-4=1.
In the actual programming phase, the different symbols are given a priority, and that is usually in the form of a table such as this short one:
">", 5
"<", 5
"^", 4
"*", 3
"/", 3
"+", 2
"-", 2

This tells the interpreter part of the program that addition should be performed only after multiplication for example.
If we had a unary minus sign the table would change to:
">", 6
"<", 6
"-", 5 (unary)
"^", 4
"*", 3
"/", 3
"+", 2
"-", 2 (subtraction)

and note that now the unary operator has higher priority than exponential, but still lower than comparisons. So with the unary operator -2^2 would yield 4, but with the subtraction operator 0-2^2 would yield -4.

This kind of thing doesnt only happen in math with the minus sign, but i could link to some experiments done by professors who did videos to try to prove some theory in physics in their video which from the video itself you can not determine if they proved it or not, and this is because they left out one or more important details. I believe it is because they knew it so well that they did not realize that they were not showing something critical to the proof. The details and the context are most important. If the author had some preference for some physical convention then we could probably apply that to other articles done by the same author, but with only one statement like -2^2 it is just too open to interpretation.

So in summary, pure math has exact rules to follow, but everything else including human written math problems can vary, so the best rule is, "USER BEWARE". :)

Button: [Disagree with the disagreement of this post] :)
 
Last edited:
JimB
Cute incident. Obviously an American worked on one side using a Brit's gauge and a Brit worked on the other side used an American's gauge. Seems that a little workplace organization would cure that.

Regarding references to Excel, over the years that Excel has existed, it is possible that the order of operations or the interpretation of that order has changed. Please give the version or approximate version being used. As mentioned above, I use the 2007 Office version.

In post #11, this is said: "Graph says 4. EXCEL on the other hand says -4!! " I am confident the 2007 Office version does not do that. It would me important to me if someone can confirm that later versions come up the negative square. It woud cerainly be a good reason not to update.

A program that calculates the square of a negative number as the negative of the square would be an absolute disaster for someone in a laboratory calculating the "root mean square" of analytical results who did not know the program was doing that:
upload_2016-2-25_4-54-41.png

(x = signed deviation from the mean)

Some might argue you can just use the absolute value of the deviations from the mean; however, it was always my practice to review the raw data and do a simple "sign test" in my head before accepting the RMS parameter was valid.

John
 
One lesson to take away from this discussion is that it is important to remove all ambiguity when writing mathematical expressions which have to be interpreted by others.

Which, at the end of the journey, it seems to me, simply boils down to use the adequate number of parenthesis in the adequate places. Isn't it? Even a calculator understands that.

BTW, note the time penalty for using extra parenthesis with RFOBasic! in my Samsung tablet to run 5000 times the following calculations (time spent in red - microseconds?):

val = byte1 + byte2 * 2 + byte3 *2 (920.0)

and then

val = (byte1) + (byte2 * 2) + (byte3 *2) (1424.0)

Does anyone care about that anyway?
 
Hello again,

Ha ha, somebody 'disagreed' with post #36. That's a lot to disagree with :)

Is there a button for disagreeing with the disagreement? (har har) :)
If so i would have to click it because i cant see somebody disagreeing with something with that much content without pointing out what it was they disagreed with.
 
Which, at the end of the journey, it seems to me, simply boils down to use the adequate number of parenthesis in the adequate places. Isn't it? Even a calculator understands that.
Exactly right!

BTW, note the time penalty for using extra parenthesis with RFOBasic! in my Samsung tablet to run 5000 times the following calculations (time spent in red - microseconds?):

val = byte1 + byte2 * 2 + byte3 *2 (920.0)

and then

val = (byte1) + (byte2 * 2) + (byte3 *2) (1424.0)

Does anyone care about that anyway?
Depending on the application, probably not.
However, if a program is taking up too much processor time, then to speed-up the execution time, removal of excess parentheses should be considered. But, the accuracy of the calculation must be thoroughly tested.

JimB
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top