News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Fun (and trouble) with loops

Started by RedSkeleton007, September 09, 2015, 11:23:20 AM

Previous topic - Next topic

rrr314159

*** WARNING: Nit-pick alert ***

it's not really ambiguous, due to operator precedence (* binds b4 +)
I am NaN ;)

zedd151

Quote from: hutch-- on September 10, 2015, 08:39:38 AM
...as it is ambiguous as it is.
Not really.

Actually that is one of the few things I remember from school. Multiplication always comes first.  :lol:

Don't ask what else follows in what order, now THAT I simply do not remember.

dedndave


rrr314159

Interesting that of all programmers, only assembly language programmers could be unsure of mathematical operator precedence. All others languages allow an expression like "6/2 + 5*5" so the programmer must know how it evaluates; but for assembler the knowledge is irrelevant

altho, come to think of it, I have seen competent C programmers who don't really know precedence rules; they always put parentheses, just to make sure
I am NaN ;)

RedSkeleton007

Quote from: hutch-- on September 10, 2015, 08:39:38 AM
:biggrin:

Dave,

I would like to see that formula bracketed as it is ambiguous as it is.

(3+5)*5 = 40
3+(5*5) = 28

Not necessary. Hasn't anybody here ever heard of the old mnemonic "Please Excuse My Dear Aunt Sally"?

Parentheses
Exponents
Multiplication
Division
Addition
Subtraction

As long as you use PEMDAS, you can't go wrong ;)

zedd151

Quote from: RedSkeleton007 on September 10, 2015, 01:46:00 PM
Hasn't anybody here ever heard of the old mnemonic "


Quote
Parentheses
Exponents
Multiplication
Division
Addition
Subtraction

As long as you use PEMDAS, you can't go wrong ;)


Um, thats's an ACRONYM that you just used. :lol:

Haven't heard of it, but I'm OldSkool.

MichaelW

Quote from: RedSkeleton007 on September 10, 2015, 01:46:00 PM
As long as you use PEMDAS, you can't go wrong ;)

As long as you use parentheses to make any non-obvious precedence obvious, you can't go wrong.
Well Microsoft, here's another nice mess you've gotten us into.

dedndave

i think it's a "phonetic" - what do i know

as for the parens, i sometimes use them, even when they are not needed
it helps, when reading the code later, to understand how an expression was derived
WindowWidth-(NumberColumns-6)

dedndave

ok - had to google it - it's a mnemonic

a word we ought to know - lol

zedd151

Quote from: dedndave on September 10, 2015, 03:23:02 PM
ok - had to google it - it's a mnemonic

a word we ought to know - lol

Damn, I must be having another memory leak.  :lol:
Happens more and more lately. I'm getting to be an old fart.

FORTRANS

Hi,

   Back in the "good old days", I heard it as Pretty Pink Roses My
Dear Aunt Sally.  For Parentheses, Powers, Roots, Multiplication,
Division, Addition, and Subtraction.

Cheers,

Steve N.

rrr314159

In case there's still confusion,

"Please Excuse My Dear Aunt Sally" is a mnemonic

PEMDAS is an acronym

Personally I never heard of it (since I never taught math below college level). Anyway, it's wrong. Addition does NOT bind b4 subtraction, rather, they have the same precedence, and are evaluated left to right. Thus

9 - 4 + 3 = 8, NOT 9 - (4+3) which would give 7

Same with multiplication and division, So

9 / 4 * 3 = 6.75 NOT 9 / (4*3) = .75

Must admit I'm not sure what Hebrew mathematicians (speakers of languages that are written right to left) do, I suppose they follow the left-to-right convention of English. Also, must admit I've seen people treat mult as binding b4 division. But the "official" convention is as I say, so PEMDAS should be something like

P
E
M, D
A, S

BTW this relates to a comment I made elsewhere. Traditionally academic ability is said to have two varities, math and verbal (of course reality is infinitely more complex but this is a good first cut). And, traditionally (40 years ago) programming was thought to need math more than verbal. In fact, I think verbal ability is more important for programming (believe it or not) because the key skill is very related to parsing a sentence. That's essentially what we're doing with "PEMDAS" - while the actual arithmetic operations are of course done by the machine, not the programmer
I am NaN ;)

hutch--

 :biggrin:

Now you know why I bracket formulas. Came from studying logic using Tarski's notation from a text book by Donald Kalish and Richard Montague. Bracteting is extremely UNambiguous.  :P

sinsi

>9 - 4 + 3 = 8, NOT 9 - (4+3) which would give 7

Using brackets would change the sign of 3
9 - 4 + 3 = 8
9 - (4 - 3) = 8
Tá fuinneoga a haon déag níos fearr :biggrin:

rrr314159

@sinsi,

of course you're right, but I suspect you missed my point. The PEMDAS rule was stated incorrectly. It said that addition "takes precedence" (or, "binds before") subtraction. That means you could always put brackets around the two numbers being added, to get (in this case)

9 - (4 + 3)

but that's wrong. Addition does NOT bind b4 subtraction, rather they evaluate left to right. Therefore you're right, if you want to use brackets here u must change the sign.

I hope that's clear but am afraid it's not.

@hutch,

Whenever I have to do basic arithmetic, like 9-4+3, I always consult my well-thumbed copy of Principia Mathematica to make sure I'm doing it right. Doesn't everybody? Can't imagine how people added numbers together before Russell, Tarski etc showed us how :P
I am NaN ;)