Fix math.tostring

This commit is contained in:
Lars Mueller 2021-08-08 22:37:47 +02:00
parent 5fb4145a49
commit 90c3b0ed90
2 changed files with 12 additions and 2 deletions

@ -41,7 +41,7 @@ function tostring(number, base, digit_function, precision)
while number >= base do while number >= base do
digit = math.floor(number % base) digit = math.floor(number % base)
table.insert(out, digit_function(digit)) table.insert(out, digit_function(digit))
number = number / base number = (number - digit) / base
end end
digit = math.floor(number) digit = math.floor(number)
table.insert(out, digit_function(digit)) table.insert(out, digit_function(digit))
@ -49,7 +49,7 @@ function tostring(number, base, digit_function, precision)
number = number % 1 number = number % 1
if number ~= 0 and number >= base ^ -precision then if number ~= 0 and number >= base ^ -precision then
table.insert(out, ".") table.insert(out, ".")
while precision >= 0 and number >= base ^ precision do while precision >= 0 and number >= 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))

@ -16,6 +16,16 @@ setfenv(1, setmetatable({}, {
end end
})) }))
-- math
do
local function assert_tonumber(num, base)
local str = modlib.math.tostring(num, base)
assert(tonumber(str, base) == num, str)
end
assert_tonumber(134217503, 36)
assert_tonumber(3.14, 10)
end
-- func -- func
do do
local tab = {a = 1, b = 2} local tab = {a = 1, b = 2}