/flap_math

The math package for Flap

BSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

math

This is the math package for Flap, containing pure Flap implementations of common mathematical functions.

Functions

abs(x)

Returns the absolute value of a number.

Example:

abs(-5)   // -> 5
abs(42)   // -> 42

average(list)

Computes the mean of a list.

Example:

average([2, 3, 4])  // -> 3.0

clamp(x, min_val, max_val)

Restricts a value to a range [min_val, max_val].

Example:

clamp(5, 0, 10)   // -> 5
clamp(-5, 0, 10)  // -> 0
clamp(15, 0, 10)  // -> 10

distance(v1, v2)

Computes Euclidean distance between two vectors.

Example:

distance([0, 0], [3, 4])  // -> 5.0

dot(v1, v2)

Computes the dot product of two vectors (lists).

Example:

dot([1, 2, 3], [4, 5, 6])  // -> 32.0 (1*4 + 2*5 + 3*6)

magnitude(v)

Computes the length of a vector.

Example:

magnitude([3, 4])  // -> 5.0

max(x, y)

Returns the maximum of two numbers.

Example:

max(3, 5)  // -> 5

min(x, y)

Returns the minimum of two numbers.

Example:

min(3, 5)  // -> 3

normalize(v)

Returns a unit vector in the same direction.

Example:

normalize([3, 4])  // -> [0.6, 0.8]

product(list)

Multiplies all elements in a list using tail recursion.

Example:

product([2, 3, 4])  // -> 24

sign(x)

Returns the sign of a number (-1, 0, or 1).

Example:

sign(5)   // -> 1
sign(0)   // -> 0
sign(-3)  // -> -1

square(x)

Returns x * x.

Example:

square(5)  // -> 25

sum(list)

Adds all elements in a list using tail recursion.

Example:

sum([1, 2, 3, 4])  // -> 10

License

BSD-3