From 612b5bbe11c56ee04eba952e3d7b0ebe88039744 Mon Sep 17 00:00:00 2001 From: Ryan Rempel Date: Sun, 10 Apr 2016 09:38:40 -0500 Subject: [PATCH] Add `abs`. Closes #2. --- src/Data/Int53.purs | 14 ++++++++++++-- test/Test/Main.purs | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Data/Int53.purs b/src/Data/Int53.purs index 519abdf..b6d3002 100644 --- a/src/Data/Int53.purs +++ b/src/Data/Int53.purs @@ -19,7 +19,8 @@ -- | etc., in the usual way, since the ordinary classes for arithmetic are -- | implemented. It should feel pretty much like using an ordinary integer. -- | --- | There are also functions for [`even`](#v:even), [`odd`](#v:odd), and [`pow`](#v:pow). +-- | There are also functions for [`even`](#v:even), [`odd`](#v:odd), [`pow`](#v:pow), +-- | and [`abs`](#v:abs). -- | -- | ### Converting an `Int53` to something else -- | @@ -44,7 +45,7 @@ module Data.Int53 , fromString, toString , ceil, floor, round, truncate , even, odd - , pow + , pow, abs ) where @@ -312,6 +313,15 @@ pow (Int53 base) (Int53 exponent) = else Int53 $ Math.pow base exponent +-- | Takes the absolute value. +-- | +-- | abs (fromInt 2) == (fromInt 2) +-- | abs (fromInt (-2)) == (fromInt 2) +-- | abs (fromInt 0) == (fromInt 0) +abs :: Int53 -> Int53 +abs (Int53 i) = Int53 $ Math.abs i + + -- | A class which allows a function to accept eitner an `Int` or an `Int53`, -- | work with `Int53` internally, and then return whatever type was provided. -- | diff --git a/test/Test/Main.purs b/test/Test/Main.purs index b728dff..39e4ddb 100644 --- a/test/Test/Main.purs +++ b/test/Test/Main.purs @@ -134,6 +134,11 @@ main = runTest do pow (fromInt 0) (fromInt 0) ==> (fromInt 1) pow (fromInt 2) (fromInt (-2)) ==> (fromInt 0) + test "abs" do + abs (fromInt 2) ==> (fromInt 2) + abs (fromInt (-2)) ==> (fromInt 2) + abs (fromInt 0) ==> (fromInt 0) + test "top" do ((topInt53 - one) + one) ==> topInt53