整数域 |
|
--**** should be compatible with 5.xx function intlimit() local floor = math.floor -- get highest power of 2 which Lua can still handle as integer local step = 2 while true do local nextstep = step*2 if nextstep-(nextstep-1) == 1 and nextstep > 0 then step = nextstep else break end end -- now get the highest number which Lua can still handle as integer local limit,step = step,floor(step/2) while step > 0 do local nextlimit = limit+step if nextlimit-(nextlimit-1) == 1 and nextlimit > 0 then limit = nextlimit end step = floor(step/2) end return limit end
Example
local limit = intlimit() print() print("IntegerDomain - what is the largest supported integer number?") print() --**** do not rely on Lua to print "limit" properly by itself! --local printablelimit = string.format("%d", limit) -- fails under Lua! local printablelimit = string.format("%.16e", limit) print("supported integer range is: -" .. printablelimit .. "...+" .. printablelimit)
正如你所见,Lua 在处理大整数时没有问题 - 只要你不尝试将它们转换为字符串 ;-)
为了确保整数能正确地转换为字符串(而不是科学计数法),请使用 format.string("%.0f",x) 而不是 tostring(x)。
嗯,让我想想这些要求。
此外,我还做了另外两个假设:
这两个假设都可以轻松地显式检查(不要忘记在测试负数时将“floor”替换为“ceil”!)。
如果满足这些要求,就可以使用“连续逼近”来获得最大的可表示整数。
由于我没有提及任何具体结果,所以我并没有真正假定 IEEE,除了我上面显示的假设(符号位编码保证了对称性)以及我选择的测试。
floor(x) == x 应该足以测试一个数是否为整数。x-1 ~= x 刚刚被添加,以排除 NaN 和 Infinity。---- 我在 lua 5.1 上运行了这个算法,但它不太奏效:它给出的最大整数是 9007199254740994,但 print(string.format("%0.f", intlimit()-1) 返回 9007199254740992 而不是 9007199254740993。我修正了代码中的三处。
floor(x) == x 无用,因为你从整数开始,并进行整数稳定的运算,即使超出最大整数(大于尾数可表示范围的浮点数总是整数)。x-1 ~= x 已被替换为 x-(x-1) == 1,它检查 x-1 是否也可表示(它也排除 NaN 和无穷大,但它们无论如何都无法到达)。x > 0--4xel