CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-big-js

A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Big.js

Big.js is a small, fast, easy-to-use JavaScript library for arbitrary-precision decimal arithmetic. It provides immutable Big number objects that solve JavaScript's floating-point precision problems (like 0.1 + 0.2 !== 0.3) by storing values in decimal floating-point format and performing all operations with exact precision.

Package Information

  • Package Name: big.js
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install big.js

Core Imports

const Big = require('big.js');

ES Module:

import Big from 'big.js';

Browser (global):

<script src="big.js"></script>
<!-- Big is available as global -->

Basic Usage

import Big from 'big.js';

// Create Big numbers from strings, numbers, or other Big instances
const x = new Big('123.456');
const y = Big('78.9');           // 'new' is optional
const z = new Big(x);            // Copy constructor

// Perform arithmetic operations (all return new Big instances)
const sum = x.plus(y);           // "202.356"
const diff = x.minus(y);         // "44.556" 
const product = x.times(y);      // "9740.4984"
const quotient = x.div(y);       // "1.56388..."

// Chain operations
const result = x.plus(y).times(2).minus(100);

// Compare values
x.gt(y);                         // true
x.eq(new Big('123.456'));        // true

// Format output
x.toFixed(2);                    // "123.46"
x.toPrecision(5);                // "123.46"

Architecture

Big.js is built around several key design principles:

  • Immutable Operations: All arithmetic operations return new Big instances, ensuring thread safety and predictable behavior
  • Decimal Floating-Point Storage: Values are stored as coefficient arrays with exponent and sign, avoiding binary floating-point precision issues
  • Global Configuration: Settings like decimal places and rounding modes are shared across all instances via static properties
  • Lightweight Design: Single-file library with no dependencies, optimized for size and performance
  • Chainable API: Methods return Big instances enabling fluent operation chaining
  • Multiple Constructor Support: Ability to create independent Big constructors with different global settings

Configuration

Big.js provides global configuration options that affect all instances:

// Set decimal places for division operations (default: 20)
Big.DP = 10;

// Set rounding mode (default: 1 - half up)
Big.RM = Big.roundHalfEven;      // or 0, 1, 2, 3

// Configure exponential notation thresholds
Big.NE = -7;                     // Negative exponent threshold
Big.PE = 21;                     // Positive exponent threshold

// Enable strict mode (disallow primitive numbers)
Big.strict = true;

Capabilities

Constructor

Creates a new Big number from various input types.

/**
 * Create a new Big number
 * @param {number|string|Big} n - Numeric value to convert
 * @returns {Big} New Big instance
 */
function Big(n) {}

// Can be called with or without 'new'
const x = new Big('123.45');
const y = Big('67.89');

Arithmetic Operations

Core mathematical operations that return new Big instances.

/**
 * Return absolute value
 * @returns {Big} New Big with absolute value
 */
abs() {}

/**
 * Addition
 * @param {number|string|Big} y - Value to add
 * @returns {Big} New Big with sum
 */
plus(y) {}
add(y) {}  // Alias for plus

/**
 * Subtraction
 * @param {number|string|Big} y - Value to subtract
 * @returns {Big} New Big with difference
 */
minus(y) {}
sub(y) {}  // Alias for minus

/**
 * Multiplication
 * @param {number|string|Big} y - Value to multiply by
 * @returns {Big} New Big with product
 */
times(y) {}
mul(y) {}  // Alias for times

/**
 * Division
 * @param {number|string|Big} y - Value to divide by
 * @returns {Big} New Big with quotient
 * @throws {Error} If divisor is zero
 */
div(y) {}

/**
 * Modulo operation
 * @param {number|string|Big} y - Divisor for modulo
 * @returns {Big} New Big with remainder
 * @throws {Error} If divisor is zero
 */
mod(y) {}

/**
 * Exponentiation
 * @param {number} n - Integer exponent (-1000000 to 1000000)
 * @returns {Big} New Big raised to power n
 * @throws {Error} If exponent is invalid
 */
pow(n) {}

/**
 * Square root
 * @returns {Big} New Big with square root
 * @throws {Error} If value is negative
 */
sqrt() {}

/**
 * Negation
 * @returns {Big} New Big with opposite sign
 */
neg() {}

Comparison Methods

Methods for comparing Big numbers, returning boolean values.

/**
 * Compare two Big numbers
 * @param {number|string|Big} y - Value to compare with
 * @returns {number} -1 if less, 0 if equal, 1 if greater
 */
cmp(y) {}

/**
 * Equal to
 * @param {number|string|Big} y - Value to compare with
 * @returns {boolean} True if equal
 */
eq(y) {}

/**
 * Greater than
 * @param {number|string|Big} y - Value to compare with
 * @returns {boolean} True if greater
 */
gt(y) {}

/**
 * Greater than or equal to
 * @param {number|string|Big} y - Value to compare with
 * @returns {boolean} True if greater than or equal
 */
gte(y) {}

/**
 * Less than
 * @param {number|string|Big} y - Value to compare with
 * @returns {boolean} True if less
 */
lt(y) {}

/**
 * Less than or equal to
 * @param {number|string|Big} y - Value to compare with
 * @returns {boolean} True if less than or equal
 */
lte(y) {}

Rounding Methods

Methods for precision control, returning new Big instances.

/**
 * Round to specified decimal places
 * @param {number} [dp=0] - Decimal places (integer, -1000000 to 1000000)
 * @param {number} [rm] - Rounding mode (0-3, defaults to Big.RM)
 * @returns {Big} New rounded Big
 * @throws {Error} If decimal places invalid
 */
round(dp, rm) {}

/**
 * Round to specified significant digits
 * @param {number} sd - Significant digits (integer, 1 to 1000000)
 * @param {number} [rm] - Rounding mode (0-3, defaults to Big.RM)
 * @returns {Big} New Big with specified precision
 * @throws {Error} If precision invalid
 */
prec(sd, rm) {}

Formatting Methods

Methods for converting Big numbers to various string representations.

/**
 * String representation (normal or exponential notation)
 * @returns {string} String representation of the number
 */
toString() {}

/**
 * Fixed-point notation
 * @param {number} [dp] - Decimal places (0 to 1000000)
 * @param {number} [rm] - Rounding mode (0-3, defaults to Big.RM)
 * @returns {string} Fixed-point string representation
 * @throws {Error} If decimal places invalid
 */
toFixed(dp, rm) {}

/**
 * Exponential notation
 * @param {number} [dp] - Decimal places (0 to 1000000)
 * @param {number} [rm] - Rounding mode (0-3, defaults to Big.RM)
 * @returns {string} Exponential string representation
 * @throws {Error} If decimal places invalid
 */
toExponential(dp, rm) {}

/**
 * Precision notation
 * @param {number} [sd] - Significant digits (1 to 1000000)
 * @param {number} [rm] - Rounding mode (0-3, defaults to Big.RM)
 * @returns {string} Precision string representation
 * @throws {Error} If precision invalid
 */
toPrecision(sd, rm) {}

/**
 * Convert to primitive number
 * @returns {number} JavaScript number (may lose precision)
 * @throws {Error} In strict mode if conversion imprecise
 */
toNumber() {}

/**
 * Primitive value for JSON serialization
 * @returns {string} String representation
 * @throws {Error} In strict mode if valueOf called
 */
valueOf() {}

/**
 * JSON serialization
 * @returns {string} String representation for JSON
 */
toJSON() {}

Configuration Properties

Static properties for global configuration.

/**
 * Maximum decimal places for division operations
 * @type {number} Integer 0 to 1000000 (default: 20)
 */
Big.DP = 20;

/**
 * Rounding mode for operations
 * @type {number} 0-3 (default: 1)
 */
Big.RM = 1;

/**
 * Negative exponent threshold for exponential notation
 * @type {number} 0 to -1000000 (default: -7)
 */
Big.NE = -7;

/**
 * Positive exponent threshold for exponential notation
 * @type {number} 0 to 1000000 (default: 21)
 */
Big.PE = 21;

/**
 * Strict mode flag
 * @type {boolean} Default: false
 */
Big.strict = false;

// Rounding mode constants
Big.roundDown = 0;      // Towards zero
Big.roundHalfUp = 1;    // To nearest, ties away from zero
Big.roundHalfEven = 2;  // To nearest, ties to even
Big.roundUp = 3;        // Away from zero

/**
 * Export properties for compatibility
 * @type {function} Reference to Big constructor
 */
Big.default = Big;
Big.Big = Big;

Instance Properties

Internal representation properties (read-only).

/**
 * Coefficient array containing the digits
 * @type {number[]} Array of single digits
 */
c: number[]

/**
 * Exponent
 * @type {number} Integer exponent
 */
e: number

/**
 * Sign
 * @type {number} 1 for positive, -1 for negative
 */
s: number

/**
 * Constructor reference
 * @type {function} Reference to Big constructor
 */
constructor: function

Error Handling

Big.js throws Error objects with descriptive messages prefixed with [big.js] for various invalid operations:

// Invalid input
new Big('invalid');           // Error: [big.js] Invalid number

// Division by zero
Big('1').div('0');            // Error: [big.js] Division by zero

// Invalid decimal places
Big('1').toFixed(-1);         // Error: [big.js] Invalid decimal places

// Strict mode violations
Big.strict = true;
new Big(1.23);                // TypeError: [big.js] Invalid value
Big('1.1').valueOf();         // Error: [big.js] valueOf disallowed

Advanced Usage

Multiple Constructors

Create independent Big constructors with different configurations:

// Default constructor
const Big1 = Big();
Big1.DP = 10;
Big1.RM = Big1.roundDown;

// Another constructor with different settings
const Big2 = Big();
Big2.DP = 20;
Big2.RM = Big2.roundHalfEven;

const x = new Big1('1.23456789');
const y = new Big2('1.23456789');

Internal Representation Access

Access internal coefficient, exponent, and sign for advanced operations:

const x = new Big('-123.456');
console.log(x.c);  // [1, 2, 3, 4, 5, 6] - coefficient array  
console.log(x.e);  // 2 - exponent
console.log(x.s);  // -1 - sign
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/big.js@7.0.x
Publish Source
CLI
Badge
tessl/npm-big-js badge