UNPKG

@lodestar/utils

Version:

Utilities required across multiple lodestar packages

50 lines 1.13 kB
/** * Return the min number between two big numbers. */ export function bigIntMin(a, b) { return a < b ? a : b; } /** * Return the max number between two big numbers. */ export function bigIntMax(a, b) { return a > b ? a : b; } export function intDiv(dividend, divisor) { return Math.floor(dividend / divisor); } /** * Calculate the largest integer k such that k**2 <= n. */ export function intSqrt(n) { let x = n; let y = intDiv(x + 1, 2); while (y < x) { x = y; y = intDiv(x + intDiv(n, x), 2); } return x; } export function bigIntSqrt(n) { let x = n; let y = (x + BigInt(1)) / BigInt(2); while (y < x) { x = y; y = (x + n / x) / BigInt(2); } return x; } /** * Regenerates a random integer between min (included) and max (excluded). */ export function randBetween(min, max) { return Math.floor(Math.random() * (max - min)) + min; } /** * Wraps randBetween and returns a bigNumber. * @returns {bigint} */ export function randBetweenBigInt(min, max) { return BigInt(randBetween(min, max)); } //# sourceMappingURL=math.js.map