LearnBuild toward a chatbotObstacle 1: only one question

Obstacle 1: only one question

Aim. At the end of this lesson you have felt, in the step counter, why the obvious way to compare and to divide cannot work for a model.

Where the lessons are going

The sum is made: 11,868. The next part divides it by 128 and rounds. Multiplying was cheap. This is not, because a brainfuck program can ask only one question: is this cell zero?

Which one is bigger?

Try it: two cells, a holds 5 and b holds 3. Press Play, or Step, until the program has ended. How does it find out which is bigger?

Figure 1. Comparing by counting down. Watch a and b fall together until one is empty. (interactive)

The same, with bigger numbers

Try it: make both numbers large, a near 1000 and b near 900. Go to the last step and read how many steps it took:

Figure 2. The same comparison with your numbers. Watch the step counter, not the tape. (interactive)

Divide 11,868 by 128

Try it: the slider stands at 11,868, the number from the sum. Drag it all the way to the right and watch the steps:

Answer of the brainfuck program (divided by 128, rounded)
Steps
Steps for each unit of the number
steps
Figure 3. Dividing by counting. The steps are measured again each time you move the slider. (interactive)

How bad does it get?

Try it: now take a much larger number, 1,000,000. How many steps will dividing it take? Work it out from the table of step 3, type your estimate and check:

Figure 4. Your estimate against a real run of the dividing program on 1,000,000. (interactive)

What you can do now

Nothing new, and that is the point. You can compare and divide, but only by counting, and counting costs a step for every unit. The second part of the sum, 11,868 divided by 128, costs 131,538 steps this way.

The way through uses the oddest thing from step 7 of lesson 1: a cell that is full goes round to zero. How does that help?

Why a fast interpreter does not save it

The multiplying loop of step 3 of lesson 3 only adds and moves, so a fast interpreter does it in one step. A counting division has a question inside every round ("is this the 128th time?"), and a question is a loop of its own. The fast interpreter has to go through every round.

A model has to do three things of this kind all the time: divide, round, and cut a number off at a lowest and a highest value. All three are comparisons in disguise.