AssemblyScript library for arbitrary-precision decimal arithmetic
MIT License
AssemblyScript library for arbitrary-precision decimal arithmetic 🚀
npm install as-big
import Big from "as-big";
let r = Big.of(0.1) + Big.of(0.2); // Big(0.3)
let x = Big.of(42);
let y = Big.of("13");
let a = x + y; // Big(55)
a = x.plus(13); // Big(55)
let a0 = a.prec(1); // Big(60)
let aNum = a.toNumber() + 1; // 56
let aStr = a.toString(); // "55"
let c = a0 + Big.TEN / Big.TWO; // Big(65)
Big.of(n)
returns a Big
instance, where
n
is either another Big
instance, string, or numberBig.copyOf(x)
creates a copy from a Big
instance, where
x
is the Big
instance to copyx + y
or x.plus(y)
x - y
or x.minus(y)
x * y
or y.times(y)
x / y
or x.div(y)
x % y
or x.mod(y)
x ^ n
or x.pow(n)
, where n
is i32
x.sqrt()
x.cmp(y)
returns
1
if the value of x
is greater than the value of y
,-1
if the value of x
is less than the value of y
, or0
if they have the same value.x == y
or x.eq(y)
x != y
or x.neq(y)
x < y
or x.gt(y)
x <= y
or x.gte(y)
x > y
or x.lt(y)
x >= y
or x.lte(y)
x.abs()
-x
or x.neg()
+x
or x.pos()
x.round(dp, rm)
where
dp
is the maximum of decimal places, andrm
is the rounding mode (0
, 1
, 2
, 3
)
0
(down), 1
(half-up), 2
(half-even) or 3
(up)x.prec(sd, rm)
where
sd
is the maximum of significant digits, andrm
is the rounding mode (0
, 1
, 2
, 3
)
0
(down), 1
(half-up), 2
(half-even) or 3
(up)toString
(string representation): let s: string = x.toString()
toNumber
(f64
represenation): let n: f64 = x.toNumber()
toExponential
(string represenation): let s: string = x.toExponential()
Big.ZERO
: a Big
instance with the value zero 0
Big.ONE
: a Big
instance with the value one 1
Big.TWO
: a Big
instance with the value two 2
Big.TEN
: a Big
instance with the value ten 10
Big.HALF
: a Big
instance with the value one half 0.5
Big.DP
: the maximum number of decimal places of the results of operations involving division (default: 20
)
Big.RM
: the rounding mode used when rounding to the above decimal places (default: 1
)
Big.PE
: the positive exponent at and above which toString
returns exponential notation (default: 21
)
Big.NE
: the negative exponent at and beneath which toString
returns exponential notation (default: -7
)
There is a collection of examples in the examples
directory.
The assembly
directory contains AS source code.
npm i
npm run asbuild
The tests
directory contains all unit tests.
Run all the tests:
npm test
Test a single method:
node tests/<method>