mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 01:03:46 +01:00
number.lua cleanup, tostring fixes
This commit is contained in:
parent
0579840908
commit
3a5b436e11
81
number.lua
81
number.lua
@ -1,50 +1,51 @@
|
|||||||
-- Make random random
|
-- Make random random
|
||||||
math.randomseed(minetest.get_us_time())
|
math.randomseed(minetest.get_us_time())
|
||||||
for i = 1, 100 do
|
for _ = 1, 100 do math.random() end
|
||||||
math.random()
|
|
||||||
end
|
function round(number, steps)
|
||||||
-- Number helpers
|
steps = steps or 1
|
||||||
function round(number, steps) -- Rounds a number
|
return math.floor(number * steps + 0.5) / steps
|
||||||
steps = steps or 1
|
|
||||||
return math.floor(number * steps + 0.5) / steps
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local c0 = ("0"):byte()
|
local c0 = ("0"):byte()
|
||||||
local cA = ("A"):byte()
|
local cA = ("A"):byte()
|
||||||
|
|
||||||
function default_digit_function(digit)
|
function default_digit_function(digit)
|
||||||
if digit <= 9 then
|
if digit <= 9 then return string.char(c0 + digit) end
|
||||||
return string.char(c0+digit)
|
return string.char(cA + digit - 10)
|
||||||
end
|
|
||||||
return string.char(cA+digit-10)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
default_precision = 10
|
default_precision = 10
|
||||||
|
|
||||||
|
-- See https://github.com/appgurueu/Luon/blob/master/index.js#L724
|
||||||
function tostring(number, base, digit_function, precision)
|
function tostring(number, base, digit_function, precision)
|
||||||
digit_function = digit_function or default_digit_function
|
digit_function = digit_function or default_digit_function
|
||||||
precision = precision or default_precision
|
precision = precision or default_precision
|
||||||
local out = {}
|
local out = {}
|
||||||
if number < 0 then
|
if number < 0 then
|
||||||
table.insert(out, "-")
|
table.insert(out, "-")
|
||||||
number = -number
|
number = -number
|
||||||
end
|
end
|
||||||
local digit
|
number = number + base ^ -precision / 2
|
||||||
while number >= base do
|
local digit
|
||||||
digit = math.floor(number % base)
|
while number >= base do
|
||||||
table.insert(out, digit_function(digit))
|
digit = math.floor(number % base)
|
||||||
number = number / base
|
table.insert(out, digit_function(digit))
|
||||||
end
|
number = number / base
|
||||||
digit = math.floor(number)
|
end
|
||||||
table.insert(out, digit_function(digit))
|
digit = math.floor(number)
|
||||||
modlib.table.reverse(out)
|
table.insert(out, digit_function(digit))
|
||||||
number = number % 1
|
modlib.table.reverse(out)
|
||||||
if number >= math.pow(base, precision) then
|
number = number % 1
|
||||||
table.insert(out, ".")
|
if number ~= 0 and number >= base ^ -precision then
|
||||||
-- precision >= 0 eventually redundant
|
table.insert(out, ".")
|
||||||
while precision >= 0 and number >= math.pow(base, precision) do
|
while precision >= 0 and number >= math.pow(base, precision) do
|
||||||
number = number * base
|
number = number * base
|
||||||
digit = math.floor(number % base)
|
digit = math.floor(number % base)
|
||||||
table.insert(out, digit_function(digit))
|
table.insert(out, digit_function(digit))
|
||||||
number = number - digit
|
number = number - digit
|
||||||
precision = precision - 1
|
precision = precision - 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return table.concat(out)
|
return table.concat(out)
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user