Module Mu.Q

Rational numbers (ℚ).

Rational numbers

type t

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.

Constants

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.

Predicates and comparisons

type kind =
| Nan
| Neg_infinity
| Infinity
| Zero
| Non_zero

The type for kinds of rational numbers.

val kind : t -> kind

kind r is the kind of r.

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 is_nan : t -> bool

is_nen r is true iff r is nan.

val sign : t -> int

sign r is the sign of r. This is either -1, 0 or 1. The sign of Nan is 0.

val compare : t -> t -> int

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.

val equal : t -> t -> bool

equal r0 r1 is compare r0 r1 = 0. nan is equal to itself.

val (=) : t -> t -> bool

r0 = r1 is compare r0 r1 = 0.

val (<) : t -> t -> bool

r0 < r1 is compare r0 r1 < 0.

val (<=) : t -> t -> bool

r0 <= r1 is compare r0 r1 <= 0.

val (>) : t -> t -> bool

r0 > r1 is compare r0 r1 > 0.

val (>=) : t -> t -> bool

r0 >= r1 is compare r0 r1 >= 0.

val min : t -> t -> t

min r0 r1 is the smallest of r0 and r1.

val max : t -> t -> t

max r0 r1 is the largest of r0 and r1.

Arithmetic

Note. All these operations return nan if they get a nan argument.

val neg : t -> t

neg r is r negated.

val abs : t -> t

abs r is r's absolute value.

val add : t -> t -> t

add r0 r1 is r1 added to r0.

val sub : t -> t -> t

sub r0 r1 is r1 subtracted from r0.

val mul : t -> t -> t

mul r0 r1 is r0 multiplied by r1.

val inv : t -> t

inv r is the inverse of r.

val div : t -> t -> t

div r0 r1 is r0 divided by r1.

val (~-) : t -> t

~- r is neg r.

val (+) : t -> t -> t

r0 + r1 is add r0 r1.

val (-) : t -> t -> t

r0 - r1 is sub r0 r1.

val (*) : t -> t -> t

r0 * r1 is mul r0 r1.

val (/) : t -> t -> t

r0 / r1 is div r0 r1.

Converting

val of_int : int -> t

of_int n is n // 1.

val to_int : t -> int

to_int r is num r / den r. Raises Division_by_zero if den r is 0; that is on nan, neg_infinity, infinity.

val safe_to_int : t -> int option

safe_to_int is like to_int 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.

Formatting

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.