Skip to content
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

toBase :: Int -> BigInt -> String #5

Merged
merged 1 commit into from
Sep 24, 2017
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
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 = 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