Num.BigInteger
Num / BigInteger
Import
import { Num } from '@wollybeard/kit'
// Access via namespace
Num.BigInteger.someFunction()
import * as Num from '@wollybeard/kit/num'
// Access via namespace
Num.BigInteger.someFunction()
Functions
[F]
is
(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
(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
(a: BigInteger, b: BigInteger): BigInteger
Parameters:
a
- First BigInteger to addb
- 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
(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
(a: BigInteger, b: BigInteger): BigInteger
Parameters:
a
- First BigIntegerb
- Second BigInteger
Returns: The exact product as a BigInteger
Multiply two BigIntegers.
[F]
divide
(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
(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
(base: BigInteger, exponent: BigInteger): BigInteger
Parameters:
base
- The BigInteger baseexponent
- 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
(value: BigInteger): BigInteger
Parameters:
value
- The BigInteger
Returns: The absolute value as a BigInteger
Get the absolute value of a BigInteger.
[F]
compare
(a: BigInteger, b: BigInteger): 0 | 1 | -1
Parameters:
a
- First BigIntegerb
- Second BigInteger
Returns: -1 if a b, 0 if a = b, 1 if a b
Compare two BigIntegers.
[F]
isEven
(value: BigInteger): boolean
Parameters:
value
- The BigInteger to check
Returns: True if the BigInteger is even
Check if a BigInteger is even.
[F]
isOdd
(value: BigInteger): boolean
Parameters:
value
- The BigInteger to check
Returns: True if the BigInteger is odd
Check if a BigInteger is odd.
[F]
isPositive
(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
(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
(value: BigInteger): boolean
Parameters:
value
- The BigInteger to check
Returns: True if the BigInteger is zero
Check if a BigInteger is zero.
[F]
toNumber
(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
(value: BigInteger, radix?: number = 10): string
Parameters:
value
- The BigInteger to convertradix
- The base to use (default: 10)
Returns: String representation
Convert BigInteger to string representation.
Constants
[C]
ZERO
BigInteger
BigInteger constants for common values.
[C]
ONE
BigInteger
[C]
TWO
BigInteger
[C]
addOn
;((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
;((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
;((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
;((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
;((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
;((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
;((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
;((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
;((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
;((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
;((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
;((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
;((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
;((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
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