Test for sqrt(MAX_SAFE_INT) (w/ comments for excluded functions) (#1526)
* Test for sqrt(MAX_SAFE_INT) (w/ comments for excluded functions)
* Adjust {bi, bn}{Max, Min} with factory
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* @name arrayShuffle
|
||||
* @description Shuffles the input array (unlike sort, this is not done in-place)
|
||||
*/
|
||||
export function arrayShuffle <T> (input: T[]): T[] {
|
||||
const result = input.slice();
|
||||
let curr = result.length;
|
||||
|
||||
+15
-14
@@ -3,40 +3,41 @@
|
||||
|
||||
import { assert } from '../assert';
|
||||
|
||||
/** @internal */
|
||||
function gt (a: bigint, b: bigint): boolean {
|
||||
return a > b;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function lt (a: bigint, b: bigint): boolean {
|
||||
return a < b;
|
||||
}
|
||||
|
||||
function find (items: bigint[], cmp: (a: bigint, b: bigint) => boolean): bigint {
|
||||
assert(items.length >= 1, 'Must provide one or more bigint arguments');
|
||||
/** @internal */
|
||||
function createCmp (cmp: (a: bigint, b: bigint) => boolean): (...items: bigint[]) => bigint {
|
||||
return (...items: bigint[]): bigint => {
|
||||
assert(items.length >= 1, 'Must provide one or more bigint arguments');
|
||||
|
||||
let result = items[0];
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
if (cmp(items[i], result)) {
|
||||
result = items[i];
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
if (cmp(items[i], result)) {
|
||||
result = items[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @name nMax
|
||||
* @summary Finds and returns the highest value in an array of bigint.
|
||||
*/
|
||||
export function nMax (...items: bigint[]): bigint {
|
||||
return find(items, gt);
|
||||
}
|
||||
export const nMax = createCmp(gt);
|
||||
|
||||
/**
|
||||
* @name nMin
|
||||
* @summary Finds and returns the lowest value in an array of bigint.
|
||||
*/
|
||||
export function nMin (...items: bigint[]): bigint {
|
||||
return find(items, lt);
|
||||
}
|
||||
export const nMin = createCmp(lt);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { BN } from '../bn';
|
||||
import { SQRT_MAX_SAFE_INTEGER } from './sqrt';
|
||||
import { nSqrt } from '.';
|
||||
|
||||
const TESTS: [string | number | BN | bigint, string | number][] = [
|
||||
@@ -34,9 +35,21 @@ describe('nSqrt', (): void => {
|
||||
).toThrow(/negative numbers is not supported/);
|
||||
});
|
||||
|
||||
it('has the correct constant for sqrt(Number.MAX_SAFE_INTEGER)', (): void => {
|
||||
expect(
|
||||
BigInt(
|
||||
~~Math.sqrt(
|
||||
Number.MAX_SAFE_INTEGER
|
||||
)
|
||||
) === SQRT_MAX_SAFE_INTEGER
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
TESTS.forEach(([value, expected], index): void => {
|
||||
it(`calcs for test #${index}`, (): void => {
|
||||
expect(nSqrt(value) === BigInt(expected)).toEqual(true);
|
||||
expect(
|
||||
nSqrt(value) === BigInt(expected)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -10,7 +10,8 @@ import { assert } from '../assert';
|
||||
import { _0n, _1n, _2pow53n } from './consts';
|
||||
import { nToBigInt } from './toBigInt';
|
||||
|
||||
const _sqrt2pow53n = BigInt(94906265);
|
||||
/** @internal */
|
||||
export const SQRT_MAX_SAFE_INTEGER = BigInt(94906265);
|
||||
|
||||
/**
|
||||
* @name nSqrt
|
||||
@@ -30,7 +31,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 = _sqrt2pow53n;
|
||||
let x0 = SQRT_MAX_SAFE_INTEGER;
|
||||
|
||||
while (true) {
|
||||
const x1 = ((n / x0) + x0) >> _1n;
|
||||
|
||||
@@ -8,6 +8,7 @@ import { objectSpread } from '../object/spread';
|
||||
import { u8aToHex } from '../u8a';
|
||||
import { nToU8a } from './toU8a';
|
||||
|
||||
/** @internal */
|
||||
const ZERO_STR = '0x00';
|
||||
|
||||
/**
|
||||
|
||||
+12
-13
@@ -4,16 +4,19 @@
|
||||
import { assert } from '../assert';
|
||||
import { BN } from './bn';
|
||||
|
||||
export function find (items: BN[], cmp: (a: BN, b: BN) => BN): BN {
|
||||
assert(items.length >= 1, 'Must provide one or more BN arguments');
|
||||
/** @internal */
|
||||
function createCmp (cmp: (a: BN, b: BN) => BN): (...items: BN[]) => BN {
|
||||
return (...items: BN[]): BN => {
|
||||
assert(items.length >= 1, 'Must provide one or more BN arguments');
|
||||
|
||||
let result = items[0];
|
||||
let result = items[0];
|
||||
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
result = cmp(result, items[i]);
|
||||
}
|
||||
for (let i = 1; i < items.length; i++) {
|
||||
result = cmp(result, items[i]);
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,9 +32,7 @@ export function find (items: BN[], cmp: (a: BN, b: BN) => BN): BN {
|
||||
* bnMax([new BN(1), new BN(3), new BN(2)]).toString(); // => '3'
|
||||
* ```
|
||||
*/
|
||||
export function bnMax (...items: BN[]): BN {
|
||||
return find(items, BN.max);
|
||||
}
|
||||
export const bnMax = createCmp(BN.max);
|
||||
|
||||
/**
|
||||
* @name bnMin
|
||||
@@ -46,6 +47,4 @@ export function bnMax (...items: BN[]): BN {
|
||||
* bnMin([new BN(1), new BN(3), new BN(2)]).toString(); // => '1'
|
||||
* ```
|
||||
*/
|
||||
export function bnMin (...items: BN[]): BN {
|
||||
return find(items, BN.min);
|
||||
}
|
||||
export const bnMin = createCmp(BN.min);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { SQRT_MAX_SAFE_INTEGER } from './sqrt';
|
||||
import { BN, bnSqrt } from '.';
|
||||
|
||||
const TESTS: [string | number | BN | bigint, string | number][] = [
|
||||
@@ -33,9 +34,23 @@ describe('bnSqrt', (): void => {
|
||||
).toThrow(/negative numbers is not supported/);
|
||||
});
|
||||
|
||||
it('has the correct constant for sqrt(Number.MAX_SAFE_INTEGER)', (): void => {
|
||||
expect(
|
||||
SQRT_MAX_SAFE_INTEGER.eq(
|
||||
new BN(
|
||||
~~Math.sqrt(
|
||||
Number.MAX_SAFE_INTEGER
|
||||
)
|
||||
)
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
TESTS.forEach(([value, expected], index): void => {
|
||||
it(`calcs for test #${index}`, (): void => {
|
||||
expect(bnSqrt(value).eq(new BN(expected))).toEqual(true);
|
||||
expect(
|
||||
bnSqrt(value).eq(new BN(expected))
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -8,7 +8,8 @@ import { BN } from './bn';
|
||||
import { BN_MAX_INTEGER, BN_ONE, BN_ZERO } from './consts';
|
||||
import { bnToBn } from './toBn';
|
||||
|
||||
const SQRT_MAX_SAFE_INTEGER = new BN(94906265);
|
||||
/** @internal */
|
||||
export const SQRT_MAX_SAFE_INTEGER = new BN(94906265);
|
||||
|
||||
/**
|
||||
* @name bnSqrt
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
|
||||
import type { BitLength } from './types';
|
||||
|
||||
/** @internal */
|
||||
export const DEFAULT_BITLENGTH: BitLength = 32;
|
||||
|
||||
@@ -6,6 +6,10 @@ function zeroPad (value: number): string {
|
||||
return value.toString().padStart(2, '0');
|
||||
}
|
||||
|
||||
/**
|
||||
* @name formatDate
|
||||
* @description Formats a date in CCYY-MM-DD HH:MM:SS format
|
||||
*/
|
||||
export function formatDate (date: Date): string {
|
||||
const year = date.getFullYear().toString();
|
||||
const month = zeroPad((date.getMonth() + 1));
|
||||
|
||||
@@ -4,6 +4,10 @@
|
||||
// eslint-disable-next-line prefer-regex-literals
|
||||
const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g');
|
||||
|
||||
/**
|
||||
* @name formatDecimal
|
||||
* @description Formats a number into string format with thousand seperators
|
||||
*/
|
||||
export function formatDecimal (value: string): string {
|
||||
// We can do this by adjusting the regx, however for the sake of clarity
|
||||
// we rather strip and re-add the negative sign in the output
|
||||
|
||||
@@ -6,6 +6,7 @@ import type { ToBn } from '../types';
|
||||
|
||||
import { bnToBn } from '../bn/toBn';
|
||||
|
||||
/** @internal */
|
||||
function formatValue (elapsed: number): string {
|
||||
if (elapsed < 15) {
|
||||
return `${elapsed.toFixed(1)}s`;
|
||||
@@ -18,6 +19,10 @@ function formatValue (elapsed: number): string {
|
||||
return `${elapsed / 3600 | 0}h`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name formatElapsed
|
||||
* @description Formats an elapsed value into s, m, h or day segments
|
||||
*/
|
||||
export function formatElapsed <ExtToBn extends ToBn> (now?: Date | null, value?: bigint | BN | ExtToBn | Date | number | null): string {
|
||||
const tsNow = (now && now.getTime()) || 0;
|
||||
const tsValue = value instanceof Date
|
||||
|
||||
@@ -7,6 +7,10 @@ import type { ToBn } from '../types';
|
||||
import { bnToBn } from '../bn/toBn';
|
||||
import { formatDecimal } from './formatDecimal';
|
||||
|
||||
/**
|
||||
* @name formatNumber
|
||||
* @description Formats a number into string format with thousand seperators
|
||||
*/
|
||||
export function formatNumber <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null): string {
|
||||
return formatDecimal(bnToBn(value).toString());
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
|
||||
import type { SiDef } from '../types';
|
||||
|
||||
/** @internal */
|
||||
export const SI_MID = 8;
|
||||
|
||||
/** @internal */
|
||||
export const SI: SiDef[] = [
|
||||
{ power: -24, text: 'yocto', value: 'y' },
|
||||
{ power: -21, text: 'zepto', value: 'z' },
|
||||
@@ -26,6 +28,7 @@ export const SI: SiDef[] = [
|
||||
];
|
||||
|
||||
// Given a SI type (e.g. k, m, Y) find the SI definition
|
||||
/** @internal */
|
||||
export function findSi (type: string): SiDef {
|
||||
// use a loop here, better RN support (which doesn't have [].find)
|
||||
for (let i = 0; i < SI.length; i++) {
|
||||
@@ -37,6 +40,7 @@ export function findSi (type: string): SiDef {
|
||||
return SI[SI_MID];
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function calcSi (text: string, decimals: number, forceUnit?: string): SiDef {
|
||||
if (forceUnit) {
|
||||
return findSi(forceUnit);
|
||||
|
||||
@@ -17,6 +17,7 @@ function isAsciiByte (b: number): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function isAsciiChar (s: string): boolean {
|
||||
return isAsciiByte(s.charCodeAt(0));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user