LibMath
LibMath is a collection of math functions that extends the Lua math library significantly.
Functions
math.avg (...
)
Returns the average (mean value) of the numbers passed.
- Usage:
local avg = math.avg(1,2,3,4,5) assert(avg == 3) -- (1+2+3+4+5)/5 = 3
math.ceil (number[, significance]
)
Rounds a number up to the nearest integer value or the nearest multiple of significance.
- Usage:
local rounded = math.ceil(4.51) assert(rounded == 5) local minutes = math.ceil(110, 60) assert(minutes == 2)
math.count (...
)
Returns the total number of values passed
- Usage:
local count = math.count(1,2,3,4,5,6) assert(count == 6)
math.countif (statement, ...
)
Returns the total number of numbers passed that fits the statement (fx. ">= 4")
- Usage:
local goodEggs = math.countif("<=4", 1,2,3,4,5,6) assert(goodEggs == 4)
math.floor (number[, significance]
)
Rounds a number down (towards zero) to the nearest integer or multiple of significance.
- Usage:
local rounded = math.floor(4.51) assert(rounded == 4) local minutes = math.floor(110, 60) assert(minutes == 1)
math.large (k, ...
)
Returns the k<sub>th</sub> largest value among the numbers passed
- Usage:
-- Looking for the 3rd largest number. local largest = math.large(3, 7,2,3,6,4,9,5) assert(largest == 6)
math.log (num[, base]
)
Returns the logarithm of the specified number to either e
or the specified base number.
- Usage:
local result = math.log(20, 10) assert(result == math.log10(20))
math.matrix (x, y, {a,b,c,d,e,f}
)
Performs a 2-by-3 transformation in 3x3 matrices on the coordinates passed so that:
x<sub>1</sub> = [a c e] x
y<sub>1</sub> = [b d f] y
1<sub>1</sub> = [0 0 1] 1
- Parameters:
- x
- x-coordinate
- y
- y-coordinate
- {a,b,c,d,e,f}
- a table containing the two transformation vectors (a,b,c,d,e,f)
- Usage:
local x,y = math.matrix(5,10, {1,0,5,0,1,0}) assert(x == 56 and y == 0)
math.median (...
)
Returns the median (the number in the middle of all the numbers sorted numerically)
- Usage:
local med = math.median(1,5,2,4,3) assert(med == 3)
math.mode (...
)
Returns the most frequently occurring number among those passed.
- Usage:
local mode = math.mode(1,2,1,3,1,4) assert(mode == 1)
math.real(num
)
Returns true if the passed number is a real number, false otherwise.
- Usage:
local good, bad, ugly = 5, 1/0, 0/0 assert(math.real(good) == true) assert(math.real(bad) == false) assert(math.real(ugly) == false)
math.rotate (x, y, a[, cx, cy]
)
Performs a rotation of a
degrees of the coordinates passed by either 0,0 or cx,cy
- Parameters:
- x
- x-coordinate
- y
- y-coordinate
- a
- angle of rotation (in radians)
- cx
- (optional) x-coordinate of rotation point
- cy
- (optional) y-coordinate of rotation point
- Usage:
local x,y = math.rotate(10, 5, math.rad(90)) assert(x == -5 and y == 10) local x,y = math.rotate(10, 5, math.rad(90), 5,0) assert(x == 0 and y == 5)
math.round (number[, significance]
)
Rounds the number to the nearest integer value or multiple of significance.
- Usage:
local rounded = math.round(4.51) assert(rounded == 5) local minutes = math.round(110, 60) assert(minutes == 2)
math.sign (val
)
Returns -1 for numbers less than zero, +1 for numbers greater than zero and 0 for zero.
- Usage:
local sign = math.sign(math.pi/-3000) assert(sign == -1)
math.skewx (x, y, a
)
Skews the coordinates passed horizontally by a
degrees
- Usage:
local x,y = math.skewx(10, 5, math.rad(45)) assert(x == 15 and y == 5)
math.skewy (x, y, a
)
Skews the coordinates passed vertically by a
degrees
- Usage:
local x,y = math.skewx(10, 5, math.rad(45)) assert(x == 10 and y == 15)
math.small (k, ...
)
Returns the k<sub>th</sub> smallest value among the numbers passed
- Usage:
-- Looking for the 2nd smallest number. local smallest = math.small(2, 5,3,6,2,8,1) assert(smallest == 2)
math.stddev (...
)
Returns the standard deviation for the population passed.
- Usage:
local std = math.stddev(10,20,30) assert(std == 10)
math.sum (...
)
Returns the sum of all numbers passed
- Usage:
local sum = math.sum(1,2,3,4) assert(sum == 10) -- 1+2+3+4 = 10
math.sumif (statement, ...
)
Returns the sum of all numbers that passes the statement (fx. ">25")
- Usage:
local sum = math.sumif(">2.5", 1,2,3,4,5,6,7) assert(sum == 25)
math.unique (...
)
Returns a list of all the unique numbers passed.
- Usage:
local uniques = {math.unique(1,2,3,2,1,4,5,2,3)} -- yields {4,5}
Comments