UNPKG

@chainsafe/eth2.0-utils

Version:

Utilities required across multiple lodestar packages

62 lines (49 loc) 949 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.bigIntMin = bigIntMin; exports.bigIntMax = bigIntMax; exports.intDiv = intDiv; exports.intSqrt = intSqrt; exports.bigIntSqrt = bigIntSqrt; /** * @module util/math */ /** * Return the min number between two big numbers. */ function bigIntMin(a, b) { return a < b ? a : b; } /** * Return the max number between two big numbers. */ function bigIntMax(a, b) { return a > b ? a : b; } function intDiv(dividend, divisor) { return Math.floor(dividend / divisor); } /** * Calculate the largest integer k such that k**2 <= n. */ 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; } function bigIntSqrt(n) { let x = n; let y = (x + 1n) / 2n; while (y < x) { x = y; y = (x + n / x) / 2n; } return x; } //# sourceMappingURL=math.js.map