sqr
Replaces the top number of the stack with its rounded square root; if it isn't positive, it gives 0.
Facts
- Word
sqr- Family
- Advanced
- Integer stack
- pops 1, pushes 1
- What it does
- Replaces the top with its square root, rounded to the nearest integer. If it is not positive, it gives 0.
- Cost
- Advanced command; debugint and debugbool are free
- Runs
- inside a gene: in the condition (cond) and in the body (start or else)
x sqr leaves the square root of x, rounded to the nearest integer: 100 sqr gives 10, 10 sqr gives 3 and 2 sqr gives 1. Careful, the name is misleading: it doesn't square (for that, x dup mult or x 2 pow).
With a negative number, or with 0, it gives 0. That, which seems like a detail, is exploited by several bots in the Bestiary: x sqr dup div is 1 if x is positive and 0 if not, because div by zero gives 0. Pacifist v0.01 uses it to load venom only when it has more than 100 energy:
cond
start
' the address ends up at 824 if *.nrg − 100 is positive, and at 0 if not
40 .strvenom *.nrg 100 sub sqr dup div mult store
stopLook at what gets multiplied: not the 40, but the address of .strvenom, which ends up at 824 or at 0. And a store to address 0 does nothing (see Numbers and addresses), so the 40 is only written when there's energy to spare.
For the length of a vector, which is the root of a sum of squares, there is pyth; for other roots, root.