math

Adds two values.

Type signature

(a: number, b: number) => number

Examples

add(3, 5);
// ⇒ 8
Try in REPL

Questions

  • How to add two values?

TypeScript source • JavaScript source

Calculates the average of the given array of numbers.

Type signature

(xs?: number[]) => number

Examples

average([2, 4, 15]);
// ⇒ 7
Try in REPL

Questions

  • How to compute the average of an array?

TypeScript source • JavaScript source

Finds the nearest power of two greater or equal to the given value.

Type signature

(x: number) => number

Examples

ceilToNearestPowerOfTwo(345);
// ⇒ 512
Try in REPL

Questions

  • How to get the nearest power of two greater or equal to the given value?

TypeScript source • JavaScript source

Clamps the given value to the given range.

Type signature

(min: number, max: number) => ((x: number) => number)

Examples

clamp(0, 10)(5);
// ⇒ 5
Try in REPL
clamp(0, 10)(-5);
// ⇒ 0
Try in REPL
clamp(0, 10)(15);
// ⇒ 10
Try in REPL

Questions

  • How to clamp value to the desired range?
  • How to enforce a value to be in a given range?

TypeScript source • JavaScript source

Clamps the given value to the [0, 1] range.

Type signature

(x: number) => number

Examples

clampNormal(0.5);
// ⇒ 0.5
Try in REPL
clampNormal(-0.5);
// ⇒ 0
Try in REPL
clampNormal(1.5);
// ⇒ 1
Try in REPL

Questions

  • How to clamp value to be in 0 to 1 inclusive range?
  • How to clamp value to be in the normal range?

TypeScript source • JavaScript source

Clamps the given value to the [0, 100] range.

Type signature

(x: number) => number

Examples

clampPercentage(50);
// ⇒ 50
Try in REPL
clampPercentage(-50);
// ⇒ 0
Try in REPL
clampPercentage(150);
// ⇒ 100
Try in REPL

Questions

  • How to enforce a percentage be between 0% and 100%?

TypeScript source • JavaScript source

Calculates the absolute distance between given values.

Type signature

(a: number, b: number) => number

Examples

delta(-3, 5);
// ⇒ 8
Try in REPL

Questions

  • How to calculate an absolute distance between two numbers?

TypeScript source • JavaScript source

Checks if the given value is in the rectangular range of [0, width] and [0, height]

Type signature

(width: number, height: number) => ((x: number, y: number) => boolean)

Examples

inRectangleRange(50, 100)(25, 50);
// ⇒ true
Try in REPL
inRectangleRange(50, 100)(-25, 50);
// ⇒ false
Try in REPL

Questions

  • How to check if a point is inside a rectangle defined by width and height?

TypeScript source • JavaScript source

Linearly interpolates two given values by the normal value of their distance.

Type signature

(t: number) => ((a: number, b: number) => number)

Examples

lerp(0.5)(0, 10);
// ⇒ 5
Try in REPL
lerp(0)(0, 10);
// ⇒ 0
Try in REPL
lerp(1)(0, 10);
// ⇒ 10
Try in REPL

Questions

  • How to linearly interpolate between two values?
  • How to interpolate two numbers?

TypeScript source • JavaScript source

Calculates the maximum by a given selector.

Type signature

(f: (x: number) => number) => ((xs: number[]) => number)

Examples

maximumBy(({ age }) => age)([
  { age: 13 },
  { age: 20 },
  { age: 7 },
  { age: 18 },
]);
// ⇒ { age: 20 }
Try in REPL

Questions

  • How to find a maximum element by a given function?

TypeScript source • JavaScript source

Calculates the median of the values. If there is an even number of items, the average of the middle ones is returned.

Type signature

(xs?: number[]) => number | undefined

Examples

median([-5, 3, 2, 29, 43]);
// ⇒ 3
Try in REPL

Questions

  • How to compute a median of an array?

TypeScript source • JavaScript source

Calculates the minimum and maximum value of the two given values.

Type signature

([a, b]: [number, number]) => [number, number]

Examples

minMax([5, 3]);
// ⇒ [3, 5]
Try in REPL
minMax([3, 5]);
// ⇒ [3, 5]
Try in REPL

Questions

  • How to get ordered values where the lower is the first and the higher is the second?

TypeScript source • JavaScript source

Checks if all the given values have the same sign.

Type signature

(xs: number[]) => boolean

Examples

sameSign([-1, -2, -3]);
// ⇒ true
Try in REPL
sameSign([1, 2, -3]);
// ⇒ false
Try in REPL

Questions

  • How to check if all values have the same sign?

TypeScript source • JavaScript source

Calculates the sign of the value and returns -1 for negative values, 1 for positive values and 0 for zeros.

Type signature

(x: number) => number

Examples

sign(3);
// ⇒ 1
Try in REPL
sign(-5);
// ⇒ 5
Try in REPL
sign(0);
// ⇒ 0
Try in REPL
sign(-0);
// ⇒ 0
Try in REPL

Questions

  • How to get a sign of a number?

TypeScript source • JavaScript source

Calculates the standard deviation of the given array of numbers.

Type signature

(xs: number[], origin?: number) => number

Examples

standardDeviation([
  96, 81, 68, 79, 23, 13, 13, 59,
  44, 86,
]);
// ⇒ (2 * Math.sqrt(10922 / 5)) / 3
Try in REPL

Questions

  • How to compute a standard deviation of an array?

TypeScript source • JavaScript source

Subtracts two values.

Type signature

(a: number, b: number) => number

Examples

subtract(3, 5);
// ⇒ -2
Try in REPL

Questions

  • How to subtract two numbers?

TypeScript source • JavaScript source