You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

bigdecimal.js

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bigdecimal.js - npm Package Compare versions

Comparing version
0.0.12
to
1.0.0
+53
-12
lib/bigdecimal.d.ts

@@ -381,4 +381,4 @@ /**

* arithmetic defined by IEEE 754 and by this class, there are notable
* differences as well. The fundamental similarity shared by {@code
* BigDecimal} and IEEE 754 decimal arithmetic is the conceptual
* differences as well. The fundamental similarity shared by
* `BigDecimal` and IEEE 754 decimal arithmetic is the conceptual
* operation of computing the mathematical infinitely precise real

@@ -436,4 +436,2 @@ * number value of an operation and then mapping that real number to a

export declare class BigDecimal {
private static readonly MAX_INT_VALUE;
private static readonly MIN_INT_VALUE;
/**

@@ -446,2 +444,3 @@ * Returns the signum function of this `BigDecimal`.

signum(): number;
precision(): number;
/**

@@ -489,2 +488,19 @@ * Returns a `BigDecimal` whose value is `(-this)`,

/**
* Returns a `BigDecimal` whose value is `(this / divisor)`,
* and whose scale is as specified. If rounding must
* be performed to generate a result with the specified scale, the
* specified rounding mode is applied.
*
* @param divisor value by which this `BigDecimal` is to be divided.
* @param scale scale of the `BigDecimal` quotient to be returned.
* @param roundingMode rounding mode to apply.
* @return `this / divisor`
* @throws RangeError
* * If `divisor` is zero
* * If `roundingMode==RoundingMode.UNNECESSARY` and the specified scale is insufficient to represent the result
* of the division exactly.
* * If scale is given but rounding mode is not given.
*/
divide(divisor: BigDecimal, scale?: number, roundingMode?: RoundingMode): BigDecimal;
/**
* Returns a `BigDecimal` whose value is `(this /

@@ -499,3 +515,3 @@ * divisor)`, with rounding according to the context settings.

*/
divide(divisor: BigDecimal, mc?: MathContext): BigDecimal;
divideWithMathContext(divisor: BigDecimal, mc?: MathContext): BigDecimal;
/**

@@ -599,3 +615,3 @@ * Returns a `BigDecimal` whose value is the integer part

* precision in question. If the rounding mode is {@link
* RoundingMode.HALF_UP}, {@link RoundingMode.HALF_DOWN},
* RoundingMode.HALF_UP}, {@link RoundingMode.HALF_DOWN},
* or {@link RoundingMode.HALF_EVEN}, the

@@ -658,2 +674,4 @@ * result is within one half an ulp of the exact decimal value.

*
* The scale will be kept in the integer range, if cannot error will be thrown.
*
* @return the scale of this `BigDecimal`.

@@ -800,2 +818,9 @@ */

/**
* Compares half of second with first
* @param first
* @param second
* @private
*/
private static compareHalf;
/**
* Returns a `BigDecimal` which is equivalent to this one

@@ -1021,2 +1046,15 @@ * with the decimal point moved `n` places to the left. If

*
* The values passed must match one of Java BigDecimal's constructors, so the valid usages of this function is listed below:
* ```javascript
* Big(123n); // bigint, 123
* Big(123n, 3); // bigint and scale, 0.123
* Big(123n, 3, MC(2, RoundingMode.HALF_UP)); // bigint, scale and mc, 0.12
* Big(123n, undefined, MC(2, RoundingMode.HALF_UP)); // bigint and mc, 1.2E+2
* Big('1.13e12'); // string, 1.13E+12
* Big('1.11e11', undefined, MC(2, RoundingMode.HALF_UP)); // string and mc, 1.1E+11
* Big(10000); // number, 10000
* Big(123, 5); // integer and scale, 0.00123
* Big(1.1233, undefined, MC(2, RoundingMode.HALF_UP)); // number and scale, 1.1
* ```
*
* Sample Usage:

@@ -1050,11 +1088,14 @@ *```javascript

* * If value is a number:
* * Value is not in the range [Number.MIN_VALUE, Number.MAX_VALUE]
* * Both scale and precision is provided. You can only give one of scale and mc. Passing undefined is same as omitting.
* * If value is not a number, a BigInt or a BigDecimal, it will be converted to string.
* * Value is not in the range `[-Number.MAX_VALUE, Number.MAX_VALUE]`
* * Both a scale and a math context is provided. You can only give one of scale and math context.
* Passing `undefined` is same as omitting an argument.
* * If value is a double and scale is given.
* * If value is not a `number`, a `BigInt` or a `BigDecimal`, it will be converted to string.
* An error will be thrown if the string format is invalid.
* * If value is not a `BigInt` or `number`, and scale is given.
*/
export declare const Big: BigInterface;
interface MCInterface {
(precision: number, roundingMode: RoundingMode): MathContext;
new (precision: number, roundingMode: RoundingMode): MathContext;
(precision: number, roundingMode?: RoundingMode): MathContext;
new (precision: number, roundingMode?: RoundingMode): MathContext;
}

@@ -1086,5 +1127,5 @@ /**

* @param precision Precision value
* @param roundingMode Rounding Mode
* @param roundingMode Optional rounding Mode. By default RoundingMode.HALF_UP.
*/
export declare const MC: MCInterface;
export {};
+2
-2
{
"name": "bigdecimal.js",
"version": "0.0.12",
"version": "1.0.0",
"description": "A BigDecimal implementation with native BigInts",
"main": "lib/index.js",
"scripts": {
"test": "mocha -t 10000 test//**/*.js",
"test": "mocha -t 100000 test/**/**/*.js",
"compile": "tsc",

@@ -9,0 +9,0 @@ "clean": "rimraf docs lib",

+15
-11

@@ -6,13 +6,13 @@ # BigDecimal.js

[BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) based BigDecimal(arbitrary precision floats) implementation for Node.js.
This implementation is based on java BigDecimal class. Like java BigDecimal class, it uses big integers internally. This implementation
is faster than popular big decimal libraries. See [benchmarks results part below](https://github.com/srknzl/bigdecimal.js#benchmark-results) for comparison.
[BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) based BigDecimal implementation for Node.js 10.4 and above.
This implementation is inspired from java BigDecimal class. This implementation is faster than popular big decimal libraries for most operations.
See [benchmarks results part below](https://github.com/srknzl/bigdecimal.js#benchmark-results) for comparison of each operation.
**Note: The library is currently in preview, v1.0.0 will be released soon. API can change in the near future. I appreciate your feedback, thanks.**
## Advantages of this library
## Features
* Faster than other BigDecimal libraries because of native BigInt(for now, benchmarked against `big.js` and `bigdecimal` )
* Faster than other BigDecimal libraries because of native BigInt
* Simple API that is almost same with Java's [BigDecimal](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/math/BigDecimal.html)
* No dependencies
* Well tested
* Includes type definition file

@@ -85,3 +85,3 @@ ## Installation

* bigdecimal.js
* This library
* [big.js](https://github.com/MikeMcl/big.js)

@@ -94,2 +94,4 @@ * [GWT based BigDecimal](https://github.com/iriscouch/bigdecimal.js)

For now, benchmarked against [big.js](https://www.npmjs.com/package/big.js) and [bigdecimal](https://www.npmjs.com/package/bigdecimal).
* Test Machine:

@@ -100,4 +102,6 @@ * AMD Ryzen 5 3600

* Update Date: 01 August 2021
* Each operation is run with fixed numbers that is a mix of small and big decimal numbers.
* Micro benchmark framework: benchmark.js. Check out [benchmarks folder](https://github.com/srknzl/bigdecimal.js/tree/main/benchmarks) for source code of benchmarks.
* Each operation is run with fixed set of decimal numbers composed of both simple and complex numbers.
* Micro benchmark framework used is [benchmark](https://www.npmjs.com/package/benchmark). Check out [benchmarks folder](https://github.com/srknzl/bigdecimal.js/tree/main/benchmarks) for source code of benchmarks.
* For now, benchmarked the following operations, all operations will be added soon.
* Operations per second(op/s):

@@ -108,3 +112,3 @@

| Add | 47,477 | 12,340 | 55.58 | BigDecimal.js |
| Multiply | 362,610 | 362,610 | 1,404 | BigDecimal.js |
| Multiply | 336,782 | 31,950 | 1,451 | BigDecimal.js |
| Subtract | 43,147 | 11,678 | 54.10 | BigDecimal.js |

@@ -111,0 +115,0 @@ | Divide | 9,536 | 1,053 | 341 | BigDecimal.js |

Sorry, the diff of this file is too big to display