mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-22 15:23:48 +01:00
Fix math.tostring
This commit is contained in:
parent
5fb4145a49
commit
90c3b0ed90
4
math.lua
4
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))
|
||||
|
10
test.lua
10
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}
|
||||
|
Loading…
Reference in New Issue
Block a user