Arithmetic



You are now on speaking terms with your computer. The next task is to learn a few simple expressions, the computer equivalent of “My name is Fred”, “Does this bus go to Notre Dame?”, or “Where is the bathroom?” This chapter will introduce you to arithmetic on the computer.

Many people mistakenly think that performing arithmetic computations is the prime function of a computer. In truth, computers spend most of their time doing far less exalted work: moving bits of information around from one place to another, painstakingly examining huge piles of data for those few scraps of data that are just what the user ordered, or rewriting the data in a form that is easier for the user to appreciate. Nevertheless, arithmetic is an excellent topic to begin studying because it is familiar to people. If you can do arithmetic on a calculator, you can do arithmetic on a computer. In fact, it’s even easier on the computer. 

There’s one little problem that always trips up beginners. On the JavaScript trial page, try this:

print (8 + 12 / 4)

You could interpret this in two different ways: 

8 plus 12 makes 20; dividing 20 by 4 gives 5 
or 
12 divided by 4 makes 3; adding 8 gives 11

You would probably think that the first interpretation makes more sense; after all, it’s in the same order as the computer command, while the second interpretation does the arithmetic in backwards order. OK, so what’s the JavaScript command for this:

Uh-oh! How do you translate this into JavaScript? There’s no obvious way, is there? If you spend enough time, you can probably puzzle it out. But there’s an easier way to do all this: you just learn two rules:

Rule 1: multiplication and division are always executed before addition and subtraction.

Rule 2: When in doubt, group additions and subtractions together with parentheses.

So (8 + 12 / 4) is always interpreted like this:

12 divided by 4 makes 3; adding 8 gives 11. 

Let’s use the two rules to write the JavaScript command for the huge expression above. The best strategy is usually to start on the inside and work your way out. Thus, our command will look something like this at first:

print (??? (12 + 4) ???)

Now let’s put in the 2 that acts as a divisor:

print (??? (12 + 4) / 2)

Now we want to multiply this whole thing by 5, but remember: we want to multiply the whole thing by 5, so to do that, we put parentheses around the whole thing. That groups it all together, like this:

print (5 * ((12 + 4) / 2))

Finally, we double-check our work by counting parentheses: there should be the same number of closing parentheses as there are opening parentheses. Count them and confirm that we got it right.

This really isn’t a big deal, but it’s just tricky enough to trip you up. I still remember that, in one of my first programs, I missed a parenthesis, and got bad results. That was way back in the days when the computer was this big machine in a room and the answers came out of a printer. The printer started printing numbers that I knew were wrong, and I started yelling to the operator that he had to stop the computer. I should have kept my mouth shut and acted as if everything was working out just fine, then skulked off with my printout. 

So here’s all you have to remember:

1) Multiplication and division come before addition and subtraction.
2) Parentheses defeat the normal rules of precedence.

If you think you’ve got it down pat, try out this exercise