Mu.Q
Rational numbers (ℚ).
The type for rational numbers.
Values of this type are normalized. The numerator and denominator have no common factor and the denominator is non-negative.
The special rationals 1/0
, -1/0
and 0/0
repectively denote infinity
, neg_infinity
and nan
.
val v : int -> int -> t
v num den
is the normalized rational num / den
.
val int : int -> t
int n
is v n 1
.
val (#/) : int -> int -> t
n #/ d
is v num den
.
val num : t -> int
num r
is the numerator of r
.
val den : t -> int
den r
is the denominator of r
.
val zero : t
zero
is 0/1
.
val one : t
one
is 1/1
.
val minus_one : t
minus_one
is -1/1
.
val infinity : t
infinity
is 1/0
.
val neg_infinity : t
neg_infinity
is -1/0
.
val nan : t
nan
is 0/0
.
val is_finite : t -> bool
is_finite r
is true
iff r
's kind is either Zero
or Non_zero
.
val is_infinite : t -> bool
is_infinite r
is true
iff r
is either infinity
or neg_infinity
.
val sign : t -> int
sign r
is the sign of r
. This is either -1
, 0
or 1
. The sign of Nan
is 0
.
compare r0 r1
is a total order on r0
and r1
. nan
is the smallest value and equal to itself, the rest is ordered as expected.
Note. All these operations return nan
if they get a nan
argument.
val of_int : int -> t
of_int n
is n // 1
.
val to_int_towards_zero : t -> int
to_int_towards_zero r
is num r / den r
. Divides r
and moves towards zero to the nearest int. Raises Division_by_zero
if den r
is 0
; that is on nan
, neg_infinity
, infinity
.
val to_int_away_from_zero : t -> int
to_int_away_from_zero r
divides r
moves away from zero to the nearest int. Raises Division_by_zero
if den r
is 0
; that is on nan
, neg_infinity
, infinity
.
val checked_to_int_towards_zero : t -> int option
checked_to_int_towards_zero
is like to_int_towards_zero
but is None
if den r
is 0
.
val checked_to_int_away_from_zero : t -> int option
checked_to_int_towards_zero
is like to_int_away_from_zero
but is None
if den r
is 0
.
val to_float : t -> float
to_float r
is (float (num r) /. (float (den r))
. Special values are mapped to corresponding floating point special values.
val pp : Stdlib.Format.formatter -> t -> unit
pp
is a formatter for rationals.
val pp_kind : Stdlib.Format.formatter -> kind -> unit
pp_kind
is a formatter for rational kinds.