Skip to content
velt/math exports

velt/math

API reference

Integer 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.

import { gcd, BigInt } from "velt/math";

Exports

Re-exported from velt/math/integer

function clamp(x: i64, lo: i64, hi: i64): i64

x limited to [lo, hi] (assumes lo <= hi).

Re-exported from velt/math/integer

function fib(n: i64): i64

n-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): i64

Greatest common divisor (Euclid), always >= 0; gcd(0, 0) is 0.

Re-exported from velt/math/integer

function isPrime(n: i64): bool

Trial division by 6k +/- 1; O(sqrt n).

Re-exported from velt/math/integer

function lcm(a: i64, b: i64): i64

Least 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): BigInt

JS 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): BigInt

Parses digits in radix (2 to 36) with an optional sign; throws BigIntError.

add(other: BigInt): BigIntNo doc comment yet
sub(other: BigInt): BigIntNo doc comment yet
mul(other: BigInt): BigIntNo doc comment yet
div(other: BigInt): BigIntNo doc comment yet
rem(other: BigInt): BigIntNo doc comment yet
addInt(k: i64): BigIntNo doc comment yet
subInt(k: i64): BigIntNo doc comment yet
mulInt(k: i64): BigIntNo doc comment yet
divInt(k: i64): BigIntNo doc comment yet
remInt(k: i64): BigIntNo doc comment yet
shl(n: i64): BigInt

this << n (n >= 0).

shr(n: i64): BigInt

this >> n (n >= 0; rounds toward -Infinity, like JS).

neg(): BigIntNo doc comment yet
addAssign(other: BigInt)No doc comment yet
subAssign(other: BigInt)No doc comment yet
mulAssign(other: BigInt)No doc comment yet
divAssign(other: BigInt)No doc comment yet
remAssign(other: BigInt)No doc comment yet
addIntAssign(k: i64)No doc comment yet
subIntAssign(k: i64)No doc comment yet
mulIntAssign(k: i64)No doc comment yet
divIntAssign(k: i64)No doc comment yet
shlAssign(n: i64)No doc comment yet
shrAssign(n: i64)No doc comment yet
set(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 yet
toNumber(): f64

The nearest number (JS Number(big)).

toI64(): i64

The low 64 bits as a signed integer (JS BigInt.asIntN(64, big)).

toString(radix: i64 = 10): string

Digits in radix (2 to 36), lowercase, with a leading - when negative.

clone(): BigInt

An independent copy.

[Symbol.dispose]()

Drop hook: frees the number.

class

BigIntError

#std/math/errors.vlt:4

Re-exported from velt/math/errors

class BigIntError extends Error

A string that is not a valid integer (JS throws SyntaxError).