DSPRelated.com
Forums

This has been doing teh rounds

Started by Unknown January 1, 2016
so as engineers I want to know your opinion

I don't have the divide sign so I will use minus instead

what is

6 - 2(1+2)

where the - is a divide.

I have this down for 1 since the brackets and the multiply comes first.

There is as school of thought that writes this as 6-2 = 3X (1+2) = 9!

So is it 1 or 9. Of course I have rarely if ever seen anybody use the divide symbol at all since we use /. It is relevant in coding though if you were to write

y=a/b*(1+b)   is this (a/b)*(1+b) ? Or does this depend on the compiler. I always use brackets to avoid such problems even when it appears you may not need them.

On Saturday, January 2, 2016 at 3:41:52 PM UTC+13, gyans...@gmail.com wrote:
> so as engineers I want to know your opinion > > I don't have the divide sign so I will use minus instead > > what is > > 6 - 2(1+2) > > where the - is a divide. > > I have this down for 1 since the brackets and the multiply comes first. > > There is as school of thought that writes this as 6-2 = 3X (1+2) = 9! > > So is it 1 or 9. Of course I have rarely if ever seen anybody use the divide symbol at all since we use /. It is relevant in coding though if you were to write > > y=a/b*(1+b) is this (a/b)*(1+b) ? Or does this depend on the compiler. I always use brackets to avoid such problems even when it appears you may not need them.
In Matlab
>> 6/2*(1+2)
ans = 9
>> 6/(2*(1+2))
ans = 1
On Fri, 1 Jan 2016 19:08:22 -0800 (PST), gyansorova@gmail.com wrote:

>On Saturday, January 2, 2016 at 3:41:52 PM UTC+13, gyans...@gmail.com wrote: >> so as engineers I want to know your opinion >> >> I don't have the divide sign so I will use minus instead >> >> what is >> >> 6 - 2(1+2) >> >> where the - is a divide. >> >> I have this down for 1 since the brackets and the multiply comes first. >> >> There is as school of thought that writes this as 6-2 = 3X (1+2) = 9! >> >> So is it 1 or 9. Of course I have rarely if ever seen anybody use the divide symbol at all since we use /. It is relevant in coding though if you were to write >> >> y=a/b*(1+b) is this (a/b)*(1+b) ? Or does this depend on the compiler. I always use brackets to avoid such problems even when it appears you may not need them. > >In Matlab > > >>> 6/2*(1+2) > >ans = > > 9 > >>> 6/(2*(1+2)) > >ans = > > 1
This is why I always use parens to show what is intended. They don't hurt, and they can save your butt from very hard-to-find bugs when it gets interpreted in a way other than assumed. Eric Jacobsen Anchor Hill Communications http://www.anchorhill.com
>so as engineers I want to know your opinion > >I don't have the divide sign so I will use minus instead > >what is > >6 - 2(1+2) > >where the - is a divide. > >I have this down for 1 since the brackets and the multiply comes first. > >There is as school of thought that writes this as 6-2 = 3X (1+2) = 9! > >So is it 1 or 9. Of course I have rarely if ever seen anybody use the
divide
>symbol at all since we use /. It is relevant in coding though if you were
to
>write > >y=a/b*(1+b) is this (a/b)*(1+b) ? Or does this depend on the compiler.
I
>always use brackets to avoid such problems even when it appears you may
not
>need them.
The name for this concept is operator precedence. There is no harm in adding extra parentheses, so for readability you may want to get in the habit of adding them. In all compilers I am aware of a/b*(1+b) = (a/b)*(1+b). You could also rewrite it as a*(1+b)/b. When you handwrite it, or use something like Latex, with horizontal bars in the fraction, the format of expression is what determines the operation order. When the variable types are integers, unexpected results can occur because of rounding. In this case, there can be differences in how it works based on the language definition. Ced --------------------------------------- Posted through http://www.DSPRelated.com
On 02/01/2016 02:41, gyansorova@gmail.com wrote:
> I have this down for 1 since the brackets and the multiply comes first.
Did't you learn BODMAS in school for operator precedence? Brackets Of Division Multiplication Addition Subtraction So applying BODMAS, remeber it goes left to right, to 6 - 2(1+2) gives 6 / 2 * (1 + 2) evaluate brackets 6 / 2 * 3 evaluate division 3 * 3 evaluate multiplication 9
Ask teachers and politicians what 1+2*3 is. Most
get it wrong :(

The more clued up ask where the brackets are, and
are flummoxed when you say "no brackets, just the
normal laws of arithmetic".
On 2016-01-02, Tom Gardner <spamjunk@blueyonder.co.uk> wrote:
> Ask teachers and politicians what 1+2*3 is. Most > get it wrong :( >
Most cheap calculators, the sort bought by grade school students if they can't use their mobile, get it wrong...
>On 02/01/2016 02:41, gyansorova@gmail.com wrote: >> I have this down for 1 since the brackets and the multiply comes
first.
> >Did't you learn BODMAS in school for operator precedence? > >Brackets >Of >Division >Multiplication >Addition >Subtraction >
[...snip...] Nope, didn't learn it that way in school. Never even heard of it. For C/C++, and every other compiler I know about it is incorrect too. Keeping to your subset of operations, it should be: Brackets Of Division and Multiplication Addition and Subtraction Then left to right. Given this function: int echo( int n ) { printf( "%d ", n ); return n; } This code snippet: int r; r = echo(6) / echo(2) * (echo(1) + echo(2)); printf( " %dn", r ); r = echo(6) * (echo(1) + echo(2)) / echo(2); printf( " %dn", r ); r = echo(6) / echo(8) * (echo(1) + echo(2)); printf( " %dn", r ); r = echo(6) * (echo(1) + echo(2)) / echo(8); printf( " %dn", r ); Produces these results: 6 2 1 2 9 6 1 2 2 9 6 8 1 2 0 6 1 2 8 2 Note that division does not take precedence over multiplication. Ced --------------------------------------- Posted through http://www.DSPRelated.com
Oops, I meant:

Of
Brackets
Division and Multiplication
Addition and Subtraction

Then left to right.

The function calls get evaluated first.

Ced
---------------------------------------
Posted through http://www.DSPRelated.com
On 02/01/2016 14:29, Cedron wrote:

> Nope, didn't learn it that way in school. Never even heard of it. For > C/C++, and every other compiler I know about it is incorrect too. >
Perhaps you're a foreigner and it was called PEMDAS.
> > Note that division does not take precedence over multiplication.
I never said it did. You do the division before the multiplication here because you evaluate left to right.