aboutsummaryrefslogtreecommitdiffstats
path: root/packages/math/utils.ts
blob: 8807c275e4d5bda3b8363b450fb864e0b50021b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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;