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 aBig
instance, wheren
is either anotherBig
instance, string, or number
Big.copyOf(x)
creates a copy from aBig
instance, wherex
is theBig
instance to copy
- plus (addition):
x + y
orx.plus(y)
- minus (substraction):
x - y
orx.minus(y)
- times (multiplication):
x * y
ory.times(y)
- div (division):
x / y
orx.div(y)
- mod (modulo):
x % y
orx.mod(y)
- pow (power):
x ^ n
orx.pow(n)
, wheren
isi32
- sqrt (square root):
x.sqrt()
- cmp (compare):
x.cmp(y)
returns1
if the value ofx
is greater than the value ofy
,-1
if the value ofx
is less than the value ofy
, or0
if they have the same value.
- eq (equals):
x == y
orx.eq(y)
- neq (not equals):
x != y
orx.neq(y)
- gt (greater than):
x < y
orx.gt(y)
- gte (greater than or equals):
x <= y
orx.gte(y)
- lt (less than):
x > y
orx.lt(y)
- lte (less than or equals):
x >= y
orx.lte(y)
- abs (absolute value):
x.abs()
- neg (negative value):
-x
orx.neg()
- pos (this value):
+x
orx.pos()
- round (rounded value):
x.round(dp, rm)
wheredp
is the maximum of decimal places, andrm
is the rounding mode (0
,1
,2
,3
)0
(down),1
(half-up),2
(half-even) or3
(up)
- prec (value rounded to precision):
x.prec(sd, rm)
wheresd
is the maximum of significant digits, andrm
is the rounding mode (0
,1
,2
,3
)0
(down),1
(half-up),2
(half-even) or3
(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
: aBig
instance with the value zero0
Big.ONE
: aBig
instance with the value one1
Big.TWO
: aBig
instance with the value two2
Big.TEN
: aBig
instance with the value ten10
Big.HALF
: aBig
instance with the value one half0.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 whichtoString
returns exponential notation (default:21
) -
Big.NE
: the negative exponent at and beneath whichtoString
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>