This is the math package for Flap, containing pure Flap implementations of common mathematical functions.
Returns the absolute value of a number.
Example:
abs(-5) // -> 5
abs(42) // -> 42
Computes the mean of a list.
Example:
average([2, 3, 4]) // -> 3.0
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
Computes Euclidean distance between two vectors.
Example:
distance([0, 0], [3, 4]) // -> 5.0
Computes the dot product of two vectors (lists).
Example:
dot([1, 2, 3], [4, 5, 6]) // -> 32.0 (1*4 + 2*5 + 3*6)
Computes the length of a vector.
Example:
magnitude([3, 4]) // -> 5.0
Returns the maximum of two numbers.
Example:
max(3, 5) // -> 5
Returns the minimum of two numbers.
Example:
min(3, 5) // -> 3
Returns a unit vector in the same direction.
Example:
normalize([3, 4]) // -> [0.6, 0.8]
Multiplies all elements in a list using tail recursion.
Example:
product([2, 3, 4]) // -> 24
Returns the sign of a number (-1, 0, or 1).
Example:
sign(5) // -> 1
sign(0) // -> 0
sign(-3) // -> -1
Returns x * x.
Example:
square(5) // -> 25
Adds all elements in a list using tail recursion.
Example:
sum([1, 2, 3, 4]) // -> 10
BSD-3