DarwinBotsManual
Open the app EspañolES

~=

a b d ~= leaves true if b is within d % of a: an “about equal” with the margin you choose. Pops three numbers.

Facts

Word
~=
Family
Comparisons
Integer stack
pops 3, pushes 0
Boolean stack
pops 0, pushes 1
What it does
Pops three numbers a b d and pushes true if b is within d% of a. If only one of a and d is negative, it is always false.
Cost
Condition
Runs
inside a gene: in the condition (cond) and in the body (start or else)

a b d ~= is like %= but with the percentage of your choice: it pops three numbers and pushes true if b is no more than d % away from a. The reference is a, the one lowest in the stack; the percentage goes on top.

Stack beforeResultWhy
100 120 25truethe margin is 75 to 125
100 130 25falseout of range
1000 1015 1false1% of 1000 is 10
100 120 -25falsenegative percentage
-100 -100 5falsenegative reference

100 b 10 ~= is the same as 100 b %=. The limits count and the margin is not rounded.

It is useful for detecting whether a reading changed “for real” or just wobbled. This bot stores in cell 50 what the middle eye (.eye5) was seeing, and counts in cell 51 the cycles in which what it sees now is similar, within 5%, to what it saw in the previous cycle:

' counts the cycles in which what it sees did not change by more than 5%
cond
  *50 *.eye5 5 ~=
start
  51 inc
stop

cond
start
  *.eye5 50 store
stop

The gene that stores comes after the one that compares, so cell 50 still has the old value when it is read.

If you forget the percentage, ~= still pops three numbers: it takes as reference whatever is lower in the stack, or 0 if it is empty. The opposite is !~=.