It’s been a while since I did a post about my day job, so I thought it was about time. The subject? Math. Everyone likes to hear about math, right?
You remember all those classes in high school and college where you had to do all that fancy equation stuff, and you found yourself thinking “I’ll never use that!” Well, if you get into technology or actuarial work, you will use “that.” And here I am, a database administrator learning PHP and being reminded that Whole Numbers, Real Numbers, and parentheses are indeed important to my current work. So here’s the quiz…
What is the sum of 1 + 3 * 6 + 1?
The answer may not be what you think. SQL Server, and many other programs, do multiplication and division first, before addition and subtraction. So the correct answer is 20.
3 * 6 = 18 which becomes 1 + 18 + 1 = 20.
But what if you put in parens? The answer may change, depending on where you put in the parens because the values inside the parens gets calculated before the multiplication & division are applied to the parens value. Try this on for size:
1 + (3 * 6) + 1 = 20 –This is how the computer sees it.
1 + 3 * (6 + 1) = 22 –The computer multiplies times the value in parens, which is 7. 1 + 3 * (7) becomes 1 + 21 becomes 22.
(1 + 3) * 6 + 1 = 25 — (4) * 6 + 1 becomes 24 + 1 becomes 25.
(1 + 3) * (6 + 1) = 28 — (4) * (7) is what this becomes.
Same formula, 4 different answers. Wow. Clear as mud, right? Just remember, when coding formulas for a report or a feed or even a calculated column, precedence matters. If you mistype your parens, or forget to put them in, you may end up with a completely different value than you intend to get.
Double-check any formulas (and their results) with your business users to make sure they are getting the values they expect to see. And if they aren’t, make them define the formula for you. If they don’t know what the formula is, then you will never be able to give them the answer that they want (or need) to see.

