package colibri2
include module type of Q with type t = Q.t
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 : t
val one : t
val minus_one : t
0, 1, -1.
val inf : t
1/0.
val minus_inf : t
-1/0.
val undef : t
0/0.
val of_int : int -> t
val of_int32 : int32 -> t
val of_int64 : int64 -> t
val of_nativeint : nativeint -> t
Conversions from various integer types.
val of_ints : int -> int -> t
Conversion from an int
numerator and an int
denominator.
val of_float : float -> t
Conversion from a float
. The conversion is exact, and maps NaN to undef
.
val of_string : string -> t
Converts 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 -> bool
Whether the argument is non-infinity and non-undefined.
val sign : t -> int
Returns 1 if the argument is positive (including inf), -1 if it is negative (including -inf), and 0 if it is null or undefined.
Conversions
val to_int : t -> int
val to_int32 : t -> int32
val to_int64 : t -> int64
val to_nativeint : t -> nativeint
Convert 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 -> string
Converts to human-readable, base-10, /
-separated rational.
val to_float : t -> float
Converts to a floating-point number, using the current floating-point rounding mode. With the default rounding mode, the result is the floating-point number closest to the given rational; ties break to even mantissa.
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 print : t -> unit
Prints the argument on the standard output.
val output : Stdlib.out_channel -> t -> unit
Prints the argument on the specified channel. Also intended to be used as %a
format printer in Printf.printf
.
val sprint : unit -> t -> string
To be used as %a
format printer in Printf.sprintf
.
val bprint : Stdlib.Buffer.t -> t -> unit
To be used as %a
format printer in Printf.bprintf
.
val pp_print : Stdlib.Format.formatter -> t -> unit
Prints 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 -> t
Conversion from int
.
val (//) : int -> int -> t
Creates a rational from two int
s.
include Colibri2_popop_lib.Popop_stdlib.Datatype with type t := t
include Colibri2_popop_lib.Popop_stdlib.OrderedHashedType with type t := t
val pp : t Colibri2_popop_lib.Pp.pp
val hash_fold_t : t Base.Hash.folder
module M : Colibri2_popop_lib.Map_intf.PMap with type key = t
module H : Colibri2_popop_lib.Exthtbl.Hashtbl.S with type key = t
val two : t
val of_string_decimal : string -> t
val is_integer : t -> bool
val is_unsigned_integer : int -> t -> bool
is_unsigned_integer size q
checks that q
is an integer that fits in size
bits
val is_zero : t -> bool
val is_not_zero : t -> bool
val gen : t QCheck.Gen.t
val shrink : t QCheck.Shrink.t