More precision for numbers

Thanks to cohadar
https://love2d.org/forums/viewtopic.php?f=4&t=80157&start=10#p183885
This commit is contained in:
Robin Wellner 2015-05-08 10:43:06 +02:00
parent 8cf0f0df6e
commit 4fa4b2132a

View File

@ -1,4 +1,4 @@
local pairs, ipairs, tostring, type, concat, dump, floor = pairs, ipairs, tostring, type, table.concat, string.dump, math.floor local pairs, ipairs, tostring, type, concat, dump, floor, format = pairs, ipairs, tostring, type, table.concat, string.dump, math.floor, string.format
local function getchr(c) local function getchr(c)
return "\\" .. c:byte() return "\\" .. c:byte()
@ -11,9 +11,12 @@ end
local oddvals = {[tostring(1/0)] = '1/0', [tostring(-1/0)] = '-1/0', [tostring(0/0)] = '0/0'} local oddvals = {[tostring(1/0)] = '1/0', [tostring(-1/0)] = '-1/0', [tostring(0/0)] = '0/0'}
local function write(t, memo, rev_memo) local function write(t, memo, rev_memo)
local ty = type(t) local ty = type(t)
if ty == 'number' or ty == 'boolean' or ty == 'nil' then if ty == 'number' then
t = tostring(t) t = format("%.17g", t)
return oddvals[t] or t return oddvals[t] or t
elseif or ty == 'boolean' or ty == 'nil' then
t = tostring(t)
return tostring(t)
elseif ty == 'string' then elseif ty == 'string' then
return make_safe(t) return make_safe(t)
elseif ty == 'table' or ty == 'function' then elseif ty == 'table' or ty == 'function' then