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

### =

defn

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

```                    (= a b)
```

compares two `Rational`s `a` and `b`.

defn

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

```                    (add a b)
```

adds two `Rational`s `a` and `b`.

### copy

instantiate

(Fn [(Ref Rational a)] Rational)

copies a `Rational`.

### denominator

instantiate

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

gets the `denominator` property of a `Rational`.

### div

defn

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

```                    (div a b)
```

divides two `Rational`s `a` and `b`.

### from-double

defn

(Fn [Double] Rational)

```                    (from-double f)
```

builds a `Rational` from a `Double` `d`.

### from-float

defn

(Fn [Float] Rational)

```                    (from-float f)
```

builds a `Rational` from a `Float` `f`.

### from-int

defn

(Fn [Int] Rational)

```                    (from-int i)
```

builds a `Rational` from an `Integer` `i`.

### hash

defn

(Fn [(Ref Rational a)] Int)

```                    (hash r)
```

calculates the hash of the `Rational` `r`.

### modulo

defn

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

```                    (modulo a b)
```

calculates the modulo of two `Rational`s `a` and `b`.

### mul

defn

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

```                    (mul a b)
```

multiplies two `Rational`s `a` and `b`.

### new

defn

(Fn [Int, Int] Rational)

```                    (new n d)
```

builds a `Rational` from a numerator `n` and a denominator `d`.

### numerator

instantiate

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

gets the `numerator` property of a `Rational`.

### prn

instantiate

(Fn [(Ref Rational a)] String)

converts a `Rational` to a string.

### set-denominator

instantiate

(Fn [Rational, Int] Rational)

sets the `denominator` property of a `Rational`.

### set-denominator!

instantiate

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

sets the `denominator` property of a `Rational` in place.

### set-numerator

instantiate

(Fn [Rational, Int] Rational)

sets the `numerator` property of a `Rational`.

### set-numerator!

instantiate

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

sets the `numerator` property of a `Rational` in place.

### str

defn

(Fn [(Ref Rational a)] String)

```                    (str r)
```

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

### sub

defn

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

```                    (sub a b)
```

subtracts two `Rational`s `a` and `b`.

### to-double

defn

(Fn [(Ref Rational a)] Double)

```                    (to-double r)
```

converts a `Rational` `r` to a `Double`.

This function might incur a precision loss.

### to-float

defn

(Fn [(Ref Rational a)] Float)

```                    (to-float r)
```

converts a `Rational` `r` to a `Float`.

This function might incur a precision loss.

### to-int

defn

(Fn [(Ref Rational a)] Int)

```                    (to-int r)
```

converts a `Rational` `r` to an `Integer`.

This function might incur a precision loss.

### update-denominator

instantiate

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

updates the `denominator` property of a `Rational` using a function `f`.

### update-numerator

instantiate

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

updates the `numerator` property of a `Rational` using a function `f`.

### zero

defn

(Fn [] Rational)

```                    (zero)
```