divstore
Divides what is in a cell by a number, rounding to the nearest integer; dividing by 0 leaves the cell at 0.
Facts
- Word
divstore- Family
- Writing to memory
- Integer stack
- pops 2, pushes 0
- What it does
- Pops an address and a value, and divides the cell by the value, rounding to the nearest integer (to the even one on a tie). Dividing by 0 leaves it at 0. With address 0 the value is lost.
- Cost
- Store divided by the divisor of each one, only if it writes
- Cost divisor
- ÷ 5
- Runs
- in the body of a gene (start or else), and only if the top of the boolean stack is true or the stack is empty
- Marks ties
- yes
v d divstore leaves in cell d what it held divided by v. It gives the same as *d v div d store, with fewer words and at one fifth of the cost of a store.
| Word | Stack after |
|---|---|
2 | 2 |
50 | 2 50 |
divstore | (empty); cell 50 holds half of what it did |
The division rounds to the nearest integer, it does not truncate, and on an exact tie it goes to the even one. You can see it well by halving over and over:
' halves cell 50 every cycle
cond
*.robage 0 =
start
100 50 store
stop
start
2 50 divstore
stopThe cell goes through 50, 25, 12, 6, 3, 2, 1 and 0. Note the ties: 12.5 gives 12, 1.5 gives 2 and 0.5 gives 0.
Two traps:
- Dividing by 0 does not leave the cell as it was: it sets it to 0. If the divisor comes from a reading that can be 0, guard it with a condition.
- With address 0, unlike
store, the value is popped from the stack and lost.
Like store, it notifies the tie system if you write to .tieang1 or .tielen1. Division on the stack, without storing, is div.