diff --git a/CHANGELOG.md b/CHANGELOG.md index c3e590c..9c8feb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: New features: +- Added `minInt` and `maxInt` (#54 by @JamieBallingall) Bugfixes: diff --git a/src/Data/Int.purs b/src/Data/Int.purs index c637fc8..4e0c8e6 100644 --- a/src/Data/Int.purs +++ b/src/Data/Int.purs @@ -1,5 +1,9 @@ +-- | Functions for working with PureScript's builtin `Int` type. A `Int` +-- | is a signeed 32-bit integer. module Data.Int - ( fromNumber + ( minInt + , maxInt + , fromNumber , ceil , floor , trunc @@ -31,6 +35,25 @@ import Data.Maybe (Maybe(..), fromMaybe) import Data.Number (isFinite) import Data.Number as Number +-- | The most negative `Int`, equal to -2^31. +-- | ```purs +-- | > minInt +-- | -2147483648 +-- | ``` +minInt :: Int +minInt = -0x80000000 + +-- | The most positive `Int`, equal to 2^31 - 1. +-- | ```purs +-- | > maxInt +-- | 2147483647 +-- | +-- | > maxInt + 1 == minInt +-- | true +-- | ``` +maxInt :: Int +maxInt = 0x7FFFFFFF + -- | Creates an `Int` from a `Number` value. The number must already be an -- | integer and fall within the valid range of values for the `Int` type -- | otherwise `Nothing` is returned. diff --git a/test/Test/Data/Int.purs b/test/Test/Data/Int.purs index 1eabc6b..edafadd 100644 --- a/test/Test/Data/Int.purs +++ b/test/Test/Data/Int.purs @@ -2,7 +2,7 @@ module Test.Data.Int (testInt) where import Prelude -import Data.Int (binary, ceil, even, floor, fromNumber, fromString, fromStringAs, hexadecimal, octal, odd, parity, pow, quot, radix, rem, round, toNumber, toStringAs) +import Data.Int (binary, ceil, even, floor, fromNumber, fromString, fromStringAs, hexadecimal, maxInt, minInt, octal, odd, parity, pow, quot, radix, rem, round, toNumber, toStringAs) import Data.Maybe (Maybe(..), fromJust) import Effect (Effect) import Effect.Console (log) @@ -13,6 +13,15 @@ import Test.Assert (assert) testInt :: Effect Unit testInt = do + log "minInt should be -2147483648" + assert $ minInt == -2147483648 + + log "maxInt should be 2147483647" + assert $ maxInt == 2147483647 + + log "maxInt + 1 should equal minInt" + assert $ maxInt + 1 == minInt + log "fromNumber should coerce integer values" assert $ fromNumber 1.0 == Just 1 assert $ fromNumber 42.0 == Just 42