Intel's Ronler Acres Plant

Silicon Forest
If the type is too small, Ctrl+ is your friend

Wednesday, November 8, 2017

Precedence

Precendence Park, Ankeny, Iowa
Because I needed a picture for this post, and I really wonder what prompted this name choice.
There is an equation kicking around out there on the internet that is causing a fuss. It's a simple equation:
6/2(1+2)
If you try to evaluate it, but you don't know and follow the rules of precedence, you might get the wrong answer. Depending on whether you divide or multiple first you could get 9 or 1.

There are rules for precedence. I think they are the same for computer programs and high school algebra, but I'm not be sure. (Teachers don't you fill me up with your rules.)

They made a big deal out of it at college when I was studying computer science, and maybe if you spent all day coding equations they would become second nature to you.

I've done a little bit of mathematical coding, so I've dealt with this problem and I don't like it, so I use parentheses. Parentheses overrule all other rules, so if you don't want your equation misinterpreted, load it up with parentheses. That way there can be no doubt as what you mean.

If I recall the rules correctly, they are:
  1. Parentheses
  2. Multiplication and division, in order, left to right
  3. Addition and subtraction, in order, left to right
So this equation: 6/2(1+2)
  • would be (1+2) = 3
  • 6 / 2 = 3
  • 3 x 3 = 9
But I also wouldn't trust it, so I would add parentheses, i.e. (6/2)(1+2)
and if you want to feed it to a computer you also need the symbol for multiplication, i.e. (6/2) * (1+2).

I don't know of any programming language that understands implicit multiplication, i.e. 2(3x).

Via Iaman

No comments: