Society of Robots - Robot Forum
General Misc => Misc => Topic started 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)
-
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 -).
-
If in the slightest doubt, just use plenty of parentheses/brackets to control the operations.
-
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
-
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.
-
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. :)
-
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.
-
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
-
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
-
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.
-
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
-
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
-
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
-
doesn't multiplication come before division?
the order of evaluation isn't specified
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
-
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.
-
Hi,
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.
-
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]
-
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...
-
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)
-
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
-
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