2016-02-04 18:24:39 +00:00
|
|
|
--[[--
|
|
|
|
Simple math helper functions
|
2016-12-25 20:05:48 +00:00
|
|
|
]]
|
2013-02-19 03:57:14 +00:00
|
|
|
|
2019-03-27 21:50:44 +00:00
|
|
|
local bit = require("bit")
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local Math = {}
|
|
|
|
|
2019-03-27 21:50:44 +00:00
|
|
|
local band = bit.band
|
|
|
|
|
2020-04-15 10:50:13 +00:00
|
|
|
function Math.roundPercent(percent)
|
|
|
|
return math.floor(percent * 10000) / 10000
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
function Math.roundAwayFromZero(num)
|
2014-03-13 13:52:43 +00:00
|
|
|
if num > 0 then
|
|
|
|
return math.ceil(num)
|
|
|
|
else
|
|
|
|
return math.floor(num)
|
|
|
|
end
|
2013-02-19 03:57:14 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
function Math.round(num)
|
2014-03-13 13:52:43 +00:00
|
|
|
return math.floor(num + 0.5)
|
2013-02-19 03:57:14 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
function Math.oddEven(number)
|
2019-03-27 21:50:44 +00:00
|
|
|
if band(number, 1) == 1 then
|
2014-03-13 13:52:43 +00:00
|
|
|
return "odd"
|
|
|
|
else
|
|
|
|
return "even"
|
|
|
|
end
|
2013-02-19 03:57:14 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local function tmin_max(tab, func, op)
|
2014-03-13 13:52:43 +00:00
|
|
|
if #tab == 0 then return nil, nil end
|
|
|
|
local index, value = 1, tab[1]
|
2013-02-21 05:16:47 +00:00
|
|
|
for i = 2, #tab do
|
2014-03-13 13:52:43 +00:00
|
|
|
if func then
|
|
|
|
if func(value, tab[i]) then
|
|
|
|
index, value = i, tab[i]
|
|
|
|
end
|
|
|
|
elseif op == "min" then
|
|
|
|
if value > tab[i] then
|
|
|
|
index, value = i, tab[i]
|
|
|
|
end
|
|
|
|
elseif op == "max" then
|
|
|
|
if value < tab[i] then
|
|
|
|
index, value = i, tab[i]
|
|
|
|
end
|
|
|
|
end
|
2013-02-21 05:16:47 +00:00
|
|
|
end
|
|
|
|
return index, value
|
|
|
|
end
|
|
|
|
|
2016-12-13 16:06:02 +00:00
|
|
|
--[[--
|
|
|
|
Returns the minimum element of a table.
|
2013-02-21 05:16:47 +00:00
|
|
|
The optional argument func specifies a one-argument ordering function.
|
2016-12-13 16:06:02 +00:00
|
|
|
|
2016-12-25 20:05:48 +00:00
|
|
|
@tparam table tab
|
|
|
|
@tparam func func
|
|
|
|
@treturn dynamic minimum element of a table
|
|
|
|
]]
|
2013-10-18 20:38:07 +00:00
|
|
|
function Math.tmin(tab, func)
|
2014-03-13 13:52:43 +00:00
|
|
|
return tmin_max(tab, func, "min")
|
2013-02-21 05:16:47 +00:00
|
|
|
end
|
2013-02-19 03:57:14 +00:00
|
|
|
|
2016-12-13 16:06:02 +00:00
|
|
|
--[[--
|
|
|
|
Returns the maximum element of a table.
|
2013-02-21 05:16:47 +00:00
|
|
|
The optional argument func specifies a one-argument ordering function.
|
2016-12-13 16:06:02 +00:00
|
|
|
|
2016-12-25 20:05:48 +00:00
|
|
|
@tparam table tab
|
|
|
|
@tparam func func
|
|
|
|
@treturn dynamic maximum element of a table
|
|
|
|
]]
|
2013-10-18 20:38:07 +00:00
|
|
|
function Math.tmax(tab, func)
|
2014-03-13 13:52:43 +00:00
|
|
|
return tmin_max(tab, func, "max")
|
2013-02-21 05:16:47 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return Math
|