MartinSStewart/elm-safe-int - version: 1.0.0

for more information visit the package's GitHub page

Package contains the following modules:

This is a republished version of malaire/elm-safe-int which has been deleted. This new package is unmaintained and is purely meant for unblocking people who depend on it.

elm-safe-int

A safe 54-bit signed integer for use cases where normal Int isn't sufficient and 54-bit range will suffice.

import SafeInt

-- `2^40 / 10`
SafeInt.pow SafeInt.two (SafeInt.new 40)
    |> SafeInt.divBy (SafeInt.new 10)
    |> SafeInt.toInt
    --> Just 109951162777

-- `1 / 0` is undefined
SafeInt.div SafeInt.one SafeInt.zero
    |> SafeInt.toInt
    --> Nothing

Unchecked versions of functions are also provided for use cases where more speed is required at the cost of some lost safety and features.

import SafeInt.Unchecked as Unchecked

-- `2^40 / 10`
Unchecked.pow 2 40
    |> Unchecked.divBy 10
    --> 109951162777

Design Goals

Primary design goal is to fix many problems of normal Int with which:

So in contrast to Int, with SafeInt:

Secondary design goal is speed.

*) Elm compiler doesn't yet support compiling to WebAssembly.

Performance

I'm not interested in doing proper benchmarking, but in some quick benchmarks:

Benchmarking was done 2020-06-07 with Elm 0.19.1 and elm make --optimize and run in Firefox 68.8 in Debian.

Some example benchmarks are included in benchmarks directory of source.

Missing functionality

I don't think it makes sense to implement bitwise operations for SafeInt, but if use case is found I can reconsider this.

As Float doesn't support bitwise operations directly these would need to be simulated somehow.