fix mass and off-by-one errors

This commit is contained in:
Kistaro Windrider 2024-02-09 00:30:19 -08:00
parent a659679b6b
commit d5647a0328
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -417,8 +417,8 @@ mknew(fuzzy, function(x)
local p = (x.p or fuzzy.p) & 0xffff local p = (x.p or fuzzy.p) & 0xffff
x.mass = 0 x.mass = 0
while p ~= 0 do while p ~= 0 do
x.mass += p & 0x8000 x.mass += p & 1
p = p << 1 p = (p >>> 1) & 0xffff
end end
end) end)
@ -427,7 +427,7 @@ function fuzzy:update()
if (self.n < self.interval) return if (self.n < self.interval) return
self.n = 0 self.n = 0
for i=1,self.tries do for i=1,self.tries do
local b = 1 << rnd(15) local b = 1 << rnd(16)
if b & self.p == 0 then if b & self.p == 0 then
if i == self.tries or not self.weight or self.mass <= self.weight then if i == self.tries or not self.weight or self.mass <= self.weight then
self.mass += 1 self.mass += 1