2020-04-21 15:04:07 +02:00
|
|
|
-- Make random random
|
2021-03-04 12:18:26 +01:00
|
|
|
math.randomseed(minetest and minetest.get_us_time() or os.time() + os.clock())
|
2020-12-19 14:41:03 +01:00
|
|
|
for _ = 1, 100 do math.random() end
|
|
|
|
|
|
|
|
function round(number, steps)
|
|
|
|
steps = steps or 1
|
|
|
|
return math.floor(number * steps + 0.5) / steps
|
2020-02-09 01:39:54 +01:00
|
|
|
end
|
2020-12-19 14:41:03 +01:00
|
|
|
|
2020-03-23 20:20:43 +01:00
|
|
|
local c0 = ("0"):byte()
|
|
|
|
local cA = ("A"):byte()
|
2020-12-19 14:41:03 +01:00
|
|
|
|
2020-03-23 20:20:43 +01:00
|
|
|
function default_digit_function(digit)
|
2020-12-19 14:41:03 +01:00
|
|
|
if digit <= 9 then return string.char(c0 + digit) end
|
|
|
|
return string.char(cA + digit - 10)
|
2020-03-23 20:20:43 +01:00
|
|
|
end
|
2020-12-19 14:41:03 +01:00
|
|
|
|
2020-03-23 20:20:43 +01:00
|
|
|
default_precision = 10
|
2020-12-19 14:41:03 +01:00
|
|
|
|
|
|
|
-- See https://github.com/appgurueu/Luon/blob/master/index.js#L724
|
2020-03-23 20:20:43 +01:00
|
|
|
function tostring(number, base, digit_function, precision)
|
2020-12-19 14:41:03 +01:00
|
|
|
digit_function = digit_function or default_digit_function
|
|
|
|
precision = precision or default_precision
|
|
|
|
local out = {}
|
|
|
|
if number < 0 then
|
|
|
|
table.insert(out, "-")
|
|
|
|
number = -number
|
|
|
|
end
|
|
|
|
number = number + base ^ -precision / 2
|
|
|
|
local digit
|
|
|
|
while number >= base do
|
|
|
|
digit = math.floor(number % base)
|
|
|
|
table.insert(out, digit_function(digit))
|
|
|
|
number = number / base
|
|
|
|
end
|
|
|
|
digit = math.floor(number)
|
|
|
|
table.insert(out, digit_function(digit))
|
|
|
|
modlib.table.reverse(out)
|
|
|
|
number = number % 1
|
|
|
|
if number ~= 0 and number >= base ^ -precision then
|
|
|
|
table.insert(out, ".")
|
2020-12-19 15:31:37 +01:00
|
|
|
while precision >= 0 and number >= base ^ precision do
|
2020-12-19 14:41:03 +01:00
|
|
|
number = number * base
|
|
|
|
digit = math.floor(number % base)
|
|
|
|
table.insert(out, digit_function(digit))
|
|
|
|
number = number - digit
|
|
|
|
precision = precision - 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return table.concat(out)
|
2021-03-04 12:19:08 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround#polyfill
|
|
|
|
function fround(number)
|
|
|
|
if number == 0 or number ~= number then
|
|
|
|
return number
|
|
|
|
end
|
|
|
|
local sign = 1
|
|
|
|
if number < 0 then
|
|
|
|
sign = -1
|
|
|
|
number = -number
|
|
|
|
end
|
|
|
|
local exp = math.floor(math.log(number, 2))
|
|
|
|
local powexp = 2 ^ math.max(-126, math.min(number, 127))
|
|
|
|
local leading = exp < -127 and 0 or 1
|
|
|
|
local mantissa = math.floor((leading - number / powexp) * 0x800000 + 0.5)
|
|
|
|
if mantissa <= -0x800000 then
|
|
|
|
return sign * math.huge
|
|
|
|
end
|
|
|
|
mantissa = mantissa / 0x800000
|
|
|
|
return sign * powexp * (leading - mantissa), mantissa
|
2020-03-23 20:20:43 +01:00
|
|
|
end
|