aboutsummaryrefslogtreecommitdiffstats
path: root/packages/math/utils.ts
diff options
context:
space:
mode:
authorkj_sh6042026-03-15 16:19:35 -0400
committerkj_sh6042026-03-15 16:19:35 -0400
commit6ec259a0e71174651bae95d4628138bf6fd68742 (patch)
tree5e33c6a5ec091ecabfcb257fdc7b6a88ed8754ac /packages/math/utils.ts
parent16c8578b15c727f22921f8a80a56ee4d4e7f2272 (diff)
refactor: packages/
Diffstat (limited to 'packages/math/utils.ts')
-rw-r--r--packages/math/utils.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/packages/math/utils.ts b/packages/math/utils.ts
new file mode 100644
index 0000000..8807c27
--- /dev/null
+++ b/packages/math/utils.ts
@@ -0,0 +1,33 @@
+export const PRECISION = 10e-5;
+
+export const clamp = (value: number, min: number, max: number) => {
+ return Math.min(Math.max(value, min), max);
+};
+
+export const round = (
+ value: number,
+ precision: number,
+ func: "round" | "floor" | "ceil" = "round",
+) => {
+ const multiplier = Math.pow(10, precision);
+
+ return Math[func]((value + Number.EPSILON) * multiplier) / multiplier;
+};
+
+export const roundToStep = (
+ value: number,
+ step: number,
+ func: "round" | "floor" | "ceil" = "round",
+): number => {
+ const factor = 1 / step;
+ return Math[func](value * factor) / factor;
+};
+
+export const average = (a: number, b: number) => (a + b) / 2;
+
+export const isFiniteNumber = (value: any): value is number => {
+ return typeof value === "number" && Number.isFinite(value);
+};
+
+export const isCloseTo = (a: number, b: number, precision = PRECISION) =>
+ Math.abs(a - b) < precision;