Society of Robots - Robot Forum

General Misc => Misc => Topic started by: Admin on March 19, 2011, 04:21:39 PM

Title: -3^2 = -9 .... about order of operations
Post by: Admin on March 19, 2011, 04:21:39 PM
There was an interesting thread on the SeattleRobotics group I thought very interesting.

It turns out:
-3^2 = -9
(-3)^2 = 9

Try it in your favorite math program like Google if you don't believe me :P

It turns out when you see this:
-3^2 = -9
it is assumed you meant:
(0) - (3^2) = -9

I had gone through highschool algebra and all through college wrongly thinking:
-3^2 = (-3)^2   <- this is wrong!

This is very very important if you're programming and don't want to get the order of operations wrong. I blame it on my highschool math teachers :P

For more detailed information:
http://mathforum.org/library/drmath/view/61633.html (http://mathforum.org/library/drmath/view/61633.html)
Title: Re: -3^2 = -9 .... about order of operations
Post by: Soeren on March 19, 2011, 04:44:37 PM
Hi,

3^2 is 3*3 so if you rewrite it as -3*3 it may be easier to see (knowing that * and / has priority over + and -).
Title: Re: -3^2 = -9 .... about order of operations
Post by: Soeren on March 19, 2011, 04:49:14 PM
If in the slightest doubt, just use plenty of parentheses/brackets to control the operations.
Title: Re: -3^2 = -9 .... about order of operations
Post by: Admin on March 19, 2011, 05:04:57 PM
3^2 is 3*3 so if you rewrite it as -3*3 it may be easier to see (knowing that * and / has priority over + and -).
I, and apparently many people, were taught incorrectly that -3^2=(-3)^2. Thats the confusion :P
Title: Re: -3^2 = -9 .... about order of operations
Post by: richiereynolds on March 19, 2011, 06:48:07 PM
That's what I was taught, in mathematical notation, however, in a programming language you have to look at the operator precedence rules for the language. There's no guarantee that the rules will follow anyone else's standard, they're publishing their own for that language so you have to follow that.
Title: Re: -3^2 = -9 .... about order of operations
Post by: Metal Slug 2 on March 20, 2011, 10:14:03 PM
Shouldn't the calculation simply follow the 'BEDMAS' (or any other valid permutation for the word, ex. 'BEMDSA') standard for operations? (brackets, exponents, division/multiplication, addition/subtraction).

The way I was taught was to treat a ' - ' sign as a -1 multiplication operation if confused with a question.
For example, the above problem: -3^2 could be expanded to: -1 * 3^2, following BEDMAS, the exponent is evaluated first: -1 * 9, which finally evaluates to -9.
For the next problem: (-3)^2, expanded becomes: (-1 * 3)(-1 * 3) or (-3)(-3), evaluating to +9.
Works in every circumstance. :)
Title: Re: -3^2 = -9 .... about order of operations
Post by: robots-in-brighton on March 21, 2011, 02:31:49 AM
Seems to be some confusion over precedence rules for math and precedence rules for the C language.

BEDMAS is fine but has to be treated carefully when applied to programming languages. Unary operators make things messy when parsing because they make the same symbol take different priority depending on context.

If I hadn't read the other discussions I would have happily parsed -3^2 as 'square the number minus three' even without parentheses. The minus sign binds tightly to the 3 to make it -3 before any other operators are considered, but that's in C. The rules may well be different in formal maths, I'd always follow the practice of piling on parentheses to remove any doubts.





Title: Re: -3^2 = -9 .... about order of operations
Post by: BANE on March 21, 2011, 03:29:41 AM
Quote
I'd always follow the practice of piling on parentheses to remove any doubts.
seconded; ive had issues with my TI-84 and C and parentheses around even the simplest operations can make a difference :P   
Title: Re: -3^2 = -9 .... about order of operations
Post by: Graynomad on March 21, 2011, 05:17:16 AM
It doesn't matter what rules something should or should not follow you never really know (or remember), the only reliable way to do this sort of maths is with a set of () for every operation except probably the outermost.

______
Rob
Title: Re: -3^2 = -9 .... about order of operations
Post by: Metal Slug 2 on March 21, 2011, 07:04:02 AM
It doesn't matter what rules something should or should not follow you never really know (or remember)
______
Rob

Unless of course, you look it up =P: this (http://en.wikipedia.org/wiki/Order_of_operations#Programming_languages).  It appears that a lot of languages inherit the C standard for order of operations...so that covers it for most of us.
Although I do agree with pasting on the brackets, but only when really necessary.  I'm sure any programmer can easily understand something like: a + b * c - d / e, without the need for brackets: (a + (B * C)) - (d / e).  IMO, the first statement is easier to read too.
Title: Re: -3^2 = -9 .... about order of operations
Post by: Fr0stAngel on March 21, 2011, 08:26:28 AM
sure any programmer can easily understand something like: a + b * c - d / e, without the need for brackets: (a + (B * C)) - (d / e).  IMO, the first statement is easier to read too.


agreed, but brackets are still good cause they let your program to be easier to explain to others( not everyone's a good programmer ;)) and sometimes debug
Title: Re: -3^2 = -9 .... about order of operations
Post by: Admin on March 22, 2011, 03:54:44 PM
Shouldn't the calculation simply follow the 'BEDMAS' (or any other permutation for the word) standard for operations? (brackets, exponents, division/multiplication, addition/subtraction).
uh, doesn't multiplication come before division? Please Excuse My Dear Aunt Sally. :P
Title: Re: -3^2 = -9 .... about order of operations
Post by: joe61 on March 22, 2011, 07:54:50 PM
uh, doesn't multiplication come before division? Please Excuse My Dear Aunt Sally. :P

Not in C, they both have the same precedence (along with %), and the order of evaluation isn't specified. Other programming languages could have different rules.

Joe
Title: Re: -3^2 = -9 .... about order of operations
Post by: Graynomad on March 22, 2011, 08:06:13 PM
Quote
doesn't multiplication come before division?
Quote
the order of evaluation isn't specified
Quote
Other programming languages could have different rules.
All arguments for using () to remove any ambiguity.

And then there's macros where you can really come unstuck if you don't use ()s.

______
Rob
Title: Re: -3^2 = -9 .... about order of operations
Post by: Metal Slug 2 on March 22, 2011, 08:24:00 PM
In C/C++, the associativity of operations which have the same precedence (ex. *, /, %) is left-to-right.
That is to say, the following statement: a / b * c would be interpreted as: (a / b) * c. link (http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence)
Although with macros you have to be especially careful... definitely need to explicitly place brackets.
Title: Re: -3^2 = -9 .... about order of operations
Post by: Soeren on March 22, 2011, 10:08:49 PM
Hi,

Quote
I'd always follow the practice of piling on parentheses to remove any doubts.
seconded; ive had issues with my TI-84 [...]
So, how does it interpret -3^2?

I thought all their math calculators motors were tested more or less the same before being released and I have never seen any errors in their "algebraic notation" format.

I just tapped the "-3^2" into the 4 calcs I could reach without getting up, a TI-92, a Casio CFX9800G and two really cheap No-Brand (i.e. Chinese) math calcs from dime stores.
Only one No-Brand crapped out.
Title: Re: -3^2 = -9 .... about order of operations
Post by: Aberg098 on March 23, 2011, 07:45:42 AM
I was always taught that division precedes multiplication using the PEDMAS or BEDMAS acryonym, for paper math at least!

uh, doesn't multiplication come before division? Please Excuse My Dear Aunt Sally. :P
[/quote]
Title: Re: -3^2 = -9 .... about order of operations
Post by: voyager2 on March 23, 2011, 05:47:47 PM
Interesting...
Both equations are correct on my CASIO fx-82MS (http://edu.casio.com/products/standard/fx82ms/)
it's never failed me yet!

Also in Australia we use BoDMAS...
Title: Re: -3^2 = -9 .... about order of operations
Post by: knossos on March 23, 2011, 11:33:23 PM
I was always taught that division precedes multiplication using the PEDMAS or BEDMAS acryonym, for paper math at least!

uh, doesn't multiplication come before division? Please Excuse My Dear Aunt Sally. :P

Actually Multiplication and Division have the same precedence just like Addition and Subtraction also have the same precedence.  So really it should be:

P
E
M and D (in any order)
A and S (in any order)
Title: Re: -3^2 = -9 .... about order of operations
Post by: Gertlex on April 22, 2011, 10:39:35 AM
Working with computer math is something that I do second-nature by now.  Additionally, if I'm squaring something that's negative, that negative something is going to be in a variable, almost always, so that will default to being interpreted as (-3)^2
Title: Re: -3^2 = -9 .... about order of operations
Post by: TrickyNekro on April 22, 2011, 07:29:53 PM
Multiplication and division have exactly the same order...

Why? because division is in fact the multiplication by a decimal number...
Basically, it's this:

A*B == A/(1/B)  :-*

Dammit guys, that's pretty basic...

And if you write, in a C program, 4*3/4 you can't be really sure what the output will be without using floats,
 or even doubles...
The output can even be 3 or 4 depending on the optimization of the compiler  ;)

That's why if not using float point mathematics, it's best to write the above like this (4*3)/4
;)

Anyhow, best regards,

Lefteris, Greece