Number protocol
Factor handbook » The language » Numbers » Arithmetic

Next:Modular arithmetic


Math operations obey certain numerical upgrade rules. If one of the inputs is a bignum and the other is a fixnum, the latter is first coerced to a bignum; if one of the inputs is a float, the other is coerced to a float.

Two examples where you should note the types of the inputs and outputs:
USE: classes 3 >fixnum 6 >bignum * class-of .
bignum

1/2 2.0 + .
2.5

The following usual operations are supported by all numbers.
+ ( x y -- z )

- ( x y -- z )

* ( x y -- z )

/ ( x y -- z )


Non-commutative operations take operands from the stack in the natural order; 6 2 / divides 6 by 2.
Division by zero

Real numbers (but not complex numbers) can be ordered:
< ( x y -- ? )

<= ( x y -- ? )

> ( x y -- ? )

>= ( x y -- ? )


Numbers can be compared for equality using =, or a less precise test which disregards types:
number= ( x y -- ? )


See also
Floating point comparison operations