## 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`

.

`v num den`

is the normalized rational `num / den`

.

`val (#/) : int -> int -> t`

`num r`

is the numerator of `r`

.

`den r`

is the denominator of `r`

.

## Constants

## Predicates and comparisons

`type kind = `

`| Nan` |

`| Neg_infinity` |

`| Infinity` |

`| Zero` |

`| Non_zero` |

The type for kinds of rational numbers.

`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_nen r`

is `true`

iff `r`

is `nan`

.

`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.

`r0 = r1`

is `compare r0 r1 = 0`

.

`r0 < r1`

is `compare r0 r1 < 0`

.

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

`r0 <= r1`

is `compare r0 r1 <= 0`

.

`r0 > r1`

is `compare r0 r1 > 0`

.

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

`r0 >= r1`

is `compare r0 r1 >= 0`

.

`min r0 r1`

is the smallest of `r0`

and `r1`

.

`max r0 r1`

is the largest of `r0`

and `r1`

.

## Arithmetic

**Note.** All these operations return `nan`

if they get a `nan`

argument.

`abs r`

is `r`

's absolute value.

`add r0 r1`

is `r1`

added to `r0`

.

`sub r0 r1`

is `r1`

subtracted from `r0`

.

`mul r0 r1`

is `r0`

multiplied by `r1`

.

`inv r`

is the inverse of `r`

.

`div r0 r1`

is `r0`

divided by `r1`

.

## Converting

`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.