Divide A/B
This layout divides one binary number held in input register A, by the number held in register B , and outputs the result in register S . The calculation is performed by a series of subtractions. Register S counts the number of subtractions before register A 'underflows'.
Two functions are used:
- Accumulator function, to subtract B from A .
- Count Up function to increment S .
Operation
Clear register S . Enter binary values for A and B (eg 14/3) and start the train from the station. When the train returns and halts, the result of A / B can be read from the output register S .
|
| Click layout to pause/run train | Click points to switch 0/1 | Click start circle to reset train/points |
|
|
Lazy points switch between upper
0
or lower
1
branch lines
Trains arriving on a branch line switch the point to that line |
|
|
Sprung points allow branch line trains to join the main line
All main line trains go straight ahead and never 'branch off' |
Notes
- If the result of A / B exceeds 31, register S overflows and the train returns to the lower station platform as an error. This should not happen unless dividing by zero, or register S was not cleared before calculation commenced.
- Register B is checked first. If it is greater than A , then A will 'underflow'. The train returns to the station and the calculation halts, showing 0.
- The original value of A is 'lost' as B is subtracted from it. A Copy function can be added to preserve the initial value of A .
- The remainder value is also lost. Again, a Copy function can be added to preserve the remainder value if required.
This layout is almost a vertical reflection of the previous Multiplier function, (with two more stages added to register A ). In the Multiplier, the Count Down function is checked first, in case it is zero. Here, the Accumulator is checked first, in case it is larger than A causing an 'underflow' and halting the calculation.
Divide A/B (with zero check)
The layout works correctly when dividing by zero, eventually causing register S to overflow. Its not very elegant though. In the layout below, I've dropped in a Zero Check function, which stops the calculation with an error if B is zero.
|
| Click layout to pause/run train | Click points to switch 0/1 | Click start circle to reset train/points |
|
|
Lazy points switch between upper
0
or lower
1
branch lines
Trains arriving on a branch line switch the point to that line |
|
|
Sprung points allow branch line trains to join the main line
All main line trains go straight ahead and never 'branch off' |
See Divider using a shift function for a more efiicient design.