From 90c3b0ed90f111ece237898da15f77fcfffc6c13 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Sun, 8 Aug 2021 22:37:47 +0200 Subject: [PATCH] Fix math.tostring --- math.lua | 4 ++-- test.lua | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/math.lua b/math.lua index e3fa4d8..0e57dd2 100644 --- a/math.lua +++ b/math.lua @@ -41,7 +41,7 @@ function tostring(number, base, digit_function, precision) while number >= base do digit = math.floor(number % base) table.insert(out, digit_function(digit)) - number = number / base + number = (number - digit) / base end digit = math.floor(number) table.insert(out, digit_function(digit)) @@ -49,7 +49,7 @@ function tostring(number, base, digit_function, precision) number = number % 1 if number ~= 0 and number >= base ^ -precision then table.insert(out, ".") - while precision >= 0 and number >= base ^ precision do + while precision >= 0 and number >= base ^ -precision do number = number * base digit = math.floor(number % base) table.insert(out, digit_function(digit)) diff --git a/test.lua b/test.lua index 7e45f35..9f0fceb 100644 --- a/test.lua +++ b/test.lua @@ -16,6 +16,16 @@ setfenv(1, setmetatable({}, { 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 do local tab = {a = 1, b = 2}