1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.nSqrt = void 0;
|
4 | const x_bigint_1 = require("@polkadot/x-bigint");
|
5 | const consts_js_1 = require("./consts.js");
|
6 | const toBigInt_js_1 = require("./toBigInt.js");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function nSqrt(value) {
|
12 | const n = (0, toBigInt_js_1.nToBigInt)(value);
|
13 | if (n < consts_js_1._0n) {
|
14 | throw new Error('square root of negative numbers is not supported');
|
15 | }
|
16 |
|
17 |
|
18 | if (n <= consts_js_1._2pow53n) {
|
19 |
|
20 | return (0, x_bigint_1.BigInt)(~~Math.sqrt(Number(n)));
|
21 | }
|
22 |
|
23 |
|
24 | let x0 = consts_js_1._sqrt2pow53n;
|
25 | while (true) {
|
26 | const x1 = ((n / x0) + x0) >> consts_js_1._1n;
|
27 | if (x0 === x1 || (x0 === (x1 - consts_js_1._1n))) {
|
28 | return x0;
|
29 | }
|
30 | x0 = x1;
|
31 | }
|
32 | }
|
33 | exports.nSqrt = nSqrt;
|