~ ( x y epsilon -- ? ) (original) (raw)

~ ( x y epsilon -- ? )
Factor handbook » The language » Numbers » Mathematical functions » Arithmetic functions

Prev: round-to-step ( x step -- y )

Vocabulary
math.functions

Inputs

x a real
y a real
epsilon a real

Outputs

? a boolean

Word description
Tests if x and y are approximately equal to each other. There are three possible comparison tests, chosen based on the sign of epsilon:

epsilon is zero: exact comparison.
epsilon is positive: absolute distance test.
epsilon is negative: relative distance test.

Definition

USING: combinators kernel math ;

IN: math.functions

: ~ ( x y epsilon -- ? )
{
{ [ dup zero? ] [ drop number= ] }
{ [ dup 0 < ] [ neg ~rel ] }
[ ~abs ]
} cond ;