Skip to content

Commit e026a78

Browse files
authored
add toInt and toNumber (#7)
1 parent 320db74 commit e026a78

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

src/JS/BigInt.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export const fromInt = (n) => BigInt(n);
1919

2020
export const fromTypeLevelInt = (str) => BigInt(str);
2121

22+
export const toNumber = (n) => Number(n);
23+
2224
export const biAdd = (x) => (y) => x + y;
2325

2426
export const biMul = (x) => (y) => x * y;

src/JS/BigInt.purs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module JS.BigInt
44
, fromNumber
55
, fromString
66
, fromTLInt
7+
, toInt
8+
, toNumber
79
, asIntN
810
, asUintN
911
, toString
@@ -24,6 +26,7 @@ module JS.BigInt
2426
import Prelude
2527

2628
import Data.Int (Parity(..), Radix)
29+
import Data.Int as Int
2730
import Data.Int (Parity(..), Radix, binary, octal, decimal, hexadecimal) as Exports
2831
import Data.Maybe (Maybe(..))
2932
import Data.Reflectable (class Reflectable, reflectType)
@@ -79,6 +82,16 @@ foreign import fromTypeLevelInt ∷ String → BigInt
7982
fromTLInt i sym. ToString i sym Reflectable sym String Proxy i BigInt
8083
fromTLInt _ = fromTypeLevelInt (reflectType (Proxy Proxy sym))
8184

85+
-- | Convert a `BigInt` to a `Number`.
86+
-- | There may be a loss of precision.
87+
-- | If the `BigInt` is too large then the result will be `+Infinity`. If the `BigInt` is too small (too negative) then the result will be `-Infinity`.
88+
foreign import toNumber BigInt Number
89+
90+
-- | Convert a `BigInt` to an `Int`.
91+
-- | The `BigInt` must fall within the valid range of values for the `Int` type otherwise `Nothing` is returned.
92+
toInt BigInt Maybe Int
93+
toInt = toNumber >>> Int.fromNumber
94+
8295
foreign import biAdd BigInt BigInt BigInt
8396
foreign import biMul BigInt BigInt BigInt
8497

test/Main.purs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
module Test.Main where
22

3+
import Prelude
34
import Data.Array.NonEmpty (cons')
45
import Data.Foldable (fold)
56
import Data.Maybe (Maybe(..), fromMaybe)
67
import Debug (spy)
78
import Effect (Effect)
89
import Effect.Console (log)
9-
import Js.BigInt.BigInt (BigInt, and, fromInt, fromString, fromTLInt, not, or, pow, shl, shr, toString, xor, even, odd, parity, toStringAs, binary, octal)
10+
import JS.BigInt (BigInt, and, fromInt, fromString, fromTLInt, not, or, pow, shl, shr, toString, xor, even, odd, parity, toInt, toStringAs, binary, octal)
1011
import Prelude (class CommutativeRing, class Eq, class EuclideanRing, class Ord, class Ring, class Semiring, Unit, bind, compare, discard, identity, map, mod, negate, one, pure, show, zero, ($), (*), (+), (-), (/), (<$>), (<<<), (==))
1112
import Test.Assert (assert)
1213
import Test.QuickCheck (quickCheck)
@@ -127,15 +128,11 @@ main = do
127128
-- Data.checkEuclideanRing prxBigInt
128129

129130
log "Converting BigInt to Int"
130-
-- assert $ (fromString "0" <#> asIntN 64) == Just 0
131-
-- assert $ (fromString "2137" <#> asIntN 64) == Just 2137
132-
-- assert $ (fromString "-2137" <#> asIntN 64) == Just (-2137)
133-
-- assert $ (fromString "2147483647" <#> asIntN 64) == Just 2147483647
134-
-- assert $ (fromString "2147483648" <#> asIntN 64) == Nothing
135-
-- assert $ (fromString "-2147483648" <#> asIntN 64) == Just (-2147483648)
136-
-- assert $ (fromString "-2147483649" <#> asIntN 64) == Nothing
137-
-- assert $ (fromString "921231231322337203685124775809" <#> asIntN 64) == Nothing
138-
-- assert $ (fromString "-922337203612312312312854775809" <#> asIntN 64) == Nothing
131+
assert $ (fromString "0" >>= toInt) == Just 0
132+
assert $ (fromString "2137" >>= toInt) == Just 2137
133+
assert $ (fromString "-2137" >>= toInt) == Just (-2137)
134+
assert $ (fromString "921231231322337203685124775809" >>= toInt) == Nothing
135+
assert $ (fromString "-922337203612312312312854775809" >>= toInt) == Nothing
139136
-- quickCheck (\a b c ->
140137
-- let x = add (fromInt a) (add (fromInt b) (fromInt c))
141138
-- in case asIntN 64 x of

0 commit comments

Comments
 (0)