lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 11/20/2017 10:29 PM, Coda Highland wrote:
On Sun, Nov 19, 2017 at 12:13 AM, KHMan wrote:
Keeping 2s complement arithmetic in looping and its flaw(s) isn't
perfect but it's a known quantity. Patching the looping code would make it
"2s complement but not quite 2s complement". Either way, I hope we can have
some updates to the section on numerical loops in the reference manual.

It's NOT 2s complement, though. It's 2s complement if you're working
with integer loops, but it's signed magnitude if you're working with
floating point, and Lua doesn't tell you explicitly which you're
working with unless you ask. So it's already "signed magnitude, except
when it's 2s complement" and Paige's patch brings it just a little bit
closer to "always signed magnitude".

Sorry to myself for not being specific enough. I was discussing the integer portion, I thought that was obvious. I assumed the specified loop pseudo-code intentionally matches C-style loop behaviour, and so integer loops are more or less 2s complement thingies. I'm happy to follow specifications, that is, the pseudo-code.

All those terminology ends up just being terminology when you get to corner cases. Try these:

local j = 1
for i = 2^53-10,math.huge do
  print(string.format("%4d %20d", j, i))
  j = j + 1; if j == 20 then return end
end

local j = 1
for i = 2^53-10,2^53 do
  print(string.format("%4d %20d", j, i))
  j = j + 1; if j == 20 then return end
end

local j = 1
for i = 2^53-10,2^53+1 do
  print(string.format("%4d %20d", j, i))
  j = j + 1; if j == 20 then return end
end

local j = 1
for i = 2^53-10,2^53+2 do
  print(string.format("%4d %20d", j, i))
  j = j + 1; if j == 20 then return end
end

Corner cases are fun, as long as one does not use them in serious code.

--
Cheers,
Kein-Hong Man (esq.)
Selangor, Malaysia