File tree Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Original file line number Diff line number Diff line change @@ -8,14 +8,16 @@ module Data.Ord
88 , min , max
99 , clamp
1010 , between
11+ , abs
12+ , signum
1113 , module Data.Ordering
1214 ) where
1315
1416import Data.Eq (class Eq )
1517import Data.Function (on )
1618import Data.Ord.Unsafe (unsafeCompare )
1719import Data.Ordering (Ordering (..))
18- import Data.Ring (negate )
20+ import Data.Ring (class Ring , zero , one , negate )
1921import Data.Unit (Unit )
2022import Data.Void (Void )
2123
@@ -149,3 +151,13 @@ between low hi x
149151 | x < low = false
150152 | x > hi = false
151153 | true = true
154+
155+ -- | The absolute value function. `abs x` is defined as `if x >= zero then x
156+ -- | else negate x`.
157+ abs :: forall a . (Ord a , Ring a ) => a -> a
158+ abs x = if x >= zero then x else negate x
159+
160+ -- | The sign function; always evaluates to either `one` or `negate one`. For
161+ -- | any `x`, we should have `signum x * abs x == x`.
162+ signum :: forall a . (Ord a , Ring a ) => a -> a
163+ signum x = if x >= zero then one else negate one
You can’t perform that action at this time.
0 commit comments