CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-bignumber-js

A JavaScript library for arbitrary-precision decimal and non-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

arithmetic.mddocs/

Arithmetic Operations

Complete set of mathematical operations for addition, subtraction, multiplication, division, exponentiation, modulo, and more with precise decimal arithmetic.

Capabilities

Addition

Adds two BigNumbers together with exact precision.

/**
 * Returns the sum of this BigNumber and n
 * @param n - Value to add
 * @param base - Base of n (2-36, optional)
 * @returns New BigNumber with sum
 */
plus(n: BigNumber.Value, base?: number): BigNumber;

Usage Examples:

import BigNumber from "bignumber.js";

const x = new BigNumber('0.1');
const y = new BigNumber('0.2');
const sum = x.plus(y);           // "0.3" (exact, no floating-point error)

// With different input types
const a = new BigNumber('123.456');
a.plus(789);                     // "912.456"
a.plus('0.001');                 // "123.457"
a.plus('ff', 16);               // "378.456" (255 in decimal)

Subtraction

Subtracts one BigNumber from another with exact precision.

/**
 * Returns the difference of this BigNumber and n
 * @param n - Value to subtract
 * @param base - Base of n (2-36, optional)
 * @returns New BigNumber with difference
 */
minus(n: BigNumber.Value, base?: number): BigNumber;

Usage Examples:

const x = new BigNumber('0.3');
const y = new BigNumber('0.1');
const diff = x.minus(y);         // "0.2" (exact)

// Floating-point comparison
console.log(0.3 - 0.1);          // 0.19999999999999998
console.log(diff.toString());     // "0.2"

Multiplication

Multiplies two BigNumbers with exact precision.

/**
 * Returns the product of this BigNumber and n
 * @param n - Value to multiply
 * @param base - Base of n (2-36, optional)  
 * @returns New BigNumber with product
 */
multipliedBy(n: BigNumber.Value, base?: number): BigNumber;

// Alias
times(n: BigNumber.Value, base?: number): BigNumber;

Usage Examples:

const x = new BigNumber('0.6');
const y = x.multipliedBy(3);     // "1.8" (exact)
const z = x.times(3);            // "1.8" (same result, alias)

// Large number multiplication
const big1 = new BigNumber('7e+500');
const big2 = new BigNumber('1.8');
big1.times(big2);               // "1.26e+501"

// With base conversion
x.times('-a', 16);              // "-6" (-10 in decimal)

Division

Divides one BigNumber by another, rounded according to current settings.

/**
 * Returns the quotient of this BigNumber divided by n
 * @param n - Divisor value
 * @param base - Base of n (2-36, optional)
 * @returns New BigNumber with quotient
 */
dividedBy(n: BigNumber.Value, base?: number): BigNumber;

// Alias
div(n: BigNumber.Value, base?: number): BigNumber;

Usage Examples:

const x = new BigNumber(355);
const y = new BigNumber(113);
const quotient = x.dividedBy(y); // "3.14159292035398230088"

// Division with different bases
x.div(47, 16);                  // "5" (71 ÷ 71 = 1, but 355 ÷ 71 = 5)

Integer Division

Returns the integer part of division without remainder.

/**
 * Returns the integer part of dividing this BigNumber by n
 * @param n - Divisor value
 * @param base - Base of n (2-36, optional)
 * @returns New BigNumber with integer quotient
 */
dividedToIntegerBy(n: BigNumber.Value, base?: number): BigNumber;

// Alias
idiv(n: BigNumber.Value, base?: number): BigNumber;

Usage Examples:

const x = new BigNumber(5);
const y = new BigNumber(3);
x.dividedToIntegerBy(y);        // "1"
x.idiv(0.7);                    // "7"

Modulo

Returns the remainder of division with configurable modulo mode.

/**
 * Returns the remainder of dividing this BigNumber by n
 * @param n - Divisor value
 * @param base - Base of n (2-36, optional)
 * @returns New BigNumber with remainder
 */
modulo(n: BigNumber.Value, base?: number): BigNumber;

// Alias
mod(n: BigNumber.Value, base?: number): BigNumber;

Usage Examples:

const x = new BigNumber(1);
x.modulo(0.9);                  // "0.1" (exact)
x.mod(0.9);                     // "0.1" (alias)

// JavaScript comparison
console.log(1 % 0.9);           // 0.09999999999999998

// Different modulo modes (configured globally)
const y = new BigNumber(33);
y.mod('a', 33);                 // "3" (10 in base 33)

Exponentiation

Raises a BigNumber to a power, optionally with modular exponentiation.

/**
 * Returns this BigNumber raised to the power of n, optionally modulo m
 * @param n - Exponent (must be integer)
 * @param m - Modulus (optional, for modular exponentiation)
 * @returns New BigNumber with result
 */
exponentiatedBy(n: BigNumber.Value, m?: BigNumber.Value): BigNumber;
exponentiatedBy(n: number, m?: BigNumber.Value): BigNumber;

// Alias
pow(n: BigNumber.Value, m?: BigNumber.Value): BigNumber;
pow(n: number, m?: BigNumber.Value): BigNumber;

Usage Examples:

const x = new BigNumber(0.7);
x.exponentiatedBy(2);           // "0.49"
x.pow(2);                       // "0.49" (alias)

// JavaScript comparison
Math.pow(0.7, 2);               // 0.48999999999999994

// Negative exponents
BigNumber(3).pow(-2);           // "0.11111111111111111111"

// Modular exponentiation
const base = new BigNumber(5);
const exp = new BigNumber(3);
const mod = new BigNumber(13);
base.pow(exp, mod);             // Efficient modular exponentiation

Square Root

Returns the square root with correctly rounded result.

/**
 * Returns the square root of this BigNumber
 * @returns New BigNumber with square root
 */
squareRoot(): BigNumber;

// Alias
sqrt(): BigNumber;

Usage Examples:

const x = new BigNumber(16);
x.squareRoot();                 // "4"
x.sqrt();                       // "4" (alias)

const y = new BigNumber(3);
y.sqrt();                       // "1.73205080756887729353"

Absolute Value

Returns the absolute value (magnitude) of the BigNumber.

/**
 * Returns the absolute value of this BigNumber
 * @returns New BigNumber with absolute value
 */
absoluteValue(): BigNumber;

// Alias
abs(): BigNumber;

Usage Examples:

const x = new BigNumber(-0.8);
x.absoluteValue();              // "0.8"
x.abs();                        // "0.8" (alias)

const y = new BigNumber(42);
y.abs();                        // "42" (already positive)

Negation

Returns the negated value (multiply by -1).

/**
 * Returns this BigNumber multiplied by -1
 * @returns New BigNumber with negated value
 */
negated(): BigNumber;

Usage Examples:

const x = new BigNumber(1.8);
x.negated();                    // "-1.8"

const y = new BigNumber(-1.3);
y.negated();                    // "1.3"

Method Chaining

All arithmetic methods return new BigNumber instances, allowing for method chaining.

Usage Examples:

const x = new BigNumber(5);
const y = new BigNumber(3);
const z = new BigNumber(2);

// Chain multiple operations
const result = x.dividedBy(y).plus(z).times(9);
console.log(result.toString()); // Complex calculation result

// Long chain example
const complex = x
  .times('1.23456780123456789e+9')
  .plus(9876.5432321)
  .dividedBy('4444562598.111772')
  .integerValue();

// Method aliases in chains
const alias = x.sqrt().div(y).pow(3);
const full = x.squareRoot().dividedBy(y).exponentiatedBy(3);
console.log(alias.isEqualTo(full)); // true

docs

arithmetic.md

comparison.md

configuration.md

constructor.md

conversion.md

index.md

precision.md

tile.json