Skip to content

Num.BigInteger

Num / BigInteger

Import

typescript
import { Num } from '@wollybeard/kit'

// Access via namespace
Num.BigInteger.someFunction()
typescript
import * as Num from '@wollybeard/kit/num'

// Access via namespace
Num.BigInteger.someFunction()

Functions

[F] is

typescript
(value: unknown): boolean

Parameters:

  • value - The value to check

Returns: True if value is a BigInteger

Type predicate to check if value is a BigInteger.

[F] from

typescript
(value: string | number | bigint): BigInteger

Parameters:

  • value - The value to convert (number, string, or bigint)

Returns: The BigInteger representation

Construct a BigInteger from various input types.

Accepts numbers, strings, and existing bigints, providing a safe way to create arbitrary precision integers from different sources.

[F] add

typescript
(a: BigInteger, b: BigInteger): BigInteger

Parameters:

  • a - First BigInteger to add
  • b - Second BigInteger to add

Returns: The exact sum as a BigInteger

Add two BigIntegers together.

Performs exact addition without precision loss, regardless of the size of the numbers involved.

[F] subtract

typescript
(a: BigInteger, b: BigInteger): BigInteger

Parameters:

  • a - First BigInteger (minuend)
  • b - Second BigInteger (subtrahend)

Returns: The exact difference as a BigInteger

Subtract two BigIntegers.

[F] multiply

typescript
(a: BigInteger, b: BigInteger): BigInteger

Parameters:

  • a - First BigInteger
  • b - Second BigInteger

Returns: The exact product as a BigInteger

Multiply two BigIntegers.

[F] divide

typescript
(a: BigInteger, b: BigInteger): BigInteger

Parameters:

  • a - First BigInteger (dividend)
  • b - Second BigInteger (divisor, must be non-zero)

Returns: The quotient as a BigInteger (truncated)

Throws:

  • Error if divisor is zero

Divide two BigIntegers using integer division (truncates toward zero).

[F] remainder

typescript
(a: BigInteger, b: BigInteger): BigInteger

Parameters:

  • a - First BigInteger (dividend)
  • b - Second BigInteger (divisor, must be non-zero)

Returns: The remainder as a BigInteger

Throws:

  • Error if divisor is zero

Get the remainder of BigInteger division.

[F] power

typescript
(base: BigInteger, exponent: BigInteger): BigInteger

Parameters:

  • base - The BigInteger base
  • exponent - The BigInteger exponent (must be non-negative)

Returns: base raised to the power of exponent

Throws:

  • Error if exponent is negative

Raise a BigInteger to a power.

[F] abs

typescript
(value: BigInteger): BigInteger

Parameters:

  • value - The BigInteger

Returns: The absolute value as a BigInteger

Get the absolute value of a BigInteger.

[F] compare

typescript
(a: BigInteger, b: BigInteger): 0 | 1 | -1

Parameters:

  • a - First BigInteger
  • b - Second BigInteger

Returns: -1 if a b, 0 if a = b, 1 if a b

Compare two BigIntegers.

[F] isEven

typescript
(value: BigInteger): boolean

Parameters:

  • value - The BigInteger to check

Returns: True if the BigInteger is even

Check if a BigInteger is even.

[F] isOdd

typescript
(value: BigInteger): boolean

Parameters:

  • value - The BigInteger to check

Returns: True if the BigInteger is odd

Check if a BigInteger is odd.

[F] isPositive

typescript
(value: BigInteger): boolean

Parameters:

  • value - The BigInteger to check

Returns: True if the BigInteger is positive

Check if a BigInteger is positive ( 0).

[F] isNegative

typescript
(value: BigInteger): boolean

Parameters:

  • value - The BigInteger to check

Returns: True if the BigInteger is negative

Check if a BigInteger is negative ( 0).

[F] isZero

typescript
(value: BigInteger): boolean

Parameters:

  • value - The BigInteger to check

Returns: True if the BigInteger is zero

Check if a BigInteger is zero.

[F] toNumber

typescript
(value: BigInteger): number

Parameters:

  • value - The BigInteger to convert

Returns: The number representation

Throws:

  • Error if the BigInteger is too large for safe conversion

Convert BigInteger to regular number.

WARNING: May lose precision if the BigInteger is larger than Number.MAX_SAFE_INTEGER.

[F] toString

typescript
(value: BigInteger, radix?: number = 10): string

Parameters:

  • value - The BigInteger to convert
  • radix - The base to use (default: 10)

Returns: String representation

Convert BigInteger to string representation.

Constants

[C] ZERO

typescript
BigInteger

BigInteger constants for common values.

[C] ONE

typescript
BigInteger

[C] TWO

typescript
BigInteger

[C] addOn

typescript
;((a: BigInteger) => (b: BigInteger) => BigInteger)

Create a function that operates on a specific BigInteger by adding to it. Data-first pattern: the fixed value is the first parameter.

[C] addWith

typescript
;((b: BigInteger) => (a: BigInteger) => BigInteger)

Create a function that adds with a specific BigInteger value. Data-second pattern: the fixed value is the second parameter.

[C] subtractWith

typescript
;((b: BigInteger) => (a: BigInteger) => BigInteger)

Create a function that subtracts with a specific BigInteger. Data-second pattern: the fixed value is the second parameter (subtrahend).

[C] subtractOn

typescript
;((a: BigInteger) => (b: BigInteger) => BigInteger)

Create a function that operates on a specific BigInteger by subtracting from it. Data-first pattern: the fixed value is the first parameter (minuend).

[C] multiplyOn

typescript
;((a: BigInteger) => (b: BigInteger) => BigInteger)

Create a function that operates on a specific BigInteger by multiplying it. Data-first pattern: the fixed value is the first parameter.

[C] multiplyWith

typescript
;((b: BigInteger) => (a: BigInteger) => BigInteger)

Create a function that multiplies with a specific BigInteger. Data-second pattern: the fixed value is the second parameter (multiplier).

[C] divideOn

typescript
;((a: BigInteger) => (b: BigInteger) => BigInteger)

Create a function that operates on a specific BigInteger by dividing it. Data-first pattern: the fixed value is the first parameter (dividend).

[C] divideWith

typescript
;((b: BigInteger) => (a: BigInteger) => BigInteger)

Create a function that divides by a specific BigInteger. Data-second pattern: the fixed value is the second parameter (divisor).

[C] remainderOn

typescript
;((a: BigInteger) => (b: BigInteger) => BigInteger)

Create a function that operates on a specific BigInteger to get its remainder. Data-first pattern: the fixed value is the first parameter (dividend).

[C] remainderWith

typescript
;((b: BigInteger) => (a: BigInteger) => BigInteger)

Create a function that gets remainder with a specific divisor. Data-second pattern: the fixed value is the second parameter (divisor).

[C] powerOn

typescript
;((base: BigInteger) => (exponent: BigInteger) => BigInteger)

Create a function that operates on a specific BigInteger base by raising it to powers. Data-first pattern: the fixed value is the first parameter (base).

[C] powerWith

typescript
;((exponent: BigInteger) => (base: BigInteger) => BigInteger)

Create a function that raises to a specific power. Data-second pattern: the fixed value is the second parameter (exponent).

[C] compareOn

typescript
;((a: BigInteger) => (b: BigInteger) => 0 | 1 | -1)

Create a function that operates on a specific BigInteger by comparing it. Data-first pattern: the fixed value is the first parameter.

[C] compareWith

typescript
;((b: BigInteger) => (a: BigInteger) => 0 | 1 | -1)

Create a function that compares with a specific BigInteger. Data-second pattern: the fixed value is the second parameter.

Types

[∩] BigInteger

typescript
type BigInteger = bigint & { [BigIntegerBrand]: true }

BigInteger

  • arbitrary precision integer with branded type safety.

Provides exact arithmetic for integers of any size, without the limitations of JavaScript's Number type (which loses precision beyond 2^53-1).

Common uses:

  • Cryptography: Large prime numbers, key generation, modular arithmetic
  • Financial systems: Precise monetary calculations without rounding errors
  • Mathematical computing: Factorials, combinatorics, number theory
  • Blockchain: Transaction values, block numbers, hash computations
  • Scientific computing: Large dataset indexing, ID generation