Announcing elm-integer
Tags: elm, math
Published: 2023-12-04 10:30:00

---
title: Announcing elm-integer
published: true
description: An announcement for the first release of elm-integer.
tags: elm, math
---

I am happy to announce the release of [`elm-integer`](https://github.com/dwayne/elm-integer/tree/1.0.0).

`elm-integer` is a pure Elm library for computing with the [integers](https://en.wikipedia.org/wiki/Integer), ℤ = { ..., -2, -1, 0, 1, 2, ... }. It uses [arbitrary-precision arithmetic](https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic) so the magnitude of the numbers you can compute with is only limited by the available memory. It is extensively [tested for correctness](https://github.com/dwayne/elm-integer/blob/1.0.0/tests/Test/Integer.elm) and has [reasonable performance characteristics](https://github.com/dwayne/elm-integer/tree/1.0.0#performance).
## Try it out

I built an [integer calculator](https://dwayne.github.io/elm-integer/) that allows you to test out all the **integer input formats**, **arithmetic operations**, and **integer output formats** that's supported by `elm-integer`. Feel free to use the calculator to explore the limits of the library.

## Compared to `cmditch/elm-bigint`

### Commonly used small integers are named

The integers from `-10` to `10` are already named. In particular, `zero`, `one`, `two`, `ten`, and `negativeOne` are available for immediate use.

### Large integers are easy to create

[`fromSafeString`](https://package.elm-lang.org/packages/dwayne/elm-integer/1.0.0/Integer#fromSafeString) is useful for establishing large constants in a calculation.
### Multiple integer input formats are supported

Any base from 2 to 36 is supported using [`fromBaseBString`](https://package.elm-lang.org/packages/dwayne/elm-integer/1.0.0/Integer#fromBaseBString) and shortcuts are provided for binary, octal, decimal, and hexadecimal input formats.
### Multiple integer output formats are supported

Any base from 2 to 36 is supported using [`toBaseBString`](https://package.elm-lang.org/packages/dwayne/elm-integer/1.0.0/Integer#toBaseBString) and shortcuts are provided for binary, octal, decimal, and hexadecimal output formats.
### There are useful predicates

With these predicates it's easy to check if your integer is zero (`== 0`), negative (`< 0`), non-negative (`>= 0`), positive (`> 0`), or non-positive (`<= 0`).

`==` and `/=` work as expected so if you wanted to know if your integer, `z`, is equal to `-1` then `z == negativeOne` is all you have to do.
### Arithmetic is performant

This library is built on [`elm-natural`](https://github.com/dwayne/elm-natural/tree/1.1.1) which currently [outperforms](https://github.com/dwayne/elm-natural/tree/1.1.1/benchmarks#comparison) `cmditch/elm-bigint` in multiplication, division with remainder, and exponentiation.
### `divModBy` defines Euclidean division

`cmditch/elm-bigint`'s `divmod` operation defines truncated division (or T-division) according to Daan Leijen's paper, [Division and Modulus for Computer Scientists](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/divmodnote-letter.pdf). It is the equivalent of `quot`/`rem` in Haskell. No equivalent of Euclidean division is provided.

**N.B.** *You can [check out this Ellie of `divmod`](https://ellie-app.com/pBRQsYfTQ8ra1) to confirm the above statements.*

However, [Boute](https://dl.acm.org/doi/10.1145/128861.128862) argues that [Euclidean division](https://en.wikipedia.org/wiki/Euclidean_division) is superior to the other definitions of the **div** and **mod** functions in terms of regularity and useful mathematical properties.

Hence, [`divModBy`](https://package.elm-lang.org/packages/dwayne/elm-integer/1.0.0/Integer#divModBy) in `elm-integer` defines Euclidean division and truncated division is provided by [`quotRemBy`](https://package.elm-lang.org/packages/dwayne/elm-integer/1.0.0/Integer#quotRemBy).
## Feedback is appreciated

If you happen to use the library in an application, I would greatly appreciate your feedback.

Thank you for reading!