If you use the same HUGE number many times:
- import sys
- sys.maxint = sys.maxint+sys.maxint
This is because if you have the INFINITE(or lower) refrence to the same integer, It will take up the sppace of that integer.
If you use many diffrent huge numbers:Use a accurate way to calculate when needed[1]
- Use an inacurrate one
Here's the code:
class num(object):
"""
For STORING numbers,many operations will be slow
(will also cause a temporary low memory when the function is running,
so may cause memory error)
,increment/decrement/power,
need to have it's memory freed,
later.
Also, use x += y not x = x + y and for subtraction and power, but DO NOT use it for anything else.
"""
def convertto(self,n):
if n != 0:
x = math.log(float(n),2)
y = x - math.floor(x)
else:
x = 0.0
y = -1
return x,y
def __float__(self):
return 2**self.x + self.y
regular = __float__
def totalmem(self):
tot = 0
if type(n.y) == num:
print"A"
tot = n.y.totalmem()+tot
tot = n.x.totalmem()+tot
else:
print"B"
tot = math.abs(n.y)+tot
tot = math.abs(n.x)+tot
return tot
def freememory(self):
"call when many increment/decrement/power operations have been called in a row, if the last called was not increment/decrement/power, do not call"
self.x,self.y = self.convertto(self.convertfrom())
def newlevel(self):
"frees memory even more, may be redundant and calculations are even slower"
if (type(self.y) == int) or (type(self.y) == long) or (type(self.y) == float):
self.y = num(self.y)
self.x = num(self.x)
else:
self.y.newlevel()
self.x.newlevel()
def check(self):
if (type(self.y) == long):
self.newlevel()
if type(self.y) == num:
if self.y.check():
self.newlevel()
def __init__(self,n=1):
self.x , self.y = self.convertto(n)
def __divmod__(self,other):
return divmod(self.convertfrom(),other)
def __ipow__(self,other):
self.x = self.x * other
self.y = self.y ** other
return self
def __iadd__(self,other):
self.y += other
return self
def __str__(self):
return "2^"+str(self.x)+" + " + str(self.y)
def __cmp__(self,other):
if type(other) == num:
if self.x != other.x:
return self.x.__cmp__(other.x)
else:
return self.y.__cmp__(other.x)
s = self.convertfrom()
if other < s:
return 1
elif other == s:
return 0
else:
return -1
else:
return other.__cmp__(self)
def _nop(name,sym):# majic funcs to implement diffrent kinds of +-*/ and others
setattr(num,"__"+name+"__",eval("lambda self,other:self.convertfrom() %s other"%sym))
def _rnop(name,sym):
sym = "\""+sym+"\""
setattr(num,"__r"+name+"__",eval("lambda self,other:_op(other,%s)"%sym))
def _fnop(name,sym):
setattr(num,"__"+name+"__",eval("lambda self:[0](int(self))"%sym))
class string(object):
def __init__(self,initialize=""):
self.l = []
for c in initialize:
self.l.append(num(ord(c)))
def __str__(self):
r=""
for i in self.l:
r = r + chr(int(i))
return r
regular = __str__
_nop("add","+") #lazy!!!!!!!!
_nop("sub","-")
_nop("mul","*")
_nop("floordiv","//")
_nop("mod","%")
_nop("and","and")
_nop("pow","**")
_nop("div","/")
num.__truediv__ = lambda self,other:convertto(self)/float(other)
_rnop("add","+")
_rnop("sub","-")
_rnop("mul","*")
_rnop("floordiv","//")
_rnop("mod","%")
_rnop("and","and")
_rnop("pow","**")
def compress(obj,verbose=False):
'compress any object'
"return for builtins and classes, but in place for objects."
if type(obj) == list:
return [compress(i) for i in obj]
elif type(obj) == int:
return num(obj)
elif type(obj) == str:
return string(obj)
elif type(obj) == type:
return obj
elif str(type(obj))== "<type 'function'>":
return obj
elif type(obj) in (type(string),type(num)):
return obj
elif type(obj) == dict:
for k,v in zip(obj.keys(),obj.values()):
obj[k] = compress(v)
else:
for i in dir(obj):
try:
if i.startswith("__") and i.endswith("__"):
continue
a = getattr(obj,i)
setattr(obj,i,compress(a))
if verbose:
print "SUCCCESS on " + i + " with type of " + str(type(a))
except Exception , e:
if str(e).endswith("is read-only"):
continue
if verbose:
print "FAIL on " + i + " with type of " + str(type(i))
traceback.print_exc()