My custom float 16 type is broken

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 ;-;

Might be unrelated but readf16 takes 2 parameters but in the print statement, you’re passing 3 arguments?

Ok nevermind, I fixed it:

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

Never mind, I just realized it is broken if you put different values

I found some videos to make a float 16. If you’re trying to make a float16, hopefully these will help you.

Encoding: https://www.youtube.com/watch?v=7DDA0O6eS3c&t=29s
Decoding: https://www.youtube.com/watch?v=hAzOGrU77og

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.