Skip to content

Commit

Permalink
Add small math function library
Browse files Browse the repository at this point in the history
  • Loading branch information
nicfv committed Mar 5, 2024
1 parent fc7230f commit 3cca950
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions smath/src/SMath.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 3cca950

Please sign in to comment.