This modules builds arbitrary precision rationals on top of arbitrary integers from module Z.
This file is part of the Zarith library http://forge.ocamlcore.org/projects/zarith . It is distributed under LGPL 2 licensing, with static linking exception. See the LICENSE file included in the distribution.
Copyright (c) 2010-2011 Antoine Miné, Abstraction project. Abstraction is part of the LIENS (Laboratoire d'Informatique de l'ENS), a joint laboratory by: CNRS (Centre national de la recherche scientifique, France), ENS (École normale supérieure, Paris, France), INRIA Rocquencourt (Institut national de recherche en informatique, France).
|: Z.t||;||(*||Denominator, >= 0||*)|
A rational is represented as a pair numerator/denominator, reduced to
have a non-negative denominator and no common factor.
This form is canonical (enabling polymorphic equality and hashing).
The representation allows three special numbers:
Conversion from a
The conversion is exact, and maps NaN to
Converts a string to a rational.
Plain decimals, and
/ separated decimal ratios (with optional sign) are
Additionally, the special
undef are recognized
(they can also be typeset respectively as
# | ZERO
# | INF
|(*||infinity, i.e. 1/0||*)|
# | MINF
|(*||minus infinity, i.e. -1/0||*)|
# | UNDEF
|(*||undefined, i.e., 0/0||*)|
# | NZERO
|(*||well-defined, non-infinity, non-zero number||*)|
Rationals can be categorized into different kinds, depending mainly on whether the numerator and/or denominator is null.
Returns 1 if the argument is positive (including inf), -1 if it is negative (including -inf), and 0 if it is null or undefined.
compare x y compares
y and returns 1 if
x is strictly
y, -1 if it is strictly smaller, and 0 if they are
This is a total ordering.
Infinities are ordered in the natural way, while undefined is considered
the smallest of all: undef = undef < -inf <= -inf < x < inf <= inf.
This is consistent with OCaml's handling of floating-point infinities
OCaml's polymorphic comparison will NOT return a result consistent with the ordering of rationals.
Convert to integer by truncation.
Divide_by_zero if the argument is an infinity or undefined.
Z.Overflow if the result does not fit in the destination
In all operations, the result is
undef if one argument is
Other operations can return
undef: such as
Classic prefix and infix
int operators are redefined on