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.

Numeric comparison operators, including number=, return f in the unordered case. The equal? method for floats also returns f when either operand is a NaN. However, = first checks object identity, so comparing a NaN object to itself with = returns t.

Object equality distinguishes floats from integers: both 0.0 0 = and -0.0 0 = return f. Use number= to compare numeric values across types. Positive and negative floating point zero compare equal with both = and number=.

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.