Skip to content

Commit

Permalink
Add realize function for converting fractions to dividable types
Browse files Browse the repository at this point in the history
  • Loading branch information
Izaakwltn committed Oct 16, 2024
1 parent 60d3e81 commit 78529c2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/intro-to-coalton.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ All of these cases are sufficiently common that we provide a few shorthands:

- `floor/`, `ceiling/`, and `round/` for integer-to-integer division, and

- `fromfrac` for converting a `Fraction` into a type with a `Dividable Integer :a` instance (this conversion may result in precision loss)

## Lists

Coalton uses Lisp lists under the hood. Lists can be constructed with `make-list`.
Expand Down
12 changes: 10 additions & 2 deletions library/math/real.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
#:inexact/
#:floor/
#:ceiling/
#:round/))
#:round/
#:fromfrac))

(in-package #:coalton-library/math/real)

Expand Down Expand Up @@ -264,7 +265,14 @@ Note: This does *not* divide double-float arguments."
(declare round/ (Integer -> Integer -> Integer))
(define (round/ a b)
"Divide two integers and round the quotient."
(round (exact/ a b))))
(round (exact/ a b)))

(declare fromfrac (Dividable Integer :a => Fraction -> :a))
(define (fromfrac q)
"Converts a fraction to a type `:a` with an instance of `Dividable Integer :a`, similar to `Num`'s `fromint`.
This may result in loss of fidelity."
(general/ (numerator q) (denominator q))))

#+sb-package-locks
(sb-ext:lock-package "COALTON-LIBRARY/MATH/REAL")

0 comments on commit 78529c2

Please sign in to comment.