module Q:sig..end
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).
type t = {
|
num : |
(* |
Numerator.
| *) |
|
den : |
(* |
Denominator, >= 0
| *) |
inf (1/0), -inf (-1/0)
and undef (0/0).val make : Z.t -> Z.t -> tmake num den constructs a new rational equal to num/den.
It takes care of putting the rational in canonical form.val zero : t
val one : t
val minus_one : tval inf : tval minus_inf : tval undef : tval of_bigint : Z.t -> t
val of_int : int -> t
val of_int32 : int32 -> t
val of_int64 : int64 -> t
val of_nativeint : nativeint -> tval of_ints : int -> int -> tint numerator and an int denominator.val of_float : float -> tfloat.
The conversion is exact, and maps NaN to undef.val of_string : string -> t/ separated integer ratios (with optional sign) are
understood.
Additionally, the special inf, -inf, and undef are recognized
(they can also be typeset respectively as 1/0, -1/0, 0/0).val num : t -> Z.tval den : t -> Z.ttype kind =
| |
ZERO |
(* |
0
| *) |
| |
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
| *) |
val classify : t -> kindval is_real : t -> boolval sign : t -> intval compare : t -> t -> intcompare x y compares x to y and returns 1 if x is strictly
greater that y, -1 if it is strictly smaller, and 0 if they are
equal.
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
and NaN.
OCaml's polymorphic comparison will NOT return a result consistent with
the ordering of rationals.
val equal : t -> t -> boolcompare; in particular, undef=undef.val min : t -> t -> tval max : t -> t -> tval leq : t -> t -> boolval geq : t -> t -> boolval lt : t -> t -> boolval gt : t -> t -> boolval to_bigint : t -> Z.t
val to_int : t -> int
val to_int32 : t -> int32
val to_int64 : t -> int64
val to_nativeint : t -> nativeintDivide_by_zero if the argument is an infinity or undefined.
Raises a Z.Overflow if the result does not fit in the destination
type.val to_string : t -> string/-separated rational.val to_float : t -> floatundef if one argument is undef.
Other operations can return undef: such as inf-inf, inf*0, 0/0.val neg : t -> tval abs : t -> tval add : t -> t -> tval sub : t -> t -> tsub x y = add x (neg y).val mul : t -> t -> tval inv : t -> tinv 0 is defined, and equals inf.val div : t -> t -> tdiv x y = mul x (inv y), and inv x = div one x.val mul_2exp : t -> int -> tmul_2exp x n multiplies x by 2 to the power of n.val div_2exp : t -> int -> tdiv_2exp x n divides x by 2 to the power of n.val print : t -> unitval output : Pervasives.out_channel -> t -> unit%a format printer in Printf.printf.val sprint : unit -> t -> string%a format printer in Printf.sprintf.val bprint : Buffer.t -> t -> unit%a format printer in Printf.bprintf.val pp_print : Format.formatter -> t -> unit%a format printer in Format.printf.int operators are redefined on t.val (~-) : t -> tneg.val (~+) : t -> tval (+) : t -> t -> tadd.val (-) : t -> t -> tsub.val ( * ) : t -> t -> tmul.val (/) : t -> t -> tdiv.val (lsl) : t -> int -> tmul_2exp.val (asr) : t -> int -> tshift_right.val (~$) : int -> tint.val (//) : int -> int -> tints.val (~$$) : Z.t -> tZ.t.val (///) : Z.t -> Z.t -> tZ.t.val (=) : t -> t -> boolequal.val (<) : t -> t -> boollt.val (>) : t -> t -> boolgt.val (<=) : t -> t -> boolleq.val (>=) : t -> t -> boolgeq.val (<>) : t -> t -> boola <> b is equivalent to not (equal a b).