velt/math
API referenceInteger helpers and arbitrary-precision integers. Float math (Math.sqrt, Math.floor, Math.random, …) is the prelude's Math, a global as in JavaScript. For a walkthrough, read the guide.
Exports
x limited to [lo, hi] (assumes lo <= hi).fibn-th Fibonacci number (fib(0) = 0, fib(1) = 1), iterative; 0 for negative n.gcdGreatest common divisor (Euclid), always >= 0; gcd(0, 0) is 0.isPrimeTrial division by 6k +/- 1; O(sqrt n).lcmLeast common multiple, always >= 0; 0 if either argument is 0.BigIntAn arbitrary-precision integer (JS BigInt); see the module comment.BigIntErrorA string that is not a valid integer (JS throws SyntaxError).Re-exported from velt/math/integer
function clamp(x: i64, lo: i64, hi: i64): i64x limited to [lo, hi] (assumes lo <= hi).
Re-exported from velt/math/integer
function fib(n: i64): i64n-th Fibonacci number (fib(0) = 0, fib(1) = 1), iterative; 0 for negative n.
Re-exported from velt/math/integer
function gcd(a: i64, b: i64): i64Greatest common divisor (Euclid), always >= 0; gcd(0, 0) is 0.
Re-exported from velt/math/integer
function isPrime(n: i64): boolTrial division by 6k +/- 1; O(sqrt n).
Re-exported from velt/math/integer
function lcm(a: i64, b: i64): i64Least common multiple, always >= 0; 0 if either argument is 0. Divides before multiplying to delay overflow (which wraps, like all i64 arithmetic).
Re-exported from velt/math/bigint
An arbitrary-precision integer (JS BigInt); see the module comment.
constructor(value: i64 = 0)JS BigInt(n) / 10n.
static fromNumber(x: f64): BigIntJS BigInt(x) for a float: the integer part of x (panics on NaN and ±Infinity, which JS
rejects with a RangeError).
static parse(s: string, radix: i64 = 10): BigIntParses digits in radix (2 to 36) with an optional sign; throws BigIntError.
addInt(k: i64): BigIntNo doc comment yetsubInt(k: i64): BigIntNo doc comment yetmulInt(k: i64): BigIntNo doc comment yetdivInt(k: i64): BigIntNo doc comment yetremInt(k: i64): BigIntNo doc comment yetshl(n: i64): BigIntthis << n (n >= 0).
shr(n: i64): BigIntthis >> n (n >= 0; rounds toward -Infinity, like JS).
neg(): BigIntNo doc comment yetaddAssign(other: BigInt)No doc comment yetsubAssign(other: BigInt)No doc comment yetmulAssign(other: BigInt)No doc comment yetdivAssign(other: BigInt)No doc comment yetremAssign(other: BigInt)No doc comment yetaddIntAssign(k: i64)No doc comment yetsubIntAssign(k: i64)No doc comment yetmulIntAssign(k: i64)No doc comment yetdivIntAssign(k: i64)No doc comment yetshlAssign(n: i64)No doc comment yetshrAssign(n: i64)No doc comment yetset(other: BigInt)Makes this number equal to other, reusing this one's buffer.
compareTo(other: BigInt): i64-1, 0 or 1 as this is less than, equal to or greater than other.
compareToInt(k: i64): i64-1, 0 or 1 as this is less than, equal to or greater than k.
equals(other: BigInt): boolNo doc comment yettoNumber(): f64The nearest number (JS Number(big)).
toI64(): i64The low 64 bits as a signed integer (JS BigInt.asIntN(64, big)).
toString(radix: i64 = 10): stringDigits in radix (2 to 36), lowercase, with a leading - when negative.
clone(): BigIntAn independent copy.
[Symbol.dispose]()Drop hook: frees the number.
Re-exported from velt/math/errors
class BigIntError extends ErrorA string that is not a valid integer (JS throws SyntaxError).