Skip to content

Commit

Permalink
Merge pull request #5 from Unisay/master
Browse files Browse the repository at this point in the history
toBase :: Int -> BigInt -> String
  • Loading branch information
sharkdp authored Sep 24, 2017
2 parents 15b7274 + b6c2a21 commit 950bce2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/Data/BigInt.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ exports["fromBase'"] = function(just) {

exports.fromInt = bigInt;

exports.toString = function(x) {
return x.toString();
exports.toBase = function(base) {
return function (x) {
return x.toString(base);
};
};

exports.toNumber = function(x) {
Expand Down Expand Up @@ -124,4 +126,3 @@ exports.shr = function(x) {
return x.shiftRight(n);
};
};

7 changes: 6 additions & 1 deletion src/Data/BigInt.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Data.BigInt
, fromBase
, fromInt
, toString
, toBase
, abs
, even
, odd
Expand Down Expand Up @@ -112,7 +113,11 @@ instance ordBigInt :: Ord BigInt where
_ -> LT

-- | A decimal representation of the `BigInt` as a `String`.
foreign import toString :: BigInt -> String
toString :: BigInt -> String
toString = toBase 10

-- | A base N representation of the `BigInt` as a `String`.
foreign import toBase :: Int -> BigInt -> String

instance showBigInt :: Show BigInt where
show x = "fromString \"" <> toString x <> "\""
Expand Down
18 changes: 11 additions & 7 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Control.Monad.Eff.Console (CONSOLE, log)
import Control.Monad.Eff.Exception (EXCEPTION)
import Control.Monad.Eff.Random (RANDOM)
import Data.Array (filter, range)
import Data.BigInt (BigInt, abs, fromInt, prime, pow, odd, even, fromString, toNumber, fromBase, toString, not, or, xor, and, shl, shr)
import Data.BigInt (BigInt, abs, fromInt, prime, pow, odd, even, fromString, toNumber, fromBase, toBase, toString, not, or, xor, and, shl, shr)
import Data.Foldable (fold)
import Data.Int as Int
import Data.Maybe (Maybe(..), fromMaybe)
Expand All @@ -31,10 +31,10 @@ runSmallInt (SmallInt n) = n
-- | Arbitrary instance for BigInt
newtype TestBigInt = TestBigInt BigInt
derive newtype instance eqTestBigInt :: Eq TestBigInt
derive newtype instance ordTestBigInt :: Ord TestBigInt
derive newtype instance ordTestBigInt :: Ord TestBigInt
derive newtype instance semiringTestBigInt :: Semiring TestBigInt
derive newtype instance ringTestBigInt :: Ring TestBigInt
derive newtype instance commutativeRingTestBigInt :: CommutativeRing TestBigInt
derive newtype instance commutativeRingTestBigInt :: CommutativeRing TestBigInt
derive newtype instance euclideanRingTestBigInt :: EuclideanRing TestBigInt

instance arbitraryBigInt :: Arbitrary TestBigInt where
Expand Down Expand Up @@ -80,6 +80,11 @@ main = do
assert $ fromBase 2 "100" == Just four
assert $ fromBase 16 "ff" == fromString "255"

log "Rendering bigints as strings with a different base"
assert $ toBase 2 four == "100"
assert $ (toBase 16 <$> fromString "255") == Just "ff"
assert $ toString (fromInt 12345) == "12345"

log "Conversions between String, Int and BigInt should not loose precision"
quickCheck (\n -> fromString (show n) == Just (fromInt n))
quickCheck (\n -> Int.toNumber n == toNumber (fromInt n))
Expand Down Expand Up @@ -124,12 +129,11 @@ main = do
log "Shifting"
assert $ shl two one == four
assert $ shr two one == one

let prxBigInt = ProxyProxy TestBigInt
Data.checkEq prxBigInt
Data.checkOrd prxBigInt
Data.checkSemiring prxBigInt
Data.checkSemiring prxBigInt
Data.checkRing prxBigInt
-- Data.checkEuclideanRing prxBigInt
-- Data.checkEuclideanRing prxBigInt
Data.checkCommutativeRing prxBigInt

0 comments on commit 950bce2

Please sign in to comment.