package octez-proto-libs
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=c6df840ebbf115e454db949028c595bec558a59a66cade73b52a6d099d6fa4d4
sha512=d8aee903b9fe130d73176bc8ec38b78c9ff65317da3cb4f3415f09af0c625b4384e7498201fdb61aa39086a7d5d409d0ab3423f9bc3ab989a680cf444a79bc13
doc/octez-proto-libs.protocol-environment/Tezos_protocol_environment/V8/Make/Q/index.html
Module Make.Q
Rationals.
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).
Types
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: inf (1/0), -inf (-1/0) and undef (0/0).
Construction
make num den constructs a new rational equal to num/den. It takes care of putting the rational in canonical form.
val zero : tval one : tval minus_one : t0, 1, -1.
val inf : t1/0.
val minus_inf : t-1/0.
val undef : t0/0.
val of_int : int -> tval of_int32 : int32 -> tval of_int64 : int64 -> tConversions from various integer types.
val of_ints : int -> int -> tConversion from an int numerator and an int denominator.
val of_string : string -> tConverts a string to a rational. Plain integers, / separated integer ratios (with optional sign), decimal point and scientific notations are understood. Additionally, the special inf, -inf, and undef are recognized (they can also be typeset respectively as 1/0, -1/0, 0/0).
Inspection
Testing
Rationals can be categorized into different kinds, depending mainly on whether the numerator and/or denominator is null.
val is_real : t -> boolWhether the argument is non-infinity and non-undefined.
val sign : t -> intReturns 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 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.
Equality testing. Unlike compare, this follows IEEE semantics: undef <> undef.
Conversions
val to_int : t -> intval to_int32 : t -> int32val to_int64 : t -> int64Convert to integer by truncation. Raises a Divide_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 -> stringConverts to human-readable, base-10, /-separated rational.
Arithmetic operations
In all operations, the result is undef if one argument is undef. Other operations can return undef: such as inf-inf, inf*0, 0/0.
Printing
val pp_print : Format.formatter -> t -> unitPrints the argument on the specified formatter. Also intended to be used as %a format printer in Format.printf.
Prefix and infix operators
Classic prefix and infix int operators are redefined on t.
val (~$) : int -> tConversion from int.
val (//) : int -> int -> tCreates a rational from two ints.