DarwinBotsManual
Open the app EspañolES

pyth

Gives the hypotenuse of the top two numbers of the stack, the square root of a² + b²: the length of a vector.

Facts

Word
pyth
Family
Advanced
Integer stack
pops 2, pushes 1
What it does
Pops two numbers a and b and pushes the square root of a² + b², rounded. It is capped at 2 billion.
Cost
Advanced command; debugint and debugbool are free
Runs
inside a gene: in the condition (cond) and in the body (start or else)

a b pyth leaves the square root of a² + b², rounded: the Pythagorean theorem. 3 4 pyth gives 5. The order doesn't matter, and neither does the sign.

It's useful for the length of any vector whose two components you have. For example, the bot's speed from its forward velocity (.velup) and its sideways velocity (.veldx):

cond
start
  *.velup *.veldx pyth 50 store
stop

(For speed in particular, .velscalar already exists, which the engine calculates for you.)

With coordinate differences it gives a distance: *.refxpos *.xpos sub *.refypos *.ypos sub pyth gives almost the same as *.refxpos *.refypos dist in a normal-sized world, but dist does it in a single word and uses the bot's exact position at that moment, not the one in .xpos and .ypos, which arrives one cycle late.