For my SimpleBit module, I have made an adder and multiplier that work 30x faster than arithmetic * and +. however, I have run into an issue, How do I represent digital numbers, and most importantly, how do I run that through the tonumber(_,2) to get my result?
Decimal can be represented by the inverse of 2 to the power of whatever place you’re after.
1.5 would be 1.1b (1 * 2^0 + 1 * 2^-1 = 1 + 0.5)
5.25 would be 101.01b (1 * 2^2 + 1 * 2^0 + 1 * 2^-2 = 4 + 1 + 0.25)
You may allocate the last 4 bytes as you’ve mentioned to store strictly decimal values. However, keep in mind that IEEE 754 Floating Point is the standard format for storing decimal values now.
In terms of tonumber() and decimal places, it seems like there’s really no way to combine the two. Isolating the decimal portion then passing it through tonumber() won’t give you the right numbers. You’ll have to make your own converter or use another function/approach.
I’d also suggest taking a look at the string.pack functions. They’re undocumented by roblox but they’re there: Lua 5.3 Reference Manual
They let you use the characters of a string as bytes:
local num = 123.45
-- convert it to a string
local asAString = string.pack("d", num) -- "d" means "as double"
print(string.len(asAString)) -- 8 (like 8 bytes)
-- convert back to a number
local asANumber = string.unpack("d", asAString)
print(asANumber) -- 123.45
Well, it’s all useless if it’s in Unicode! This is how you’d do it if you wanted to convert it to a binary string
local floor,insert = math.floor, table.insert
function basen(n,b)
n = floor(n)
if not b or b == 10 then return tostring(n) end
local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local t = {}
local sign = ""
if n < 0 then
sign = "-"
n = -n
end
repeat
local d = (n % b) + 1
n = floor(n / b)
insert(t, 1, digits:sub(d,d))
until n == 0
return sign .. table.concat(t,"")
end
local binaryStr = string.pack("d", 124.4)
local stringStream = ""
for i=1,#binaryStr do
local c = string.byte(binaryStr:sub(i,i))
local byte = basen(c,2)
while (#byte ~= 8) do
byte = "0"..byte
end
stringStream = byte..stringStream
end
print(stringStream)
I think you are missing the purpose of using bitwise functions. They are meant to transform data into a number so that it can be used as an address for an instant lookup, unlike a dictionary creating this number is instant with bitwise functions unlike the costly string interning lookup operation for the key generation. Sure, it is faster to do integer math like this but if you want to get floats im pretty sure the fastest is already there