From b6c2a21ecd587bb3918d07ea8189ff1e41609cf3 Mon Sep 17 00:00:00 2001 From: Yuriy Lazarev Date: Sun, 24 Sep 2017 17:30:42 +0200 Subject: [PATCH] toBase :: Int -> BigInt -> String --- src/Data/BigInt.js | 7 ++++--- src/Data/BigInt.purs | 7 ++++++- test/Main.purs | 18 +++++++++++------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Data/BigInt.js b/src/Data/BigInt.js index 4eaa776..36567ed 100644 --- a/src/Data/BigInt.js +++ b/src/Data/BigInt.js @@ -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) { @@ -124,4 +126,3 @@ exports.shr = function(x) { return x.shiftRight(n); }; }; - diff --git a/src/Data/BigInt.purs b/src/Data/BigInt.purs index cf23fc6..7d3aff7 100644 --- a/src/Data/BigInt.purs +++ b/src/Data/BigInt.purs @@ -5,6 +5,7 @@ module Data.BigInt , fromBase , fromInt , toString + , toBase , abs , even , odd @@ -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 <> "\"" diff --git a/test/Main.purs b/test/Main.purs index 486d738..2fdd838 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -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) @@ -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 @@ -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)) @@ -124,12 +129,11 @@ main = do log "Shifting" assert $ shl two one == four assert $ shr two one == one - + let prxBigInt = Proxy ∷ Proxy 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 -