The problems with numbers in Elm

I’m currently testing a (private) type SafeInt which is restricted to integers in JavaScript safe integer range and defined as

type SafeInt
    = Defined Float
    | Undefined

Undefined works a bit like NaN, but any operation which would result in unrepresentable value causes Undefined, including overflows:

-- `2 ^ 55` is undefined
SafeInt.pow (SafeInt.fromInt 2) (SafeInt.fromInt 55)
    |> SafeInt.toInt
    --> Nothing

While this works and is easy to implement, it’s quite slow - operations can be 100 times slower than with Int. But for my use cases this is good enough.

ps. I’m not using actual NaN internally since that would result in inconsistent behavior because of bug Inconsistant equality for objects containing NaN.

1 Like