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
23
number.lua
23
number.lua
@ -1,22 +1,23 @@
|
|||||||
-- 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
|
|
||||||
function round(number, steps) -- Rounds a number
|
|
||||||
steps = steps or 1
|
steps = steps or 1
|
||||||
return math.floor(number * steps + 0.5) / steps
|
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
|
||||||
@ -25,6 +26,7 @@ function tostring(number, base, digit_function, precision)
|
|||||||
table.insert(out, "-")
|
table.insert(out, "-")
|
||||||
number = -number
|
number = -number
|
||||||
end
|
end
|
||||||
|
number = number + base ^ -precision / 2
|
||||||
local digit
|
local digit
|
||||||
while number >= base do
|
while number >= base do
|
||||||
digit = math.floor(number % base)
|
digit = math.floor(number % base)
|
||||||
@ -35,9 +37,8 @@ function tostring(number, base, digit_function, precision)
|
|||||||
table.insert(out, digit_function(digit))
|
table.insert(out, digit_function(digit))
|
||||||
modlib.table.reverse(out)
|
modlib.table.reverse(out)
|
||||||
number = number % 1
|
number = number % 1
|
||||||
if number >= math.pow(base, precision) then
|
if number ~= 0 and number >= base ^ -precision then
|
||||||
table.insert(out, ".")
|
table.insert(out, ".")
|
||||||
-- precision >= 0 eventually redundant
|
|
||||||
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user