Floating point comparison operations
Factor handbook » The language » Numbers » Floats

Prev:Bitwise operations on floats


In mathematics, real numbers are linearly ordered; for any two numbers a and b, exactly one of the following is true:
a < b a = b a > b

With floating point values, there is a fourth possibility; a and b may be unordered. This happens if one or both values are Not-a-Number values.

All comparison operators, including number=, return f in the unordered case (and in particular, this means that a NaN is not equal to itself).

The ordered comparison operators set floating point exception flags if the result of the comparison is unordered. The standard comparison operators (<, <=, >, >=) perform ordered comparisons.

The number= operation performs an unordered comparison. The following set of operators also perform unordered comparisons:
u< ( x y -- ? )

u<= ( x y -- ? )

u> ( x y -- ? )

u>= ( x y -- ? )


A word to check if two values are unordered with respect to each other:
unordered? ( x y -- ? )


To test for floating point exceptions, use the math.floats.env vocabulary.

If neither input to a comparison operator is a floating point value, then u<, u<=, u> and u>= are equivalent to the ordered operators.