[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: problem with string.format %d and very large integers
- From: Dirk Laurie <dpl@...>
- Date: Sat, 30 Jul 2011 22:30:07 +0200
On Sat, Jul 30, 2011 at 07:39:49PM +0200, Lorenzo Donati wrote:
> On 30/07/2011 12.17, liam mail wrote:
> > vm_details = function()
> > local env = string.dump(function()end)
> > assert(env:byte(6) == 0x00)--if hit then it is not an official Lua release
> > local number_type
> > local int_width
> > if env:byte(12) == 0x0 then
> > if env:byte(11) == 0x08 then
> > number_type = 'double'
> > elseif env:byte(11) == 0x04 then
> > number_type = 'float'
> > else
> > number_type = 'unknown'
> > end
> > else
> > number_type = 'int type of size ' .. tonumber(env:byte(11))
> > end
> > return number_type, tonumber(env:byte(8))
> > end
> >
>
> I don't understand it fully. It seems that this relies on the internal
> VM or bytecode format, which is not formalized anywhere, so you cannot
> rely on that - moreover it could change from version to version, so you
> would have to rewrite the function for each new Lua release. (BTW
> int_width is not used, a typo or what?)
>
function fix_format ()
local a = 2^53-1 -- all mantissa bits are 1
local format = string.format
-- test for presence of %d bug
if ("%d"):format(a) == "9007199254740991" then return 1 end
-- replaces string.format by one that works around the bug
-- BUG: unsophisticated replacement of %d by %.0f, e.g
-- =string.format("Fixes the %%d %s","bug")) --> Fixes the %.0f bug
local format = string.format
string.format = function (f,...)
return format(string.gsub(f,"%%([%+%-]?%d*)d","%%%1.0f"),...)
end
end
print(("%+020d %d"):format(2^53-1, 2^53-1))
--> -0000000000000000001 -1
fix_format()
print(("%+020d %d"):format(2^53-1, 2^53-1))
--> +0009007199254740991 9007199254740991
The output shown above comes from the Lua interpreter in
lua-5.2.0-beta-rc3-mingw.zip, running under "wine cmd" on
my Linux machine.
Of course, this does not work around all the other places where
the failure of a C long to contain a Lua integer is a problem.
Dirk
- References:
- problem with string.format %d and very large integers, Norman Ramsey
- Re: problem with string.format %d and very large integers, Luiz Henrique de Figueiredo
- Re: problem with string.format %d and very large integers, Lorenzo Donati
- Re: problem with string.format %d and very large integers, KHMan
- Re: problem with string.format %d and very large integers, Lorenzo Donati
- Re: problem with string.format %d and very large integers, Roberto Ierusalimschy
- Re: problem with string.format %d and very large integers, Lorenzo Donati
- Re: problem with string.format %d and very large integers, Lorenzo Donati
- Re: problem with string.format %d and very large integers, liam mail
- Re: problem with string.format %d and very large integers, Lorenzo Donati