add
Adds the top two numbers of the integer stack and leaves the result.
Facts
- Word
add- Family
- Basic
- Integer stack
- pops 2, pushes 1
- What it does
- Pops two numbers and pushes their sum. Above 16,777,216 it loses precision, and past 2 billion it wraps around.
- Cost
- Basic command
- Runs
- inside a gene: in the condition (cond) and in the body (start or else)
a b add leaves a + b. It is plain old addition, with the usual order of DNA: first the two numbers, then the word.
| Word | Stack after |
|---|---|
3 | 3 |
4 | 3 4 |
add | 7 |
The most common use is adjusting a reading before storing it, or keeping a count in free memory. This bot adds 1 to cell 50 every cycle:
cond
start
*50 1 add 50 store
stopFor that specific job there is a shortcut, inc (50 inc), which does the same with fewer words and for a tenth of the energy a store costs (see Execution and costs).
Two inherited quirks, which only show up with enormous numbers, beyond what fits in memory:
- Above 16,777,216, addition loses precision: the operands are rounded before adding and the result can be off by a few units (more the bigger the number is).
20000 1000 mult 1 add 1 addgives 20000001, not 20000002. - If the result goes past 2 billion, it wraps around instead of stopping at the cap:
32000 32000 mult dup addgives 48 million.mult, by contrast, saturates.
Subtraction, sub, behaves the same way.