-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nicfv
committed
Mar 5, 2024
1 parent
fc7230f
commit 3cca950
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |