# Rational

A module for rational numbers in Carp.

Fractions are expressed as pairs of integers: a numerator and a denominator. Mathematical operations and conversion primitives to and from integers and floating point numbers are provided.

```
(load "git@github.com:carpentry-org/rational@0.4")
(Rational.new 22 12) ; => (Rational 11/6)
(* (Rational.new 22 12) (Rational.new 1 6)) ; => (Rational 2/1)
(to-int (Rational.new 2 1)) ; => 2
(to-float (Rational.new 1 4)) ; => 0.25
(to-double (Rational.new 1 4)) ; => 0.25
```

### denominator

(Fn [(Ref Rational a)] (Ref Int a))

gets the `denominator`

property of a `Rational`

.

### div

(Fn [(Ref Rational a), (Ref Rational b)] Rational)

(div a b)

divides two `Rational`

s `a`

and `b`

.

### modulo

(Fn [(Ref Rational a), (Ref Rational b)] Rational)

(modulo a b)

calculates the modulo of two `Rational`

s `a`

and `b`

.

### mul

(Fn [(Ref Rational a), (Ref Rational b)] Rational)

(mul a b)

multiplies two `Rational`

s `a`

and `b`

.

### new

(Fn [Int, Int] Rational)

(new n d)

builds a `Rational`

from a numerator `n`

and a denominator `d`

.

### numerator

(Fn [(Ref Rational a)] (Ref Int a))

gets the `numerator`

property of a `Rational`

.

### set-denominator

(Fn [Rational, Int] Rational)

sets the `denominator`

property of a `Rational`

.

### set-denominator!

(Fn [(Ref Rational a), Int] ())

sets the `denominator`

property of a `Rational`

in place.

### set-numerator!

(Fn [(Ref Rational a), Int] ())

sets the `numerator`

property of a `Rational`

in place.

### str

(Fn [(Ref Rational a)] String)

(str r)

stringifies the `Rational`

`r`

. The format is
`(Rational <numerator>/<denominator>)`

.

### sub

(Fn [(Ref Rational a), (Ref Rational b)] Rational)

(sub a b)

subtracts two `Rational`

s `a`

and `b`

.

### to-double

(Fn [(Ref Rational a)] Double)

(to-double r)

converts a `Rational`

`r`

to a `Double`

.

This function might incur a precision loss.

### to-float

(Fn [(Ref Rational a)] Float)

(to-float r)

converts a `Rational`

`r`

to a `Float`

.

This function might incur a precision loss.

### to-int

(Fn [(Ref Rational a)] Int)

(to-int r)

converts a `Rational`

`r`

to an `Integer`

.

This function might incur a precision loss.

### update-denominator

(Fn [Rational, (Ref (Fn [Int] Int a) b)] Rational)

updates the `denominator`

property of a `Rational`

using a function `f`

.

### update-numerator

(Fn [Rational, (Ref (Fn [Int] Int a) b)] Rational)

updates the `numerator`

property of a `Rational`

using a function `f`

.