Skip to content

add toInt and toNumber #7

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

Merged
merged 8 commits into from
Apr 8, 2023
Merged
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
2 changes: 2 additions & 0 deletions src/JS/BigInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const fromInt = (n) => BigInt(n);

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

export const toNumber = (n) => Number(n);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this check whether n is in range?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure about this point but I think checking is not necessary because Number() returns +infinity or -infinity (which are valid Purescript numbers) if the bounds are not checked.
By the way, no function in Data.Number (like pow or acos) does checking.
Neither does purescript-bigints' toNumber function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's okay to return +infinity or -infinity but that should be documented. The toNumber export should say “If the BigInt is too positive then the result will be +Infinity. If the BigInt is too negative, then the result will be -Infinity.” or something like that.


export const biAdd = (x) => (y) => x + y;

export const biMul = (x) => (y) => x * y;
Expand Down
13 changes: 13 additions & 0 deletions src/JS/BigInt.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module JS.BigInt
, fromNumber
, fromString
, fromTLInt
, toInt
, toNumber
, asIntN
, asUintN
, toString
Expand All @@ -24,6 +26,7 @@ module JS.BigInt
import Prelude

import Data.Int (Parity(..), Radix)
import Data.Int as Int
import Data.Int (Parity(..), Radix, binary, octal, decimal, hexadecimal) as Exports
import Data.Maybe (Maybe(..))
import Data.Reflectable (class Reflectable, reflectType)
Expand Down Expand Up @@ -79,6 +82,16 @@ foreign import fromTypeLevelInt ∷ String → BigInt
fromTLInt ∷ ∀ i sym. ToString i sym ⇒ Reflectable sym String ⇒ Proxy i → BigInt
fromTLInt _ = fromTypeLevelInt (reflectType (Proxy ∷ Proxy sym))

-- | Convert a `BigInt` to a `Number`.
-- | There may be a loss of precision.
-- | 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`.
foreign import toNumber ∷ BigInt → Number

-- | Convert a `BigInt` to an `Int`.
-- | The `BigInt` must fall within the valid range of values for the `Int` type otherwise `Nothing` is returned.
toInt ∷ BigInt → Maybe Int
toInt = toNumber >>> Int.fromNumber

foreign import biAdd ∷ BigInt → BigInt → BigInt
foreign import biMul ∷ BigInt → BigInt → BigInt

Expand Down
17 changes: 7 additions & 10 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module Test.Main where

import Prelude
import Data.Array.NonEmpty (cons')
import Data.Foldable (fold)
import Data.Maybe (Maybe(..), fromMaybe)
import Debug (spy)
import Effect (Effect)
import Effect.Console (log)
import Js.BigInt.BigInt (BigInt, and, fromInt, fromString, fromTLInt, not, or, pow, shl, shr, toString, xor, even, odd, parity, toStringAs, binary, octal)
import JS.BigInt (BigInt, and, fromInt, fromString, fromTLInt, not, or, pow, shl, shr, toString, xor, even, odd, parity, toInt, toStringAs, binary, octal)
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, ($), (*), (+), (-), (/), (<$>), (<<<), (==))
import Test.Assert (assert)
import Test.QuickCheck (quickCheck)
Expand Down Expand Up @@ -127,15 +128,11 @@ main = do
-- Data.checkEuclideanRing prxBigInt

log "Converting BigInt to Int"
-- assert $ (fromString "0" <#> asIntN 64) == Just 0
-- assert $ (fromString "2137" <#> asIntN 64) == Just 2137
-- assert $ (fromString "-2137" <#> asIntN 64) == Just (-2137)
-- assert $ (fromString "2147483647" <#> asIntN 64) == Just 2147483647
-- assert $ (fromString "2147483648" <#> asIntN 64) == Nothing
-- assert $ (fromString "-2147483648" <#> asIntN 64) == Just (-2147483648)
-- assert $ (fromString "-2147483649" <#> asIntN 64) == Nothing
-- assert $ (fromString "921231231322337203685124775809" <#> asIntN 64) == Nothing
-- assert $ (fromString "-922337203612312312312854775809" <#> asIntN 64) == Nothing
assert $ (fromString "0" >>= toInt) == Just 0
assert $ (fromString "2137" >>= toInt) == Just 2137
assert $ (fromString "-2137" >>= toInt) == Just (-2137)
assert $ (fromString "921231231322337203685124775809" >>= toInt) == Nothing
assert $ (fromString "-922337203612312312312854775809" >>= toInt) == Nothing
-- quickCheck (\a b c ->
-- let x = add (fromInt a) (add (fromInt b) (fromInt c))
-- in case asIntN 64 x of
Expand Down