/usr/share/pyshared/swap/local_decimal.py is in python-swap 1.2.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | """Decimal datatype
This is an implementation of the Decimal XML schema datatype in python
magnitude is the log10 of the number we multiply it by to get an integer
$Id: local_decimal.py,v 1.2 2006/01/10 13:58:47 syosi Exp $
"""
from types import IntType, FloatType, LongType, StringTypes
from math import log10
class Decimal:
"""make a new Decimal
Argument can be string, int, long, or float
float is not recommended
"""
_limit = 16
def normalize(self):
"""convert this Decimal into some sort of canonical form
"""
if self.value == 0:
self.magnitude = 0
return
while self.value.__mod__(10) == 0:
self.value = self.value / 10
self.magnitude = self.magnitude - 1
## while self.magnitude > 2 * self.__class__._limit:
## self.value = self.value / 10
## self.magnitude = self.magnitude - 1
def __init__(self, other=0):
"""How to get a new Decimal
What happened?
"""
if isinstance(other, Decimal):
self.value = other.value
self.magnitude = other.magnitude
return
elif isinstance(other, IntType):
self.value = long(other)
self.magnitude = 0
self.normalize()
return
elif isinstance(other, LongType):
self.value = other
self.magnitude = 0
self.normalize()
return
elif hasattr(other,'__Decimal__') and callable(getattr(other, '__Decimal__')):
a = other.__Decimal__()
self.value = a.value
self.magnitude = a.magnitude
self.normalize()
return
elif isinstance(other,FloatType):
other = `other`
try:
other[0]
except TypeError:
other = `other`
other = other + 'q'
i = 0
value = long(0)
magnitude = long(0)
sign = 1
newsign = 1
base = 10
magnitude_multiplier = 0
if other[i] == '-':
sign = -1
i = i + 1
while other[i] in '0123456789':
ours = other[i]
i = i+1
value = value*base+int(ours, base)
if other[i] == '.':
i = i+1
while other[i] in '0123456789':
ours = other[i]
i = i+1
value = value*base+int(ours, base)
magnitude = magnitude + 1
if other[i] in 'eE':
i = i+1
if other[i] == '+':
i = i+1
elif other[i] == '-':
newsign = -1
i = i+1
while other[i] in '0123456789':
ours = other[i]
i = i+1
magnitude_multiplier = magnitude_multiplier*10+int(ours, 10)
self.magnitude = magnitude-newsign*magnitude_multiplier
self.value = value*sign
self.normalize()
def __abs__(self):
"""x.__abs__() <==> abs(x)
"""
a = self.__class__(self)
a.value = abs(a.value)
a.normalize()
return a
def __add__(self, other):
"""x.__add__(y) <==> x+y
"""
# if not isinstance(other, Decimal):
# other = self.__class__(other)
if other.magnitude < self.magnitude:
return other.__add__(self)
while other.magnitude > self.magnitude:
self.magnitude = self.magnitude+1
self.value = self.value * 10
a = self.__class__()
a.value = self.value + other.value
a.magnitude = self.magnitude
self.normalize()
a.normalize()
return a
def __cmp__(self, other):
"""x.__cmp__(y) <==> cmp(x,y)
"""
if not isinstance(other, Decimal):
other = self.__class__(other)
if other.magnitude < self.magnitude:
return -other.__cmp__(self)
while other.magnitude > self.magnitude:
self.magnitude = self.magnitude+1
self.value = self.value * 10
a = cmp(self.value, other.value)
self.normalize()
return a
def __coerce__(self, other):
"""x.__coerce__(y) <==> coerce(x, y)
"""
if other.__class__ == float:
return float(self), other
return self, self.__class__(other)
pass
def __div__(self, other):
"""x.__div__(y) <==> x/y
"""
while self.magnitude < self.__class__._limit + other.magnitude + int(log10(other)):
self.value = self.value * 10
self.magnitude = self.magnitude + 1
if self.value % other.value:
a = float(self) / float(other)
else:
a = self.__class__()
a.value = self.value // other.value
a.magnitude = self.magnitude - other.magnitude
a.normalize()
self.normalize()
if a == NotImplemented:
raise RuntimeError
return a
def __divmod__(self, other):
"""x.__divmod__(y) <==> divmod(x, y)
"""
return (self // other, self % other)
def __float__(self):
"""x.__float__() <==> float(x)
"""
return float(self.value * 10**(-self.magnitude))
def __floordiv__(self, other):
"""x.__floordiv__(y) <==> x//y
"""
# if not isinstance(other, Decimal):
# other = self.__class__(other)
if other.magnitude < self.magnitude:
return other.__rfloordiv__(self)
while other.magnitude > self.magnitude:
self.magnitude = self.magnitude+1
self.value = self.value * 10
a = self.__class__()
a.magnitude = 0
a.value = self.value // other.value
a.normalize()
return a
def __hash__(self):
"""x.__hash__() <==> hash(x)
"""
return hash((self.value, self.magnitude))
def __int__(self):
"""x.__int__() <==> int(x)
"""
value = self.value
power = self.magnitude
while power > 0:
value = value // 10
power = power - 1
return int(value * 10**(-power))
def __long__(self):
"""x.__long__() <==> long(x)
"""
value = self.value
power = self.magnitude
while power > 0:
value = value // 10
power = power - 1
return long(value * 10**(-power))
def __mod__(self, other):
"""x.__mod__(y) <==> x%y
"""
# if not isinstance(other, Decimal):
# other = self.__class__(other)
if other.magnitude < self.magnitude:
return other.__rmod__(self)
while other.magnitude > self.magnitude:
self.magnitude = self.magnitude+1
self.value = self.value * 10
a = self.__class__()
a.magnitude = self.magnitude
a.value = self.value % other.value
a.normalize()
return a
def __mul__(self, other):
"""x.__mul__(y) <==> x*y
"""
# if not isinstance(other, Decimal):
# other = self.__class__(other)
a = self.__class__()
a.value = self.value * other.value
a.magnitude = self.magnitude + other.magnitude
a.normalize()
return a
def __neg__(self):
"""x.__neg__ <==> -x
"""
a = self.__class__(self)
a.value = -a.value
return a
def __nonzero__(self, other):
"""x.__nonzero__() <==> x != 0
"""
return self.value != 0
def __pos__(self):
"""x.__pos__() <==> +x
"""
return self.__class__(self)
def __pow__(self, other, mod=0):
"""x.__pow__(y[, z]) <==> pow(x, y[, z])
If the exponent is not an integer, we will simply convert things to floats first
"""
if not isinstance(other, Decimal):
other = self.__class__(other)
while other.magnitude < 0:
other.value = other.value*10
other.magnitude = other.magnitude + 1
if other.magnitude == 0:
a = self.__class__()
a.value = self.value ** other.value
a.magnitude = self.magnitude * other.value
a.normalize()
if mod !=0:
a = a%mod
return a
else:
#I honestly think that here we can give up on accuracy
## tempval = self.__class__(self.value ** other.value)
## tempval2 = self.__class__(10 ** (self.magnitude * other.value))
## temppow = 10 ** other.magnitude
## a = self.__class__(n_root(tempval, temppow))
## b = self.__class__(n_root(tempval2, temppow))
## c = a / b
## c.normalize()
a = self.__class__(pow(float(self),float(other),mod))
return a
def __radd__(self, other):
"""x.__radd__(y) <==> y+x
"""
return self.__add__(other)
def __rdiv__(self, other):
"""x.__rdiv__(y) <==> y/x
"""
if not isinstance(other, Decimal):
other = self.__class__(other)
return other.__div__(self)
def __rdivmod__(self, other):
"""x.__rdivmod__(y) <==> divmod(y, x)
"""
return other.__rdivmod__(self)
def __repr__(self):
"""x.__repr__() <==> repr(x)
"""
return '%s("%s")' % (self.__class__.__name__, str(self))
def __rfloordiv__(self, other):
"""x.__rfloordiv__(y) <==> y//x
"""
# if not isinstance(other, Decimal):
# other = self.__class__(other)
if other.magnitude < self.magnitude:
return other.__floordiv__(self)
while other.magnitude > self.magnitude:
self.magnitude = self.magnitude+1
self.value = self.value * 10
a = self.__class__()
a.magnitude = 0
a.value = other.value // self.value
a.normalize()
return a
def __rmod__(self, other):
"""x.__rmod__(y) <==> y%x
"""
if not isinstance(other, Decimal):
other = self.__class__(other)
if other.magnitude < self.magnitude:
return other.__mod__(self)
while other.magnitude > self.magnitude:
self.magnitude = self.magnitude+1
self.value = self.value * 10
a = self.__class__()
a.magnitude = self.magnitude
a.value = other.value % self.value
a.normalize()
return a
def __rmul__(self, other):
"""x.__rmul__(y) <==> y*x
"""
return self.__mul__(other)
def __rpow__(self, other, mod=0):
"""y.__rpow__(x[, z]) <==> pow(x, y[, z])
"""
return other.__pow__(self, mod)
def __rsub__(self, other):
"""x.__rsub__(y) <==> y-x
"""
a = self.__class__(self)
a.value = -a.value
return a.__add__(other)
def __rtruediv__(self, other):
"""x.__rtruediv__(y) <==> y/x
"""
return self.__rdiv__(other)
# def __setattr__(self, other):
# pass
def __str__(self):
"""x.__str__() <==> str(x)
"""
magnitude = self.magnitude
value = self.value
output = []
if value == 0:
return "0"
try:
magSign = magnitude / abs(magnitude)
except ZeroDivisionError:
magSign = 0
sign = value / abs(value)
value = abs(value)
while magnitude < 0:
output.append("0")
magnitude = magnitude + 1
while value != 0:
if magnitude == 0 and magSign == 1:
output.append(".")
magSign = 0
digit = value.__mod__(10)
value = value // 10
output.append("0123456789"[digit])
magnitude = magnitude-1
while magnitude > 0:
output.append("0")
magnitude = magnitude - 1
if magSign == 1:
output.append('0.')
if sign == -1:
output.append('-')
output.reverse()
return "".join(output)
def __sub__(self, other):
"""x.__sub__(y) <==> x-y
"""
a = self.__class__(other)
a.value = -a.value
return self.__add__(a)
def __truediv__(self, other):
"""x.__truediv__(y) <==> x/y
"""
return self.__div__(self.__class__(other))
def n_root(base, power):
"""Find the nth root of a Decimal
"""
print 'trying to compute ', base, ' ** 1/ ', power
accuracy = Decimal(1)
n = 10 #Decimal._limit
while n > 0:
accuracy = accuracy / 10
n = n-1
oldguess = Decimal(0)
guess = Decimal('.00000002')
counter = 0
while 1:
oldguess = guess
counter = counter + 1
if counter == 100:
print guess
counter = 0
h = 1 - base * (guess ** power)
guess = guess + guess * h / power
if abs(guess - oldguess) <= accuracy:
# print guess
break
# print guess
answer = Decimal(1) / Decimal(guess)
print answer
return answer
|