BigIntMath: {
    abs(value: BigIntable): bigint;
    bitLength(value: BigIntable): number;
    ceilDivide(dividend: BigIntable, divisor: BigIntable): bigint;
    divide(
        dividend: BigIntable,
        divisor: BigIntable,
    ): { quotient: bigint; remainder: bigint };
    max(...args: BigIntable[]): bigint;
    min(...args: BigIntable[]): bigint;
    rand(max: bigint): bigint;
    roundDivide(dividend: BigIntable, divisor: BigIntable): bigint;
    sign(value: BigIntable): -1 | 0 | 1;
} = ...

A collection of functions to perform mathematical operations on BigInts

Type declaration

  • abs:function
    • Returns the absolute value of a given number

      Parameters

      • value: BigIntable

        The value to get the absolute value of

      Returns bigint

      The absolute value of the input

      BigIntMath.abs(-1n) // 1n
      BigIntMath.abs(1n) // 1n
      BigIntMath.abs(-1) // 1n
      BigIntMath.abs("-1") // 1n
  • bitLength:function
    • Returns the number of bits required to represent a given number

      Parameters

      • value: BigIntable

        The number to get the bit length of

      Returns number

      The number of bits required to represent the input number (minimum 1)

      BigIntMath.bitLength(0n)   // 1
      BigIntMath.bitLength(1n) // 1
      BigIntMath.bitLength(255n) // 8
      BigIntMath.bitLength(-1n) // 1
  • ceilDivide:function
    • Returns the quotient of a division between two numbers, rounded up

      Parameters

      Returns bigint

      The quotient of the division, rounded up

      BigIntMath.ceilDivide(5n, 2n) // 3n
      BigIntMath.ceilDivide(4n, 2n) // 2n
  • divide:function
    • Returns the quotient and remainder of a division between two numbers

      Parameters

      Returns { quotient: bigint; remainder: bigint }

      The quotient and remainder of the division

      RangeError If the divisor is zero

  • max:function
    • Returns the maximum of a list of numbers

      Parameters

      • ...args: BigIntable[]

        The numbers to get the maximum of

      Returns bigint

      The maximum of the input numbers transformed to a BigInt

      RangeError If no arguments are provided

      BigIntMath.max(1n, 2n, 3n) // 3n
      BigIntMath.max(1, 2n, "3") // 3n
  • min:function
    • Returns the minimum of a list of numbers

      Parameters

      • ...args: BigIntable[]

        The numbers to get the minimum of

      Returns bigint

      The minimum of the input numbers transformed to a BigInt

      RangeError If no arguments are provided

      BigIntMath.min(1n, 2n, 3n) // 1n
      BigIntMath.min(1, 2n, "3") // 1n
  • rand:function
    • Returns a random number between 0 (inclusive) and a given maximum (exclusive)

      Parameters

      • max: bigint

        The exclusive upper bound (must be positive)

      Returns bigint

      A random number in the range [0, max)

      RangeError If max is not positive

      This function uses a cryptographically secure random number generator In Node.js, it uses crypto.randomBytes In browsers, it uses crypto.getRandomValues

      BigIntMath.rand(10n) // A random number between 0 and 9 (inclusive)
      
  • roundDivide:function
    • Returns the quotient of a division between two numbers, rounded to the nearest integer

      Parameters

      Returns bigint

      The quotient of the division, rounded to the nearest integer

      BigIntMath.roundDivide(7n, 3n) // 2n
      BigIntMath.roundDivide(7n, 2n) // 4n (3.5 rounded to the nearest integer)
  • sign:function
    • Returns the sign of a given number

      Parameters

      Returns -1 | 0 | 1

      -1 if the value is negative, 0 if the value is 0, 1 if the value is positive

      BigIntMath.sign(-1n) // -1
      BigIntMath.sign(0n) // 0
      BigIntMath.sign(1n) // 1