Skip to content

Commit d9eebc1

Browse files
committed
Add fromNumber
1 parent 4118222 commit d9eebc1

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

Diff for: docs/Data/BigInt.md

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ fromInt :: Int -> BigInt
2929

3030
Convert an integer to a BigInt.
3131

32+
#### `fromNumber`
33+
34+
``` purescript
35+
fromNumber :: Number -> BigInt
36+
```
37+
38+
Convert a Number to a BigInt. The fractional part is truncated.
39+
3240
#### `toNumber`
3341

3442
``` purescript

Diff for: src/Data/BigInt.js

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ exports["fromBase'"] = function(just) {
1919

2020
exports.fromInt = bigInt;
2121

22+
function truncate(n) {
23+
if (n > 0) return Math.floor(n);
24+
return Math.ceil(n);
25+
}
26+
27+
exports.fromNumber = function(x) {
28+
return bigInt(truncate(x));
29+
};
30+
2231
exports.toBase = function(base) {
2332
return function (x) {
2433
return x.toString(base);

Diff for: src/Data/BigInt.purs

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Data.BigInt
44
, fromString
55
, fromBase
66
, fromInt
7+
, fromNumber
78
, toString
89
, toBase
910
, abs
@@ -38,6 +39,9 @@ foreign import fromBase' :: forall a. (a -> Maybe a)
3839
-- | Convert an integer to a BigInt.
3940
foreign import fromInt :: Int -> BigInt
4041

42+
-- | Convert a Number to a BigInt. The fractional part is truncated.
43+
foreign import fromNumber :: Number -> BigInt
44+
4145
-- | Converts a BigInt to a Number. Loses precision for numbers which are too
4246
-- | large.
4347
foreign import toNumber :: BigInt -> Number

Diff for: test/Main.purs

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import Control.Monad.Eff.Console (CONSOLE, log)
55
import Control.Monad.Eff.Exception (EXCEPTION)
66
import Control.Monad.Eff.Random (RANDOM)
77
import Data.Array (filter, range)
8-
import Data.BigInt (BigInt, abs, fromInt, prime, pow, odd, even, fromString, toNumber, fromBase, toBase, toString, not, or, xor, and, shl, shr)
8+
import Data.BigInt (BigInt, abs, fromInt, fromNumber, prime, pow, odd, even, fromString, toNumber,
9+
fromBase, toBase, toString, not, or, xor, and, shl, shr)
910
import Data.Foldable (fold)
1011
import Data.Int as Int
1112
import Data.Maybe (Maybe(..), fromMaybe)
@@ -85,6 +86,14 @@ main = do
8586
assert $ (toBase 16 <$> fromString "255") == Just "ff"
8687
assert $ toString (fromInt 12345) == "12345"
8788

89+
log "Converting from Number to BigInt"
90+
assert $ fromNumber 0.0 == zero
91+
assert $ fromNumber 3.4 == three
92+
assert $ fromNumber (-3.9) == -three
93+
assert $ fromNumber 1.0e7 == fromInt 10000000
94+
assert $ Just (fromNumber 1.0e47) == fromString "1e47"
95+
quickCheck (\x -> fromInt x == fromNumber (Int.toNumber x))
96+
8897
log "Conversions between String, Int and BigInt should not loose precision"
8998
quickCheck (\n -> fromString (show n) == Just (fromInt n))
9099
quickCheck (\n -> Int.toNumber n == toNumber (fromInt n))

0 commit comments

Comments
 (0)