AssemblyScript library for arbitrary-precision decimal arithmetic 🚀
npm install as-bigimport 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 aBiginstance, wherenis either anotherBiginstance, string, or number
Big.copyOf(x)creates a copy from aBiginstance, wherexis theBiginstance to copy
- plus (addition):
x + yorx.plus(y) - minus (substraction):
x - yorx.minus(y) - times (multiplication):
x * yory.times(y) - div (division):
x / yorx.div(y) - mod (modulo):
x % yorx.mod(y) - pow (power):
x ^ norx.pow(n), wherenisi32 - sqrt (square root):
x.sqrt()
- cmp (compare):
x.cmp(y)returns1if the value ofxis greater than the value ofy,-1if the value ofxis less than the value ofy, or0if they have the same value.
- eq (equals):
x == yorx.eq(y) - neq (not equals):
x != yorx.neq(y) - gt (greater than):
x < yorx.gt(y) - gte (greater than or equals):
x <= yorx.gte(y) - lt (less than):
x > yorx.lt(y) - lte (less than or equals):
x >= yorx.lte(y)
- abs (absolute value):
x.abs() - neg (negative value):
-xorx.neg() - pos (this value):
+xorx.pos() - round (rounded value):
x.round(dp, rm)wheredpis the maximum of decimal places, andrmis 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)wheresdis the maximum of significant digits, andrmis 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(f64represenation):let n: f64 = x.toNumber()toExponential(string represenation):let s: string = x.toExponential()
Big.ZERO: aBiginstance with the value zero0Big.ONE: aBiginstance with the value one1Big.TWO: aBiginstance with the value two2Big.TEN: aBiginstance with the value ten10Big.HALF: aBiginstance 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 whichtoStringreturns exponential notation (default:21) -
Big.NE: the negative exponent at and beneath whichtoStringreturns 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 asbuildThe tests directory contains all unit tests.
Run all the tests:
npm testTest a single method:
node tests/<method>