Minetest-WorldEditAdditions/worldeditadditions/utils/numbers.lua

65 lines
1.9 KiB
Lua
Raw Normal View History

2020-05-11 01:02:02 +00:00
-- From http://lua-users.org/wiki/SimpleRound
2020-05-11 23:38:42 +00:00
function worldeditadditions.round(num, numDecimalPlaces)
2021-02-26 04:22:39 +00:00
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
2020-05-11 01:02:02 +00:00
end
function worldeditadditions.hypotenuse(x1, y1, x2, y2)
local xSquare = math.pow(x1 - x2, 2);
local ySquare = math.pow(y1 - y2, 2);
return math.sqrt(xSquare + ySquare);
end
function worldeditadditions.sum(list)
2021-02-26 04:22:39 +00:00
if #list == 0 then return 0 end
local sum = 0
for i,value in ipairs(list) do
sum = sum + value
end
2021-02-26 04:22:39 +00:00
return sum
end
function worldeditadditions.average(list)
2021-02-26 04:22:39 +00:00
if #list == 0 then return 0 end
return worldeditadditions.sum(list) / #list
end
--- Returns the minetest.get_us_time() in ms
-- @return float
function worldeditadditions.get_ms_time()
return minetest.get_us_time() / 1000
end
2021-02-07 02:45:34 +00:00
function worldeditadditions.eta(existing_times, done_count, total_count)
2021-02-26 04:22:39 +00:00
local max = 100
local average = worldeditadditions.average(
worldeditadditions.table_get_last(existing_times, max)
)
local times_left = total_count - done_count
if times_left == 0 then return 0 end
return average * times_left
end
--- Returns the sign (+ or -) at the beginning of a string if present.
-- @param str string Input string.
-- @param type string The type of value to return. Valid values: "string" (default), "int"
-- @return string|int Returns the sign string or signed multiplier (1|-1).
function worldeditadditions.getsign(str, type)
if not type then type = "string" end
if not (type == "string" or type == "int") then
return false, "Error: Unknown type '"..type.."'."
end
if str:sub(1, 1) == "-" then
if type == "int" then return true, -1
else return true, "-" end
2021-02-26 04:22:39 +00:00
else
if type == "int" then return true, 1
else return true, "+" end
2021-02-26 04:22:39 +00:00
end
end
2021-02-28 17:59:09 +00:00
-- For Testing:
-- worldeditadditions = {}
-- print(worldeditadditions.getsign('-y'))