mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 17:23:45 +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
|
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))
|
||||||
|
10
test.lua
10
test.lua
@ -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}
|
||||||
|
Loading…
Reference in New Issue
Block a user