Skip to main content

bigmath

This module provides mathematical utility functions for working with BigInt values, offering operations like absolute value, multiplication with division, min/max calculations, averaging, and various rounding strategies.

import { bigMath } from "@gmx-ui/sdk/utils/bigmath";

Methods

abs

  • abs(x: bigint): bigint

Returns the absolute value of a BigInt number.

import { bigMath } from "@gmx-ui/sdk/utils/bigmath";

const result = bigMath.abs(-100n); // 100n
const positive = bigMath.abs(50n); // 50n

mulDiv

  • mulDiv(x: bigint, y: bigint, z: bigint, roundUpMagnitude?: boolean): bigint

Parameters

  • x: bigint - The first multiplicand
  • y: bigint - The second multiplicand
  • z: bigint - The divisor
  • roundUpMagnitude: boolean - Optional flag to round up when there's a remainder (default: false)

Multiplies two BigInt values and divides by a third, with optional rounding up.

import { bigMath } from "@gmx-ui/sdk/utils/bigmath";

const result = bigMath.mulDiv(100n, 3n, 2n); // 150n
const rounded = bigMath.mulDiv(100n, 3n, 7n, true); // 43n (rounded up from 42.857...)

max

  • max(max: bigint, ...rest: bigint[]): bigint

Returns the maximum value from the provided BigInt arguments.

import { bigMath } from "@gmx-ui/sdk/utils/bigmath";

const result = bigMath.max(10n, 25n, 5n, 100n); // 100n
const single = bigMath.max(42n); // 42n

min

  • min(min: bigint, ...rest: bigint[]): bigint

Returns the minimum value from the provided BigInt arguments.

import { bigMath } from "@gmx-ui/sdk/utils/bigmath";

const result = bigMath.min(10n, 25n, 5n, 100n); // 5n
const single = bigMath.min(42n); // 42n

avg

  • avg(...values: (bigint | undefined)[]): bigint | undefined

Calculates the average of the provided BigInt values, ignoring undefined values. Returns undefined if no valid values are provided.

import { bigMath } from "@gmx-ui/sdk/utils/bigmath";

const result = bigMath.avg(10n, 20n, 30n); // 20n
const withUndefined = bigMath.avg(10n, undefined, 30n); // 20n
const empty = bigMath.avg(undefined, undefined); // undefined

divRound

  • divRound(x: bigint, y: bigint): bigint

Divides two BigInt values with standard rounding (rounds to nearest integer).

import { bigMath } from "@gmx-ui/sdk/utils/bigmath";

const result = bigMath.divRound(7n, 3n); // 2n
const rounded = bigMath.divRound(8n, 3n); // 3n

divRoundUp

  • divRoundUp(x: bigint, y: bigint): bigint

Divides two BigInt values always rounding up to the next integer.

import { bigMath } from "@gmx-ui/sdk/utils/bigmath";

const result = bigMath.divRoundUp(7n, 3n); // 3n
const exact = bigMath.divRoundUp(6n, 3n); // 2n

mulmod

  • mulmod(x: bigint, y: bigint, m: bigint): bigint

Parameters

  • x: bigint - The first multiplicand
  • y: bigint - The second multiplicand
  • m: bigint - The modulus

Multiplies two BigInt values and returns the remainder when divided by the modulus.

import { bigMath } from "@gmx-ui/sdk/utils/bigmath";

const result = bigMath.mulmod(7n, 8n, 10n); // 6n (56 % 10)
const zero = bigMath.mulmod(5n, 4n, 10n); // 0n (20 % 10)