ianmackenzie / elm-units

Simple, safe and convenient unit types and conversions for Elm
https://package.elm-lang.org/packages/ianmackenzie/elm-units/latest/
BSD 3-Clause "New" or "Revised" License
85 stars 14 forks source link

Add Quantity.sign function #58

Closed ianmackenzie closed 3 years ago

ianmackenzie commented 3 years ago

Signature

Quantity.sign : Quantity number units -> number

or perhaps even

Quantity.sign : Quantity number1 units -> number2

for greatest flexibility (e.g. getting the sign of a floating-point quantity as an Int).

ianmackenzie commented 3 years ago

After some discussion on the Elm Slack, decided to go with

Quantity.sign : Quantity Float units -> Float

since getting the sign of an integer quantity is very rarely needed, and this allows better handling of NaN (the implementation can then use Basics.isNaN, which is only available for Float values).

Returning a Float is useful to allow multiplication by the sign later, but in case branching on sign is desired then Quantity.compare can be used instead:

case Quantity.compare quantity Quantity.zero of
    GT -> 
        -- quantity is positive
    LT ->
        -- quantity is negative
    EQ -> 
        -- quantity is zero