1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| class Float(object):
__slots__=('mult','val','sf','dec')
def __init__(self,value=0.0,mult=None,sf=None,d=None):
if type(value)not in (float,int): raise
if mult==None:
if type(value)==float:
if 'e-' in str(value):
self.mult=int('1'+'0'*(int(str(value).split('e-')[-1])+len(str(value).split('e-')[0].split('.')[-1])))
self.val=int(str(value).split('e-')[0].replace('.',''))
else:
self.mult=10**len(str(value).split('.')[-1])
self.val=int(str(value).replace('.',''))
else:
self.mult=1
self.val=value
else:
if mult!= 1 and mult%10!=0 or type(value)!=int:raise
self.mult=mult
self.val=value
if sf==None:
if self.val<0:
self.sf=1
self.val=abs(self.val)
else: self.sf=0
else:
if self.val<0:
self.sf=1
self.val=abs(self.val)
else: self.sf=sf
self.dec=d if type(d)==int and d >0 else None
def __repr__(self):
return '%s( %i / %s )<Float>'%('-' if self.sf==1 else '+',self.val,'1e'+str(str(self.mult).count('0')))
def __add__(self,other):
if type(other)!=float:
other=float(other)
val=float(self)+other
return Float(val,d=self.dec)
def __radd__(self,other):
return other.__add__(self)
def __sub__(self,other):
if type(other)!=float:
other=float(other)
val=float(self)-other
return Float(val,d=self.dec)
def __rsub__(self,other):
if type(other)!=float:
other=float(other)
return other.__sub__(self)
def __mul__(self, other):
if type(other) in (float,int):
other=Float(other)
if other.__class__==Float().__class__:
sf=self.sf^other.sf
val=self.val*other.val
mu=self.mult*other.mult
return Float(val,mu,sf,d=self.dec)
else: raise
def __rmul__(self,other):
if type(other) in (float,int):
other=Float(other)
return other.__mul__(self)
def __floordiv__(self,other):
if type(other) in (float,int):
other=Float(other)
if other.__class__==Float().__class__:
sf=self.sf^other.sf
val=(self.val*other.mult)/(other.val*self.mult)
return Float(val,sf=sf,d=self.dec)
else: raise
def __div__(self,other):
if type(other) in (float,int):
other=Float(other)
if other.__class__==Float().__class__:
sf=self.sf^other.sf
val=(float(self.val)*other.mult)/(other.val*self.mult)
return Float(val,sf=sf,d=self.dec)
else:raise
def __truediv__(self,other):
return self.__div__(other)
def __rdiv__(self,other):
if type(other) in (float,int):
other=Float(other)
if other.__class__==Float().__class__:
return other.__div__(self)
else:raise
def __mod__(self,other):
val=float(self.__str__())%(float(other.__str__()) if other.__class__==Float().__class__ else other)
return Float(val,d=self.dec)
def __rmod__(self,other):
if type(other) in (float,int):
other=Float(other)
return other.__mod__(Float(self.val,self.mult,self.sf))
def __divmod__(self,other):
if type(other) in (float,int):
other=Float(other)
return (self.__floordiv__(other),self.__mod__(other))
def __rdivmod__(self,other):
if type(other) in (float,int):
other=Float(other)
return (other.__floordiv__(self),other.__mod__(self))
def __pow__(self,other):
if type(other) in (float,int):
other=Float(other)
if other.__class__==Float().__class__:
return Float(float(self)**float(other),d=self.dec)
def __rpow__(self,other):
if type(other) in (float,int):
other=Float(other)
if other.__class__==Float().__class__:
return Float(float(other)**float(self),d=other.dec)
def round(self,integer):
val=('-'if self.sf==1 else '')+str(float(self.val)/self.mult)
if '-' in val: val=val.replace('-','')
if 'e' not in val:
entier=val.split('.')[0]
_float=val.split('.')[1]
else:
entier='0'
_float,dec=val.split('e')[0],int(val.split('e')[-1])
_float=_float.split('.')[0].rjust(dec,'0')+_float.split('.')[-1]
if 10-int(_float[integer])>5:
val=_float[:integer]
else:
val=str(int(_float[:integer])+1)
if len(val)>integer:
entier,val=str(int(entier)+1),val[1:]
mult=int('1'+'0'*integer)
return Float(int(entier+val),mult,self.sf)
def n_dec(self,integer):
if type(integer)!=int: raise
if integer<str(self.mult).count('0'):
return self.round(integer)
elif integer>str(self.mult).count('0'):
add='0'*(integer-str(self.mult).count('0'))
mult=str(self.mult)+add
val=str(self.val)+add
return Float(int(val),int(mult),self.sf)
else: return Float(self.val,self.mult,self.sf)
def __str__(self):
r=self.n_dec(self.dec)if self.dec!=None else self
dec=str(r.mult).count('0')
val=str(r.val)
if len(val)<= dec:
if dec-len(val)>8:
val=('-' if r.sf==1 else '')+val[0]+'.'+val[1:]+'e-'+str((dec-len(val[1:])))
else:
val=('-' if r.sf==1 else '')+'0.'+val.rjust(dec,'0')
else:
val=('-' if self.sf==1 else '')+(val[:dec*-1]+'.'+val[dec*-1:] if dec!=0 else val)
return val
def __nonzero__(self):
return (self.sf==1 and [0] or [1])[0]
def __float__(self):
return float(self.__str__())
def __int__(self):
return int(self.__float__())
def __long__(self):
return long(self.__float__())
def __complex__(self):
return complex(self.__float__())
def __neg__(self):
return Float(self.val,self.mult,(1 if self.sf==0 else 0))
def __pos__(self):
return self
def __abs__(self):
return Float(self.val,self.mult)
def __gt__(self,other):
if type(other) in (float,int):
other=Float(other)
if other.__class__==Float().__class__:
return True if self.__float__()>other.__float__() else False
def __ge__(self,other):
if type(other) in (float,int):
other=Float(other)
if other.__class__==Float().__class__:
return True if self.__float__()>=other.__float__() else False
def __eq__(self,other):
if type(other) in (float,int):
other=Float(other)
if other.__class__==Float().__class__:
return True if self.__float__()==other.__float__() else False
def __ne__(self,other):
if type(other) in (float,int):
other=Float(other)
if other.__class__==Float().__class__:
return True if self.__float__()!=other.__float__() else False
def __lt__(self,other):
if type(other) in (float,int):
other=Float(other)
if other.__class__==Float().__class__:
return True if self.__float__()<other.__float__() else False
def __le__(self,other):
if type(other) in (float,int):
other=Float(other)
if other.__class__==Float().__class__:
return True if self.__float__()<=other.__float__() else False |
Partager