go away spammer

### Author Topic: -3^2 = -9 .... about order of operations  (Read 4018 times)

0 Members and 1 Guest are viewing this topic.

• Supreme Robot
• Posts: 11,703
##### -3^2 = -9 .... about order of operations
« 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

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

For more detailed information:
http://mathforum.org/library/drmath/view/61633.html

#### Soeren

• Supreme Robot
• Posts: 4,672
• Mind Reading: 0.0
##### Re: -3^2 = -9 .... about order of operations
« Reply #1 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 -).
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Engineering is based on numbers - not adjectives

#### Soeren

• Supreme Robot
• Posts: 4,672
• Mind Reading: 0.0
##### Re: -3^2 = -9 .... about order of operations
« Reply #2 on: March 19, 2011, 04:49:14 PM »
If in the slightest doubt, just use plenty of parentheses/brackets to control the operations.
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Engineering is based on numbers - not adjectives

• Supreme Robot
• Posts: 11,703
##### Re: -3^2 = -9 .... about order of operations
« Reply #3 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

#### richiereynolds

• Full Member
• Posts: 112
##### Re: -3^2 = -9 .... about order of operations
« Reply #4 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.

#### Metal Slug 2

• Supreme Robot
• Posts: 333
##### Re: -3^2 = -9 .... about order of operations
« Reply #5 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.
« Last Edit: March 22, 2011, 07:45:05 PM by Metal Slug 2 »

#### robots-in-brighton

• Jr. Member
• Posts: 9
##### Re: -3^2 = -9 .... about order of operations
« Reply #6 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.

#### BANE

• Supreme Robot
• Posts: 639
• E=roboticsC^2
##### Re: -3^2 = -9 .... about order of operations
« Reply #7 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

• Full Member
• Posts: 79
##### Re: -3^2 = -9 .... about order of operations
« Reply #8 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
Scattered showers my arse -- Noah, 2348BC.

#### Metal Slug 2

• Supreme Robot
• Posts: 333
##### Re: -3^2 = -9 .... about order of operations
« Reply #9 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.  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.

#### Fr0stAngel

• Full Member
• Posts: 96
• [O_O] what??
##### Re: -3^2 = -9 .... about order of operations
« Reply #10 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
'crazy' is the new hype! =)

• Supreme Robot
• Posts: 11,703
##### Re: -3^2 = -9 .... about order of operations
« Reply #11 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.

#### joe61

• Supreme Robot
• Posts: 417
##### Re: -3^2 = -9 .... about order of operations
« Reply #12 on: March 22, 2011, 07:54:50 PM »
uh, doesn't multiplication come before division? Please Excuse My Dear Aunt Sally.

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

• Full Member
• Posts: 79
##### Re: -3^2 = -9 .... about order of operations
« Reply #13 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
Scattered showers my arse -- Noah, 2348BC.

#### Metal Slug 2

• Supreme Robot
• Posts: 333
##### Re: -3^2 = -9 .... about order of operations
« Reply #14 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
Although with macros you have to be especially careful... definitely need to explicitly place brackets.

#### Soeren

• Supreme Robot
• Posts: 4,672
• Mind Reading: 0.0
##### Re: -3^2 = -9 .... about order of operations
« Reply #15 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.
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Engineering is based on numbers - not adjectives

#### Aberg098

• Full Member
• Posts: 66
##### Re: -3^2 = -9 .... about order of operations
« Reply #16 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.
[/quote]

#### voyager2

• Supreme Robot
• Posts: 463
##### Re: -3^2 = -9 .... about order of operations
« Reply #17 on: March 23, 2011, 05:47:47 PM »
Interesting...
Both equations are correct on my CASIO fx-82MS
it's never failed me yet!

Also in Australia we use BoDMAS...
And Admin said "Let there be robots!"
And it was good.

#### knossos

• Robot Overlord
• Posts: 278
##### Re: -3^2 = -9 .... about order of operations
« Reply #18 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.

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)
"Never regret thy fall,
O Icarus of the fearless flight
For the greatest tragedy of them all
Is never to feel the burning light."

— Oscar Wilde

#### Gertlex

• Supreme Robot
• Posts: 763
• Nuclear Engineer · Roboticist
##### Re: -3^2 = -9 .... about order of operations
« Reply #19 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
I

#### TrickyNekro

• Contest Winner
• Supreme Robot
• Posts: 1,208
• Hardware and Firmware Designer
##### Re: -3^2 = -9 .... about order of operations
« Reply #20 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
For whom the interrupts toll...