Add math.sign

This commit is contained in:
Lars Mueller 2021-12-19 14:59:05 +01:00
parent e048fb3b03
commit 83c5c87390

@ -8,6 +8,7 @@ local inf = math.huge
local _ENV = {} local _ENV = {}
setfenv(1, _ENV) setfenv(1, _ENV)
-- TODO might be too invasive
-- Make random random -- Make random random
math.randomseed(minetest and minetest.get_us_time() or os.time() + os.clock()) math.randomseed(minetest and minetest.get_us_time() or os.time() + os.clock())
for _ = 1, 100 do math.random() end for _ = 1, 100 do math.random() end
@ -15,6 +16,13 @@ for _ = 1, 100 do math.random() end
negative_nan = 0/0 negative_nan = 0/0
positive_nan = negative_nan ^ 1 positive_nan = negative_nan ^ 1
function sign(number)
if number ~= number then return number end -- nan
if number == 0 then return 0 end
if number < 0 then return -1 end
if number > 0 then return 1 end
end
function round(number, steps) function round(number, steps)
steps = steps or 1 steps = steps or 1
return math_floor(number * steps + 0.5) / steps return math_floor(number * steps + 0.5) / steps