In this series we’ve been looking at machines with buttons, where a button press will invoke an action which we’ve called an *operator*. The operator operates on the state of the machine. It should be interesting to analyze the workings of a simplified calculator, and look at some aspects of its design. In doing so, we will be bringing in and tying in much of the prior posts in this series. Our starting point is our $2.99 four-function calculator from OfficeMax:

In the picture below it, we show the arrangement of the buttons and the screen of that calculator. On the left, all buttons are shown; on the right, we only keep the bare minimum of these buttons, so we can still say we have a four-function calculator. We want to build a model for the calculator on the bottom right, and this allows us to ignore complications for numbers with decimal points, and complications involving the memory M.

Let’s start by getting a sense of what the *state *of this machine is that the buttons operate on. It is fairly easy to see that, at least, this state must contain two numbers, which we’ll call LeftValue and RightValue. If we focus on what happens when we enter “13 + 5 = ” then just before the “=” is pressed, the screen shows “5” and right after the “=” is pressed, the screen shows “18”. It appears, then, that the machine keeps both the value “13” and the value “5” stashed somewhere, and in a form where they can be added, subtracted, multiplied or divided. We will say that 13 is the LeftValue and 5 the RightValue, just before the “=” is pressed. The whole state, with its LeftValue component and its RightValue component, and perhaps even more components, will be designated as State. The LeftValue component of the State will be denoted as State.LeftValue and State.RightValue is our notation for the RightValue component. Are these two components all the state in the machine, or is there more? Well, we could use some state to keep track of which of the four function buttons was pushed: plus, minus, multiply or divide. State.PendingOp will be our notation for this crucial piece of information; the scope of this piece of data is the four possible values “+”, “-“, “×”, “÷”.

Do we need more state to deal with the kinds of situation when you press “13 + 5 + 26 =”? On most calculators, after you have pressed “13 + 5”, when you press the second “+”, the screen will show 18 and the calculator will await your 26. It does additions etc as needed to make sure that the number of values to be kept is at most two. You may want to try your own calculator to see how it copes with “13+5×2=”. On a four-function calculator, you typically see it calculate 18×2. On more elaborate calculators, you may see it calculate 13+10. These latter calculators would be advertised as “knowing about order of operations”, typically be more expensive, and requiring more internal state. These would often be the same calculators that have “(” and “)” keys.

This diagram shows the over-all structure of the state machine. The blue part is the part that keeps the state; so far we have identified three components that make up the state. For each key, there is an operator that is invoked whenever that particular key is pressed. Missing from the diagram is the connection between the state machine and the screen, and of course no detail has yet been filled in for what any of these operators do. We will fill in some of those details in the next post.

Pingback: Operators, Functions, and Properties – part 22 « Learning and Unlearning Math

Pingback: Operators, Functions, and Properties: The Series « Learning and Unlearning Math