Log In  


Cart #f32b_test-0 | 2024-08-22 | Embed ▽ | License: CC4-BY-NC-SA

  • Operation cannot be confirmed online.

If you want to store floating point values ​​in user data (or POD storage), currently using floats in user data or memmaps will not give good results.

My guess is that using floats in user data will be treated as standard in the future.

This program, f32b.lua, is included, and main.lua contains a simple usage example.
It is very experimental, but may be useful in some way.
It may also be possible to improve it and achieve more.
I don't think f32b.lua is fully optimized, but someone could perfect it.

The usage is as follows (contents of main.lua):

include "f32b.lua"

--[[
*** If you store it in user data.

f32b.lua uses "u8" for user data. Each floating point value uses 4 bytes.
In other words, if you want to save two values, create it as userdata("u8", 8).
--]]
mydata = userdata("u8", 8)

-- To set a value for user data, do this:
set_value(mydata, 0, -123.45)
set_value(mydata, 1, 100.0)

-- To extract it from the user data:
print(get_value(mydata, 0))
print(get_value(mydata, 1))

--[[
32b.lua is not for user data.
Use this if you want to create pure floating point binaries.
--]]
v = pack(d2b(3.1415))
binary = string.format("%08x", v)
print(binary)

-- To revert back to the original value:
print(b2d(unpack(v)))

-- An unpacked value is a 4-byte table.
data = d2b(123.45)
for i=1,4 do
	print(data[i])
end

Feel free to modify it to make it easier for you. :)




[Please log in to post a comment]