6÷2(1+2)= ?

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
Blood
Enthusiast
Enthusiast
Posts: 161
Joined: Tue Dec 08, 2009 8:34 pm
Location: United Kingdom

6÷2(1+2)= ?

Post by Blood »

6÷2(1+2)= ?

Is the answer 1 or 9 ?
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. -- K&R (2nd Ed.) : Page 65
User avatar
skywalk
Addict
Addict
Posts: 4242
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: 6÷2(1+2)= ?

Post by skywalk »

I'd say 9, using left to right order of operations...
http://www.google.com/search?cx=c&sourc ... B72(1%2B2)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: 6÷2(1+2)= ?

Post by gnasen »

The term is mathematically:
6 / 2 * 3 = 6 * 2^(-1) * 3 = 9
because the sign "/" is nothing else then the inverse of the following number and multiplication is associativ.
Further there is still a "*" between 2 and 3, otherwise you have to use brackets. If there should be no "*" and no brackets, its just undefined.
pb 5.11
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: 6÷2(1+2)= ?

Post by blueznl »

6÷2(1+2) =
6 / 2 * ( 1 + 2 ) =
6 / 2 * ( 3 ) =
6 / 2 * 3 =
3 * 3 =
9

Why:

- there's no operator between 2 and (1+2) so it's a multiplication
- first get the value of anything between the brackets
- substitute brackets + expression within with result
- do the rest
Last edited by blueznl on Thu Aug 25, 2011 5:07 pm, edited 2 times in total.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
graph100
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Aug 10, 2010 3:17 pm

Re: 6÷2(1+2)= ?

Post by graph100 »

my calculator said 1.
I believe that it prioritize the use of the parenthesis like a higher priority then if it was written like : 6 / 2 * 3
And I would have said 1 too, since the "2" is near the parenthesis without operator : it is implicit.

(as you can easily guess the calculator I use is a casio Graph100+ :mrgreen: , that is if you want too test it)
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed ;))
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: 6÷2(1+2)= ?

Post by gnasen »

graph100 wrote:And I would have said 1 too, since the "2" is near the parenthesis without operator : it is implicit.
Thats the problem. There are common standards which should be used. And one of them is, that you can omit a dot (for mutliplication) but never ever a paranthesis if it is not absolutly obvious how the term should be calculated.
Calculators (including mine) seem to bind a term in the way you observated it. However thats just arbitrariness. In a term like this you should use paranthesis right or avoid the division operator.
pb 5.11
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: 6÷2(1+2)= ?

Post by spikey »

The order of precedence is denoted by the mnemonic BODMAS.

Brackets (parenthesis)
Orders (powers/roots)
Divide/Multiply
Add/Subtract

Where operators have equal precedence, overiding precedence is given to the left most operator.

So the calculation order is:-
1) 6 ÷ 2 ( 1 + 2 )
2) Brackets - gives 6 ÷ 2 x 3
3) Divide is leftmost - gives 3 x 3
4) Implicit multiply gives 3 x 3 = 9

So the answer is 9, as blueznl states.
User avatar
kenmo
Addict
Addict
Posts: 2054
Joined: Tue Dec 23, 2003 3:54 am

Re: 6÷2(1+2)= ?

Post by kenmo »

:) This was the topic of a big debate between some classmates recently (not the exact same numbers, of course), and the conclusion was:

Without explicit parentheses, the division should occur first (left-to-right, resulting in 9 in this case). It is misleading to write the division sign and not the implied multiplication sign. Furthermore, division should have equal priority to multiplication since it is really multiplication by the inverse (as gnasen said).

Some calculators may multiply the 2 before dividing (for convenience?) but really that is non-standard.
User avatar
graph100
Enthusiast
Enthusiast
Posts: 115
Joined: Tue Aug 10, 2010 3:17 pm

Re: 6÷2(1+2)= ?

Post by graph100 »

the moral is : when you take exam, type all the operator to obtain a sure answer ;)
_________________________________________________
My Website : CeriseCode (Warning : perpetual changes & not completed ;))
Post Reply