From 3cca95028d014efc0205ff8adc6dbd5493d3796b Mon Sep 17 00:00:00 2001 From: nicfv Date: Tue, 5 Mar 2024 08:27:18 -0800 Subject: [PATCH] Add small math function library --- smath/src/SMath.ts | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 smath/src/SMath.ts diff --git a/smath/src/SMath.ts b/smath/src/SMath.ts new file mode 100644 index 00000000..b2cc1130 --- /dev/null +++ b/smath/src/SMath.ts @@ -0,0 +1,39 @@ +/** + * Small Math function library + */ +export class SMath { + /** + * Determine if a value is numeric. + * @param n Any value to check + * @returns True if `n` is a number + */ + public static isNumber(n: any): boolean { + return typeof n === 'number'; + } + /** + * Clamp a number within a range. + * @param n The number to clamp + * @param min The minimum value of the range + * @param max The maximum value of the range + * @returns A clamped number + */ + public static clamp(n: number, min: number, max: number): number { + if (n < min) { + return min; + } + if (n > max) { + return max; + } + return n; + } + /** + * Check if two numbers are approximately equal with a maximum abolute error. + * @param a Any number + * @param b Any number + * @param epsilon Maximum absolute error + * @returns True if `a` is approximately `b` + */ + public static approx(a: number, b: number, epsilon: number = 1e-6): boolean { + return a - b < epsilon && b - a < epsilon; + } +} \ No newline at end of file