Previous Contents Next
Chapter 21 The num library: arbitrary-precision rational arithmetic

The num library implements integer arithmetic and rational arithmetic in arbitrary precision.

More documentation on the functions provided in this library can be found in The CAML Numbers Reference Manual by Valérie Ménissier-Morain, technical report 141, INRIA, july 1992 (available electronically, ftp://ftp.inria.fr/INRIA/publication/RT/RT-0141.ps.gz).

Programs that use the num library must be linked as follows:
        ocamlc other options nums.cma other files
        ocamlopt other options nums.cmxa other files
For interactive use of the nums library, do:
        ocamlmktop -o mytop nums.cma
        ./mytop
or (if dynamic linking of C libraries is supported on your platform), start ocaml and type #load "nums.cma";;.

21.1 Module Num: operation on arbitrary-precision numbers


open Nat
open Big_int
open Ratio
Numbers (type num) are arbitrary-precision rational numbers, plus the special elements 1/0 (infinity) and 0/0 (undefined).
type num = Int of int | Big_int of big_int | Ratio of ratio
The type of numbers.
Arithmetic operations
val (+/) : num -> num -> num
val add_num : num -> num -> num
Addition
val minus_num : num -> num
Unary negation.
val (-/) : num -> num -> num
val sub_num : num -> num -> num
Subtraction
val (*/) : num -> num -> num
val mult_num : num -> num -> num
Multiplication
val square_num : num -> num
Squaring
val (//) : num -> num -> num
val div_num : num -> num -> num
Division
val quo_num : num -> num -> num
val mod_num : num -> num -> num
Euclidean division: quotient and remainder
val (**/) : num -> num -> num
val power_num : num -> num -> num
Exponentiation
val is_integer_num : num