Expose sqrt(MAX_SAFE_INTEGER) for bi/bn (#1717)

This commit is contained in:
Jaco
2023-01-26 10:20:59 +02:00
committed by GitHub
parent 7bf2d220dd
commit e93b7a3626
6 changed files with 52 additions and 48 deletions
+22 -16
View File
@@ -7,91 +7,91 @@ import { BigInt } from '@polkadot/x-bigint';
* @name _0n
* @summary BigInt constant for 0.
*/
export const _0n = BigInt(0);
export const _0n = /*#__PURE__*/ BigInt(0);
/**
* @name _1n
* @summary BigInt constant for 1.
*/
export const _1n = BigInt(1);
export const _1n = /*#__PURE__*/ BigInt(1);
/**
* @name _2n
* @summary BigInt constant for 2.
*/
export const _2n = BigInt(2);
export const _2n = /*#__PURE__*/ BigInt(2);
/**
* @name _3n
* @summary BigInt constant for 3.
*/
export const _3n = BigInt(3);
export const _3n = /*#__PURE__*/ BigInt(3);
/**
* @name _4n
* @summary BigInt constant for 4.
*/
export const _4n = BigInt(4);
export const _4n = /*#__PURE__*/ BigInt(4);
/**
* @name _5n
* @summary BigInt constant for 5.
*/
export const _5n = BigInt(5);
export const _5n = /*#__PURE__*/ BigInt(5);
/**
* @name _6n
* @summary BigInt constant for 6.
*/
export const _6n = BigInt(6);
export const _6n = /*#__PURE__*/ BigInt(6);
/**
* @name _7n
* @summary BigInt constant for 7.
*/
export const _7n = BigInt(7);
export const _7n = /*#__PURE__*/ BigInt(7);
/**
* @name _8n
* @summary BigInt constant for 8.
*/
export const _8n = BigInt(8);
export const _8n = /*#__PURE__*/ BigInt(8);
/**
* @name _9n
* @summary BigInt constant for 9.
*/
export const _9n = BigInt(9);
export const _9n = /*#__PURE__*/ BigInt(9);
/**
* @name _10n
* @summary BigInt constant for 10.
*/
export const _10n = BigInt(10);
export const _10n = /*#__PURE__*/ BigInt(10);
/**
* @name _100n
* @summary BigInt constant for 100.
*/
export const _100n = BigInt(100);
export const _100n = /*#__PURE__*/ BigInt(100);
/**
* @name _1000n
* @summary BigInt constant for 1000.
*/
export const _1000n = BigInt(1_000);
export const _1000n = /*#__PURE__*/ BigInt(1_000);
/**
* @name _1Mn
* @summary BigInt constant for 1,000,000 (million).
*/
export const _1Mn = BigInt(1_000_000);
export const _1Mn = /*#__PURE__*/ BigInt(1_000_000);
/**
* @name _1Bn
* @summary BigInt constant for 1,000,000,000 (billion).
*/
export const _1Bn = BigInt(1_000_000_000);
export const _1Bn = /*#__PURE__*/ BigInt(1_000_000_000);
/**
* @name _1Qn
@@ -103,4 +103,10 @@ export const _1Qn = _1Bn * _1Bn;
* @name _2pow53n
* @summary BigInt constant for MAX_SAFE_INTEGER
*/
export const _2pow53n = BigInt(Number.MAX_SAFE_INTEGER);
export const _2pow53n = /*#__PURE__*/ BigInt(Number.MAX_SAFE_INTEGER);
/**
* @name _sqrt2pow53n
* @summary BigInt constant for Math.sqrt(MAX_SAFE_INTEGER)
*/
export const _sqrt2pow53n = /*#__PURE__*/ BigInt(94906265);
+2 -3
View File
@@ -2,8 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import { BN } from '../bn';
import { SQRT_MAX_SAFE_INTEGER } from './sqrt';
import { nSqrt } from '.';
import { _sqrt2pow53n, nSqrt } from '.';
export const SQRT_TESTS: [string | number | BN | bigint, string | number][] = [
[0, 0],
@@ -41,7 +40,7 @@ describe('nSqrt', (): void => {
~~Math.sqrt(
Number.MAX_SAFE_INTEGER
)
) === SQRT_MAX_SAFE_INTEGER
) === _sqrt2pow53n
).toEqual(true);
});
+2 -5
View File
@@ -6,12 +6,9 @@ import type { ToBigInt, ToBn } from '../types';
import { BigInt } from '@polkadot/x-bigint';
import { _0n, _1n, _2pow53n } from './consts';
import { _0n, _1n, _2pow53n, _sqrt2pow53n } from './consts';
import { nToBigInt } from './toBigInt';
/** @internal */
export const SQRT_MAX_SAFE_INTEGER = BigInt(94906265);
/**
* @name nSqrt
* @summary Calculates the integer square root of a bigint
@@ -32,7 +29,7 @@ export function nSqrt <ExtToBn extends ToBn | ToBigInt> (value: ExtToBn | BN | b
// Use sqrt(MAX_SAFE_INTEGER) as starting point. since we already know the
// output will be larger than this, we expect this to be a safe start
let x0 = SQRT_MAX_SAFE_INTEGER;
let x0 = _sqrt2pow53n;
while (true) {
const x1 = ((n / x0) + x0) >> _1n;
+22 -16
View File
@@ -7,91 +7,91 @@ import { BN } from './bn';
* @name BN_ZERO
* @summary BN constant for 0.
*/
export const BN_ZERO: BN = new BN(0);
export const BN_ZERO: BN = /*#__PURE__*/ new BN(0);
/**
* @name BN_ONE
* @summary BN constant for 1.
*/
export const BN_ONE: BN = new BN(1);
export const BN_ONE: BN = /*#__PURE__*/ new BN(1);
/**
* @name BN_TWO
* @summary BN constant for 2.
*/
export const BN_TWO: BN = new BN(2);
export const BN_TWO: BN = /*#__PURE__*/ new BN(2);
/**
* @name BN_THREE
* @summary BN constant for 3.
*/
export const BN_THREE: BN = new BN(3);
export const BN_THREE: BN = /*#__PURE__*/ new BN(3);
/**
* @name BN_FOUR
* @summary BN constant for 4.
*/
export const BN_FOUR: BN = new BN(4);
export const BN_FOUR: BN = /*#__PURE__*/ new BN(4);
/**
* @name BN_FIVE
* @summary BN constant for 5.
*/
export const BN_FIVE: BN = new BN(5);
export const BN_FIVE: BN = /*#__PURE__*/ new BN(5);
/**
* @name BN_SIX
* @summary BN constant for 6.
*/
export const BN_SIX: BN = new BN(6);
export const BN_SIX: BN = /*#__PURE__*/ new BN(6);
/**
* @name BN_SEVEN
* @summary BN constant for 7.
*/
export const BN_SEVEN: BN = new BN(7);
export const BN_SEVEN: BN = /*#__PURE__*/ new BN(7);
/**
* @name BN_EIGHT
* @summary BN constant for 8.
*/
export const BN_EIGHT: BN = new BN(8);
export const BN_EIGHT: BN = /*#__PURE__*/ new BN(8);
/**
* @name BN_NINE
* @summary BN constant for 9.
*/
export const BN_NINE: BN = new BN(9);
export const BN_NINE: BN = /*#__PURE__*/ new BN(9);
/**
* @name BN_TEN
* @summary BN constant for 10.
*/
export const BN_TEN: BN = new BN(10);
export const BN_TEN: BN = /*#__PURE__*/ new BN(10);
/**
* @name BN_HUNDRED
* @summary BN constant for 100.
*/
export const BN_HUNDRED: BN = new BN(100);
export const BN_HUNDRED: BN = /*#__PURE__*/ new BN(100);
/**
* @name BN_THOUSAND
* @summary BN constant for 1,000.
*/
export const BN_THOUSAND: BN = new BN(1_000);
export const BN_THOUSAND: BN = /*#__PURE__*/ new BN(1_000);
/**
* @name BN_MILLION
* @summary BN constant for 1,000,000.
*/
export const BN_MILLION: BN = new BN(1_000_000);
export const BN_MILLION: BN = /*#__PURE__*/ new BN(1_000_000);
/**
* @name BN_BILLION
* @summary BN constant for 1,000,000,000.
*/
export const BN_BILLION: BN = new BN(1_000_000_000);
export const BN_BILLION: BN = /*#__PURE__*/ new BN(1_000_000_000);
/**
* @name BN_QUINTILL
@@ -103,4 +103,10 @@ export const BN_QUINTILL: BN = BN_BILLION.mul(BN_BILLION);
* @name BN_MAX_INTEGER
* @summary BN constant for MAX_SAFE_INTEGER
*/
export const BN_MAX_INTEGER: BN = new BN(Number.MAX_SAFE_INTEGER);
export const BN_MAX_INTEGER: BN = /*#__PURE__*/ new BN(Number.MAX_SAFE_INTEGER);
/**
* @name BN_SQRT_MAX_INTEGER
* @summary BN constant for Math.sqrt(MAX_SAFE_INTEGER)
*/
export const BN_SQRT_MAX_INTEGER: BN = /*#__PURE__*/ new BN(94906265);
+2 -3
View File
@@ -2,8 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import { SQRT_TESTS } from '../bi/sqrt.spec';
import { SQRT_MAX_SAFE_INTEGER } from './sqrt';
import { BN, bnSqrt } from '.';
import { BN, BN_SQRT_MAX_INTEGER, bnSqrt } from '.';
describe('bnSqrt', (): void => {
it('fails on < 0 roots', (): void => {
@@ -14,7 +13,7 @@ describe('bnSqrt', (): void => {
it('has the correct constant for sqrt(Number.MAX_SAFE_INTEGER)', (): void => {
expect(
SQRT_MAX_SAFE_INTEGER.eq(
BN_SQRT_MAX_INTEGER.eq(
new BN(
~~Math.sqrt(
Number.MAX_SAFE_INTEGER
+2 -5
View File
@@ -4,12 +4,9 @@
import type { ToBn } from '../types';
import { BN } from './bn';
import { BN_MAX_INTEGER, BN_ONE } from './consts';
import { BN_MAX_INTEGER, BN_ONE, BN_SQRT_MAX_INTEGER } from './consts';
import { bnToBn } from './toBn';
/** @internal */
export const SQRT_MAX_SAFE_INTEGER = new BN(94906265);
/**
* @name bnSqrt
* @summary Calculates the integer square root of a BN
@@ -39,7 +36,7 @@ export function bnSqrt <ExtToBn extends ToBn> (value: ExtToBn | BN | bigint | st
// Use sqrt(MAX_SAFE_INTEGER) as starting point. since we already know the
// output will be larger than this, we expect this to be a safe start
let x0 = SQRT_MAX_SAFE_INTEGER.clone();
let x0 = BN_SQRT_MAX_INTEGER.clone();
while (true) {
const x1 = n.div(x0).iadd(x0).ishrn(1);