of     2   
chevron_rightchevron_rightchevron_right

Meelo
#8219946Monday, May 04, 2009 10:52 PM GMT

It's only partially done - it can only instance and add number now, but it will get better. I promise. This script could be used for a couple purposes when done. Anything that needs precision. print("Meelo's Bit Script Loaded. It really did") local bits = {} bits.methods = {} bits.metatable = {} --This will be the metatable for all instances. bits.metatable.__index = bits.methods --------------------------------------------METATABLES---------------------------------------------- --Addition bits.metatable.__add = function(a,b) local ret = bits.new() local tab = a.inner local other = b.inner if(#other > #tab)then tab = b.inner other = a.inner end local carry = false for i,v in ipairs(tab)do local total = 0 if(v)then --Thanks Lua. You really make me feel good by not allowing arithmetic on booleans :/ total = total + 1 end if(other[i])then total = total + 1 end if(carry)then total = total + 1 carry = false end if(total > 1)then carry = true total = total - 2 end ret:add(total==1) end if(carry)then ret:add(true) end return ret end --tostring bits.metatable.__tostring = function(a) return tostring(a:numbervalue()) end -------------------------------------------METHODS--------------------------------------------------------- bits.methods.add = function(me,bool) table.insert(me.inner,bool) end bits.methods.numbervalue = function(a) local val = 1 local ret = 0 for i,v in ipairs(a.inner)do if(v)then ret = ret + val end val = val * 2 end return ret end -------------------------------------------MISC--------------------------------------------------------------- function bits.new(val) --Instance it val = math.floor(val or 0) --Round it/Make it real local ret = {} ret.inner = {} local place = 2 while val > 0 do --Well, it has to become binary somehow! local mod = val % place if(mod == 0)then table.insert(ret.inner,false) else table.insert(ret.inner,true) end val = val - mod place = place * 2 end setmetatable(ret,bits.metatable) return ret end local num = bits.new(5) local num2 = bits.new(7) local num3 = bits.new(15) print(num+num2+num3)
level140roblox
#8220025Monday, May 04, 2009 10:53 PM GMT

Rofl i can't think of a use for that =)
Meelo
#8220078Monday, May 04, 2009 10:54 PM GMT

Big number calculator XD I question the accuracy of Lua's built in numbers.
XlegoX
#8220162Monday, May 04, 2009 10:56 PM GMT

Nice. You should make it with an argument to "tostring" to determine what base it should be put in =P
Meelo
#8220254Monday, May 04, 2009 10:57 PM GMT

True. Would it just be passed to the __tostring metamethod as another parameter? Although I might crop it to bases 2 - 36
XlegoX
#8220346Monday, May 04, 2009 10:59 PM GMT

Yea, if the second arg is nil, then you use a default base, otherwise you use the base specified as the second arg up to base 32
Meelo
#8221696Monday, May 04, 2009 11:25 PM GMT

My script isn't recieving the second argument... local t = {} setmetatable(t,{__tostring = print}) print(tostring(t,8)) Isn't working, but, I tried it several times and got a very misplaced C Stack overflow error... it didn't even print the stack...
XlegoX
#8221820Monday, May 04, 2009 11:27 PM GMT

that's because when it tries to print the thing, sirt is uses tostring on it, but tostring is print, so it goes back to print, which goes back to string etc...
Meelo
#8221918Monday, May 04, 2009 11:28 PM GMT

Oh, well still... local t = {} setmetatable(t,{__tostring = function(__,b) print(b) end}) print(tostring(t,8)) prints nil.
Meelo
#8226790Tuesday, May 05, 2009 12:56 AM GMT

Methinks that tostring phails.
Meelo
#8247621Tuesday, May 05, 2009 9:00 PM GMT

Added operators: - (Subtraction, Absolute value) - (Negation, Binary Inversion in this case) Comparative operators. print("Meelo's Bit Script Loaded. It really did") --Positive numbers only pl0x. local bits = {} bits.methods = {} bits.metatable = {} --This will be the metatable for all instances. bits.metatable.__index = bits.methods --------------------------------------------METATABLES---------------------------------------------- --Addition bits.metatable.__add = function(a,b) local ret = bits.new() local tab = a.inner local other = b.inner if(#other > #tab)then tab = b.inner other = a.inner end local carry = false for i,v in ipairs(tab)do local total = 0 if(v)then --Thanks Lua. You really make me feel good by not allowing arithmetic on booleans :/ total = total + 1 end if(other[i])then total = total + 1 end if(carry)then total = total + 1 carry = false end if(total > 1)then carry = true total = total - 2 end ret:add(total==1) end if(carry)then ret:add(true) end return ret end --Subtraction [Note, this will get the absolute value] bits.metatable.__sub = function(a,b) local ret = bits.new() local tab = a.inner local other = b.inner if(b > a)then other = a.inner tab = b.inner end local carry = false --Errr... wrong terminology... for i,v in ipairs(tab)do local total = 0 if(v)then total = 1 end if(other[i])then total = total - 1 end if(carry)then total = total - 1 carry = false end if(total < 0)then carry = true total = total + 2 end ret:add(total==1) end return ret end --Binary inverse. bits.metatable.__unm = function(a) local ret = bits.new() for i,v in ipairs(a.inner)do ret:add(not v) end return ret end --Length operator. #blah bits.metatable.__len = function(a) return a:tonumber() end --Equal to bits.metatable.__eq = function(a,b) for i = 1,math.max(#a.inner,#b.inner)do if(not ((a.inner[i] or 0) == (b.inner[i] or 0)))then return false end end return true end --Less than bits.metatable.__lt = function(a,b) local big = math.max(#a.inner,#b.inner) for i = 0,big-1 do if(((a.inner[big-i] and 1) or 0) < ((b.inner[big-i] and 1) or 0))then --Spaghetti Code? Nowai! return true elseif(((a.inner[big-i] and 1) or 0) > ((b.inner[big-i] and 1) or 0))then return false end end return false end --Less than or equal to. bits.metatable.__le = function(a,b) local big = math.max(#a.inner,#b.inner) for i = 0,big-1 do if(((a.inner[big-i] and 1) or 0) < ((b.inner[big-i] and 1) or 0))then --Spaghetti Code? Nowai! return true elseif(((a.inner[big-i] and 1) or 0) > ((b.inner[big-i] and 1) or 0))then print(a.inner[big-i],b.inner[big-i]) return false end end return true end --tostring bits.metatable.__tostring = function(a,b) print(b) return a:inbase(b) end -------------------------------------------METHODS--------------------------------------------------------- --Not useful for user bits.methods.add = function(me,bool) table.insert(me.inner,bool) end --tonumber method bits.methods.tonumber = function(me) local val = 0 local place = 1 for i,v in ipairs(me.inner)do if(v)then val = val + place end place = place * 2 end return val end --inbase method. local digits = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"} bits.methods.inbase = function(a,base) base = base or 10 print("BASE: " .. base) local val = 1 local cur = base local ret = "" local digit = 0 for i,v in ipairs(a.inner)do if(v)then digit = digit + val end val = val * 2 if(val > cur)then local pos = digit%cur pos = (pos - (digit%(cur/base))) ret = digits[pos/(cur/base)+1] .. ret cur = cur * base digit = digit - pos end end if(digit>0)then local pos = digit%cur pos = (pos - (digit%(cur/base))) ret = digits[pos/(cur/base)+1] .. ret end return ret end -------------------------------------------MISC--------------------------------------------------------------- function bits.new(val) --Instance it val = math.floor(val or 0) --Round it/Make it real local ret = {} ret.inner = {} local place = 2 while val > 0 do --Well, it has to become binary somehow! local mod = val % place if(mod == 0)then table.insert(ret.inner,false) else table.insert(ret.inner,true) end val = val - mod place = place * 2 end setmetatable(ret,bits.metatable) return ret end local num = bits.new(1024) local num2 = bits.new(4) print(num,num2,num-num2,num+num2,-num)
Meelo
#8247900Tuesday, May 05, 2009 9:06 PM GMT

How would you do division in bits?
XlegoX
#8250392Tuesday, May 05, 2009 9:56 PM GMT

Have you learnt polynomial subtraction yet? Just do it like that with X as the base
DingDong272
Top 100 Poster
#8250437Tuesday, May 05, 2009 9:57 PM GMT

Have you tried "simplyfing" radical expressions yet? Blegh...
Meelo
#8250500Tuesday, May 05, 2009 9:58 PM GMT

Nups. I'm trying to use this: bits.metatable.__mul = function(a,b) local ret = bits.new() local new = b:clone() for i,v in ipairs(a.inner)do if(v)then ret = ret + new print(ret,new) end new:lshift(1) end return ret end But it doesn't work, and it's not efficient (The complexity is quadratic)
Meelo
#8251206Tuesday, May 05, 2009 10:11 PM GMT

Oh - lshift = left shift (The bitwise operator)
Meelo
#8252003Tuesday, May 05, 2009 10:26 PM GMT

Blech - It printed the wrong thing.
Davidii
#8253167Tuesday, May 05, 2009 10:48 PM GMT

Erm... I don't understand what you're trying to do, here. XD
Meelo
#8253264Tuesday, May 05, 2009 10:49 PM GMT

It's like Lua's numbers + accuracy.
blobbyblob
#8253632Tuesday, May 05, 2009 10:57 PM GMT

Lol, me too, David. I have given up trying to decipher meelo's scripts long ago.
Meelo
#8253844Tuesday, May 05, 2009 11:01 PM GMT

I took the time to write it, I only expect you to memorize every single line!
DingDong272
Top 100 Poster
#8253986Tuesday, May 05, 2009 11:04 PM GMT

Meelo is brilliant for one reason only: He understands WHY things work. Most of you know that :GetChildren() is used in a for statement and you can edit things, but you don't know WHY IT WORKS. [no yelling intended in caps] It works because (prepare yourselves), it organizes the data into a table. The pairs statement goes through tables, so you can have: for i,v in pairs(game:GetChildren()) do end it goes through the table. Most of you know that, just giving you an example of why he's brilliant. Like those Guiness guys.
Davidii
#8255075Tuesday, May 05, 2009 11:27 PM GMT

But what's it DO?
blobbyblob
#8255147Tuesday, May 05, 2009 11:28 PM GMT

"I took the time to write it, I only expect you to memorize every single line!" Oh, darn, better get crackin'
Meelo
#8255328Tuesday, May 05, 2009 11:32 PM GMT

"But what's it DO?" More accurate numbers than are default

    of     2   
chevron_rightchevron_rightchevron_right