SPB Git

Repository Public

helpers.ts 1.5 KB 37 lines typescript

1import { D, type Dec, type DecStr } from '../src/index.ts';23/** mulberry32: a small deterministic PRNG for property tests (tests only; never used in the package itself) */4export function prng(seed: number): () => number {5  let a = seed >>> 0;6  return () => {7    a = (a + 0x6d2b79f5) >>> 0;8    let t = a;9    t = Math.imul(t ^ (t >>> 15), t | 1);10    t ^= t + Math.imul(t ^ (t >>> 7), t | 61);11    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;12  };13}1415export function randInt(r: () => number, lo: number, hi: number): number { return lo + Math.floor(r() * (hi - lo + 1)); }1617function digits(r: () => number, n: number): string {18  let s = '';19  for (let i = 0; i < n; i++) s += String(randInt(r, 0, 9));20  return s;21}2223/** canonical decimal string with up to `intDigits` integer digits and up to `fracDigits` decimals */24export function randDecStr(r: () => number, o: { intDigits?: number; fracDigits?: number; signed?: boolean; positive?: boolean } = {}): DecStr {25  const int = digits(r, randInt(r, 1, o.intDigits ?? 9)).replace(/^0+(?=\d)/, '');26  const fn = randInt(r, 0, o.fracDigits ?? 9);27  let s = fn === 0 ? int : `${int}.${digits(r, fn)}`;28  if (o.positive && D.isZero(D.from(s))) s = '1';29  if (o.signed && r() < 0.5) s = `-${s}`;30  return s;31}3233export function randDec(r: () => number, o: Parameters<typeof randDecStr>[1] = {}): Dec { return D.from(randDecStr(r, o)); }3435export const d = (x: DecStr | bigint): Dec => D.from(x);36export const s = (x: Dec): DecStr => D.str(x);37
Files main
Menu

Theme