Anyone know what LFSRs are?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Anyone know what LFSRs are?

Post by pgimeno »

Thanks for the info on bit being limited to 32 bits, Zorg. It's confusing to me because this code doesn't print what I'd expect:

Code: Select all

  local bit = require('bit')
  print(("%X"):format(bit.band(2147483648, 2147483648))) -- prints FFFFFFFF80000000 on a 64 bit machine
zorg wrote: Sat Jan 07, 2023 9:02 pm ...also, i kinda want to ask what's with the recent-CS-graduate bitlen function :D
this is probably better

Code: Select all

function bitlen(x) return x == 0 and 1 or math.ceil(math.log(x+1)/math.log(2)) end
Probably, unless you try it with certain powers of two, like 2^49 or above. I didn't try such approach because it has precision problems. I could have tested which was the first number it failed with, but I just stayed away from it. I could also have made a loop but since that function was in a hot loop, I wanted it fast, so I organized it as a hardcoded binary search. Also, short loops are not LuaJIT-friendly and might break traces.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Anyone know what LFSRs are?

Post by zorg »

it actually failed at 2^39-1 for me; that should have returned 39, but returned 40 instead... interestingly 2^40-1 worked again up until 2^47-1, which failed, then 2^48-1 worked again, up until 2^51-1 which failed.

Still, for up to 32 bits, the function should work reliably, although i didn't test it (i did up to 2^23, online interpreter times out above that), but i see that logarithms do tend to cause inaccuracies with this method, compared to just comparisons with constants; that should work up to the mantissa limit for whatever number type lua uses.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 67 guests