or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

arithmetic.mdcomparison.mdconfiguration.mdconstructor.mdindex.mdprecision.mdstring-conversion.mdtype-checking.md
tile.json

arithmetic.mddocs/

Arithmetic Operations

Mathematical operations for BigNumber instances including basic arithmetic, advanced operations, and utility methods.

Capabilities

Addition

Adds two BigNumber values with full precision.

/**
 * Returns sum of this BigNumber and n
 * @param {string|number|BigNumber} n - Value to add
 * @param {number} [base] - Base of n if string
 * @returns {BigNumber} Sum result
 */
plus(n, base): BigNumber

Usage Examples:

const x = new BigNumber(0.1);
const y = new BigNumber(0.2);
const sum = x.plus(y);  // '0.3' (exact, not 0.30000000000000004)

// With different input types
x.plus(5);              // add number
x.plus('0.1', 8);       // add string in base 8
x.plus(new BigNumber(0.3));  // add another BigNumber

Subtraction

Subtracts one BigNumber from another with full precision.

/**
 * Returns difference of this BigNumber minus n
 * @param {string|number|BigNumber} n - Value to subtract
 * @param {number} [base] - Base of n if string
 * @returns {BigNumber} Difference result
 */
minus(n, base): BigNumber

Usage Examples:

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

// Subtraction preserves precision
const large = new BigNumber('1111222233334444555566');
const result = large.minus(1);  // '1111222233334444555565'

Multiplication

Multiplies BigNumber values with full precision.

/**
 * Returns product of this BigNumber and n
 * @param {string|number|BigNumber} n - Value to multiply by
 * @param {number} [base] - Base of n if string
 * @returns {BigNumber} Product result
 */
multipliedBy(n, base): BigNumber

/**
 * Alias for multipliedBy
 */
times(n, base): BigNumber

Usage Examples:

const x = new BigNumber(0.6);
const product = x.multipliedBy(3);  // '1.8' (exact, not 1.7999999999999998)

// Multiplication with large numbers
const big = new BigNumber('7e+500');
const result = big.times(2);  // '1.4e+501'

// Using alias
x.times('-a', 16);  // multiply by -10 (base 16)

Division

Divides BigNumber values according to current DECIMAL_PLACES and ROUNDING_MODE settings.

/**
 * Returns quotient of this BigNumber divided by n
 * @param {string|number|BigNumber} n - Divisor
 * @param {number} [base] - Base of n if string
 * @returns {BigNumber} Quotient result
 */
dividedBy(n, base): BigNumber

/**
 * Alias for dividedBy
 */
div(n, base): BigNumber

Usage Examples:

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

// Division respects configuration
BigNumber.config({ DECIMAL_PLACES: 5 });
x.div(3);  // '118.33333'

Integer Division

Returns the integer part of division.

/**
 * Returns integer part of this BigNumber divided by n
 * @param {string|number|BigNumber} n - Divisor
 * @param {number} [base] - Base of n if string
 * @returns {BigNumber} Integer quotient
 */
dividedToIntegerBy(n, base): BigNumber

/**
 * Alias for dividedToIntegerBy
 */
idiv(n, base): BigNumber

Usage Examples:

const x = new BigNumber(5);
const y = new BigNumber(3);
const intDiv = x.dividedToIntegerBy(y);  // '1'

x.idiv(0.7);        // '7'
x.idiv('0.f', 16);  // '5' (15 in base 16)

Modulo Operation

Returns the remainder of division according to MODULO_MODE setting.

/**
 * Returns remainder of this BigNumber divided by n
 * @param {string|number|BigNumber} n - Divisor
 * @param {number} [base] - Base of n if string
 * @returns {BigNumber} Remainder result
 */
modulo(n, base): BigNumber

/**
 * Alias for modulo
 */
mod(n, base): BigNumber

Usage Examples:

const x = new BigNumber(1);
const remainder = x.modulo(0.9);  // '0.1' (exact, not 0.09999999999999998)

const y = new BigNumber(33);
y.mod('a', 33);  // '3' (33 mod 10 in base 33)

// Modulo mode affects sign
BigNumber.config({ MODULO_MODE: BigNumber.EUCLID });

Exponentiation

Raises BigNumber to a power, optionally with modular exponentiation.

/**
 * Returns this BigNumber raised to power n, optionally modulo m
 * @param {string|number|BigNumber} n - Exponent (must be integer)
 * @param {string|number|BigNumber} [m] - Modulus for modular exponentiation
 * @returns {BigNumber} Result of exponentiation
 */
exponentiatedBy(n, m): BigNumber

/**
 * Alias for exponentiatedBy
 */
pow(n, m): BigNumber

Usage Examples:

const x = new BigNumber(0.7);
const powered = x.exponentiatedBy(2);  // '0.49' (exact)

// Large exponentiation with precision limit
BigNumber.config({ POW_PRECISION: 100 });
const large = new BigNumber(2).pow(1000);

// Modular exponentiation
const modResult = new BigNumber(3).pow(4, 5);  // (3^4) mod 5 = 1

Square Root

Returns the correctly-rounded square root.

/**
 * Returns square root of this BigNumber
 * @returns {BigNumber} Square root result
 */
squareRoot(): BigNumber

/**
 * Alias for squareRoot
 */
sqrt(): BigNumber

Usage Examples:

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

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

// Square root of negative numbers
const neg = new BigNumber(-4);
neg.sqrt();  // 'NaN'

Absolute Value

Returns the absolute value (magnitude) of the BigNumber.

/**
 * Returns absolute value of this BigNumber
 * @returns {BigNumber} Absolute value
 */
absoluteValue(): BigNumber

/**
 * Alias for absoluteValue
 */
abs(): BigNumber

Usage Examples:

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

const y = new BigNumber(123);
y.abs();  // '123' (unchanged for positive)

Negation

Returns the negated value of the BigNumber.

/**
 * Returns negated value of this BigNumber
 * @returns {BigNumber} Negated value
 */
negated(): BigNumber

Usage Examples:

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

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

// Negation of zero
const zero = new BigNumber(0);
zero.negated();  // '0' (not '-0')

Precision and Rounding

All arithmetic operations follow these precision rules:

  • Addition/Subtraction/Multiplication: Always exact and unrounded
  • Division: Rounded to DECIMAL_PLACES using ROUNDING_MODE
  • Square Root: Rounded to DECIMAL_PLACES using ROUNDING_MODE
  • Exponentiation: Limited by POW_PRECISION setting

Configuration Impact

Arithmetic operations are affected by global configuration:

BigNumber.config({
  DECIMAL_PLACES: 10,
  ROUNDING_MODE: BigNumber.ROUND_HALF_UP,
  POW_PRECISION: 100
});

Immutability

All arithmetic operations return new BigNumber instances. The original BigNumber is never modified:

const x = new BigNumber(5);
const y = x.plus(3);  // Returns new BigNumber('8')
console.log(x);       // Still '5'