Const
Returns the absolute value of a safe integer.
Note: Math.abs(MIN_SAFE_INTEGER)
would exceed MAX_SAFE_INTEGER
, so this
function clamps the result to maintain the safe integer guarantee.
Adds two SafeInt values.
Clamps a number to the safe integer range.
Divides one SafeInt by another using floor division.
Performs mathematical floor division: ⌊a / b⌋
. The divisor must be
non-zero (enforced by type constraints).
Type guard that checks if a value is a safe integer.
assert.ok(isSafeInt(Number.MAX_SAFE_INTEGER));
assert.notOk(isSafeInt(Number.MAX_SAFE_INTEGER + 0.5));
assert.ok(SafeInt.is(Number.MIN_SAFE_INTEGER));
isSafeInt for usage examples
Readonly
max: (...values: readonly WithSmallInt<SafeInt, 40>[]) => SafeIntReturns the maximum value from a list of safe integers.
Readonly
MAX_VALUE: SafeUintThe maximum safe integer value (2^53 - 1).
Readonly
min: (...values: readonly WithSmallInt<SafeInt, 40>[]) => SafeIntReturns the minimum value from a list of safe integers.
Readonly
MIN_VALUE: SafeIntThe minimum safe integer value (-(2^53 - 1)).
Multiplies two SafeInt values.
Raises a SafeInt to the power of another SafeInt.
Generates a random safe integer within the specified range (inclusive).
The range is inclusive on both ends. If min > max, they are automatically swapped.
Subtracts one SafeInt from another.
Namespace providing type-safe operations for SafeInt branded types.
SafeInt represents integers that can be exactly represented in JavaScript's number type without precision loss. The range is [±(2^53 - 1)], which covers approximately ±9 quadrillion.
All operations automatically clamp results to stay within the safe range, preventing precision loss that occurs with larger integers. This makes SafeInt ideal for: