meows
Version:
A kittybin of tools.
113 lines (111 loc) • 4.22 kB
TypeScript
export declare type fraction = [number, number];
/**
* A suite of random tools.
*/
export declare class Rand {
static int: (min?: number, max?: number) => number;
static real: (min?: number, max?: number) => number;
/** Gives a weak random boolean. */
static wBool: () => boolean;
/**
* Given natural `n` → returns `n` strong random booleans.
* @example
* Rand.bool() ? 'heads' : 'tails'
* Rand.bool(10).filter(x => x)
*/
static bool(nat?: number): boolean | boolean[];
}
/** Generate an array of `n` strong random integers. */
export declare const randomInts: (n?: number) => Uint8Array | Int8Array | Int16Array | Int32Array | Uint16Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | DataView;
export declare const sum: (nums: number[]) => number;
export declare const product: (nums: number[]) => number;
/**
* Finds the difference in magnitude between two numbers.
* @example
* diff_abs(10, -3) // => 13
* diff_abs(-5, 5) // => 10
*/
export declare const diff_abs: (a: number, b: number) => number;
/**
* Finds the delta of unidimensional array of reals.
* @example
* diff_nums([0, 0, 1, 3, 6, 10, 15]) // => [0, 1, 2, 3, 4, 5]
*/
export declare const diff_nums: (nums: number[]) => any[];
/**
* Converts an integer to an array of its digits.
* @example
* int_to_digits(123450) // => [1, 2, 3, 4, 5, 0]
* int_to_digits(1/0) // => []
*/
export declare const int_to_digits: (int: number, base?: number) => number[];
/**
* Randomly signs a signable number. If the value isn't signable, return `NaN`.
* @example
* const backoff = (tries) => 2**tries + plus_or_minus(Math.random())
*/
export declare const plus_or_minus: (num: number) => number;
/** Check if strictly equal to `0`. */
export declare const isZero: (num: any) => boolean;
/** Floor division that returns `NaN` for non-number inputs. */
export declare const fdiv: (num: number, div: number) => number;
/**
* Returns the result of a division operation in the form of an array, with the
* first item as the whole quotient, and the second item as the remainder.
*
* @example
* divmod(13, 2) // => [6, 1]
* divmod(12, 2) // => [6, 0]
* divmod(12.5, 2) // => [6, 0.5]
*/
export declare const divmod: (num: number, div: number) => [number, number];
/**
* Takes a decimal number and separates its whole part from its fractional part.
*
* @example
* splitDecimal(3) // => [3, 0]
* splitDecimal(3.5) // => [3, 0.5]
* splitDecimal(0.5) // => [0, 0.5]
* splitDecimal(0) // => [0, 0]
* splitDecimal(null) // => [NaN, NaN]
*/
export declare function splitDecimal(real: number): [number, number];
/** Replacement for `Math.floor`. */
export declare const floor: (x: number) => number;
/** Changes a signable number into negative. */
export declare const negative: (num: number) => number;
/** Changes a signable number into positive. */
export declare const positive: (num: number) => number;
/**
* Flips the sign on a signable number, but excludes `null`.
* @example
* flipSign(0) // => -0
* flipSign(-0) // => 0
* flipSign(1/0) // => -Infinity
* flipSign(NaN) // => NaN
* flipSign(null) // => NaN
*/
export declare const flipSign: (num: number) => number;
/**
* Takes target real number and a maximum denominator for precision and finds
* the simplest and closest fraction up to that target real in the form of an
* array: `[numerator, divisor]`.
* @example
* realToFraction(0.23, 10) // => [2, 9]
* realToFraction(0.31415, 100) // => [11, 35]
* realToFraction(NaN, NaN) // => [NaN, NaN]
*/
export declare function realToFraction(real: number, denMax: number): fraction;
/**
* Simplifies a fraction by recursively looking for greatest common divisor
* ("gcd") of the numerator and divisor (denominator). Returns a fraction in the
* form of an array, with the numerator being the first item, and the divisor
* being the second.
*
* @example
* simplifyFraction(6/3) // => [2, 1]
* simplifyFraction(3/6) // => [1, 2]
* simplifyFraction(1/2) // => [1, 2]
* simplifyFraction(1/0) // => [NaN, NaN]
*/
export declare function simplifyFraction(num: number, div: number): fraction;