Whenever I try to deserialize it, it comes out as the wrong number.
local function writef16(buf, value, index)
--for reference: f32 = 4 bytes
local exp = 0
local num = value
local maxAttempts = 100
local attempts = 0
while attempts <= maxAttempts do
num = value/2
exp += 1
attempts += 1
if math.floor(num) == 1 or math.floor(num) == -1 then
break
end
end
local decimal = math.abs(num) - 1
if math.abs(value) ~= value then --Negative
exp = -exp
end
buffer.writei8(buf, index, exp)
buffer.writeu16(buf, index + 1, math.round(decimal * 100000))
return 3
end
local function readf16(buf, index)
local exp = buffer.readi8(buf, index)
local decimal = buffer.readu16(buf, index + 1)/100000
local num = (decimal + 1) * (exp * 2)
return num / (220.357758/90) --To do: remove the constant
end
local buf = buffer.create(50)
writef16(buf, 0.25, 0)
print(readf16(buf, 0.25, 0)) --118.4500597433016 ;-;
local function writef16(buf, value, index)
local exp = 0
local num = math.abs(value) + 2
local maxAttempts = 100
local attempts = 0
while attempts <= maxAttempts do
num = num/2
exp += 1
attempts += 1
if math.floor(num) == 1 then
break
end
end
local decimal = math.abs(num) - 1
if math.abs(value) ~= value then --Negative
exp = -exp
end
buffer.writei8(buf, index, exp)
buffer.writeu16(buf, index + 1, math.round(decimal * 100000))
return 3
end
local function readf16(buf, index)
local exp = buffer.readi8(buf, index)
local decimal = buffer.readu16(buf, index + 1)/100000
local num = (decimal + 1) * (exp * 2)
return num - 2
end
Also this isn’t actually a float16, I just wanted to make something smaller than a float32