Skip to content

Specific integers #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
25 changes: 24 additions & 1 deletion src/Data/Int.purs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion test/Test/Data/Int.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down