@bignum/core
Arbitrary-precision decimal arithmetic with BigInt.
💿 Installation
npm install @bignum/core
📖 Usage
import { BigNum } from "@bignum/core";
console.log(BigNum.valueOf(0.2).add(BigNum.valueOf(0.1)).toString());
console.log(0.2 + 0.1);
🧮 API
new BigNum(value)
Translates a value into a BigNum.
Numbers with decimals are first converted to strings and processed.
Note that numbers that have already lost precision are not handled well.
BigNum.valueOf(value): BigNum
Translates a value into a BigNum.
Same as constructor, but if given a BigNum instance, that instance is returned.
BigNum.prototype.add(augend): BigNum
Returns a BigNum whose value is (this + augend).
BigNum.prototype.subtract(subtrahend): BigNum
Returns a BigNum whose value is (this - subtrahend).
BigNum.prototype.multiply(multiplicand): BigNum
Returns a BigNum whose value is (this * multiplicand).
BigNum.prototype.divide(divisor, [options]): BigNum
Returns a BigNum whose value is (this / divisor).
An object can be given as an option.
Options:
Name | Type | Description |
---|
overflow | (context)=>boolean | You can specify an overflow test function. By default, if the number of decimal places exceeds 20, it is considered an overflow. |
maxDp | bigint | Deprecated. The maximum number of decimal places. |
maxDecimalPrecision | bigint | Deprecated. The maximum number of precision when having decimals. |
BigNum.prototype.modulo(divisor): BigNum
Returns a BigNum whose value is (this % divisor).
BigNum.prototype.negate(): BigNum
Returns a BigNum whose value is (-this).
BigNum.prototype.pow(n, [options]): BigNum
Returns a BigNum whose value is (this ** n).
If n
is given a non-integer value, an error will be raised.
An object can be given as an option. It's the same option for divide()
. This is used in negative pows.
Options:
Name | Type | Description |
---|
overflow | (context)=>boolean | You can specify an overflow test function. By default, if there are decimals and the number of precisions exceeds 20, it is considered an overflow. |
maxDp | bigint | Deprecated. The maximum number of decimal places. |
maxDecimalPrecision | bigint | Deprecated. The maximum number of precision when having decimals. |
BigNum.prototype.scaleByPowerOfTen(n): BigNum
Returns a BigNum whose value is (this * 10 ** n).
If n
is given a non-integer value, an error will be raised.
BigNum.prototype.sqrt([options]): BigNum
Returns an approximation to the square root of this.
If this BigNum is a negative value, an error will be raised.
An object can be given as an option. It's the same option for divide()
.
BigNum.prototype.abs(): BigNum
Returns a BigNum whose value is the absolute value of this BigNum.
BigNum.prototype.trunc(): BigNum
Returns a BigNum that is the integral part of this BigNum, with removing any fractional digits.
BigNum.prototype.round(): BigNum
Returns this BigNum rounded to the nearest integer.
BigNum.prototype.floor(): BigNum
Returns the greatest integer less than or equal to this BigNum.
BigNum.prototype.ceil(): BigNum
Returns the smallest integer greater than or equal to this BigNum.
BigNum.prototype.signum(): 0 | 1 | -1 | NaN
Returns a number indicating the sign.
BigNum.prototype.compareTo(value): 0 | -1 | 1 | NaN
Compares this BigNum with the specified BigNum.
BigNum.prototype.isNaN(): boolean
Returns true
if this is NaN.
BigNum.prototype.isFinite(): boolean
Returns true
if this is finite number.
🛸 Prior Art