[Python-checkins] python/nondist/sandbox/decimal Decimal.py, 1.21,
1.22
facundobatista at users.sourceforge.net
facundobatista at users.sourceforge.net
Thu Jun 24 21:29:32 EDT 2004
Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10118
Modified Files:
Decimal.py
Log Message:
Made near immutable. Lot of clean-ups.
Index: Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** Decimal.py 21 Jun 2004 21:50:51 -0000 1.21
--- Decimal.py 25 Jun 2004 01:29:29 -0000 1.22
***************
*** 96,99 ****
--- 96,101 ----
# facundobatista:
+ # 0.8.2 2004.06.24 Lot of small clean-ups from Raymond Hettinger.
+ # 0.8.1 2004.06.23 Complies with immutability-near as discussed in c.l.py.
# 0.8.0 2004.06.21 Using the updated Spec (and complies with new test cases) from
# Cowlishaw. Deprecated trim and rescale. Changed __str__ and
***************
*** 131,135 ****
import copy
import math
- import time
from operator import xor
--- 133,136 ----
***************
*** 351,360 ****
return Infsign[sign]
if sign == 0:
! if context.rounding in (ROUND_CEILING,):
return Infsign[sign]
return Decimal((sign, (9,)*context.prec,
context.Emax-context.prec+1))
if sign == 1:
! if context.rounding in (ROUND_FLOOR,):
return Infsign[sign]
return Decimal( (sign, (9,)*context.prec,
--- 352,361 ----
return Infsign[sign]
if sign == 0:
! if context.rounding == ROUND_CEILING:
return Infsign[sign]
return Decimal((sign, (9,)*context.prec,
context.Emax-context.prec+1))
if sign == 1:
! if context.rounding == ROUND_FLOOR:
return Infsign[sign]
return Decimal( (sign, (9,)*context.prec,
***************
*** 441,445 ****
self._exp = 'N' #sNaN
self._sign = sign
! self._int = tuple(map(int, tuple(diag))) #Diagnostic info
return
self._convertString(value, context)
--- 442,446 ----
self._exp = 'N' #sNaN
self._sign = sign
! self._int = tuple(map(int, diag)) #Diagnostic info
return
self._convertString(value, context)
***************
*** 489,493 ****
return
! raise TypeError("Can't convert " + `value`)
def from_float(cls, value, positions=None):
--- 490,494 ----
return
! raise TypeError("Can't convert %r" % value)
def from_float(cls, value, positions=None):
***************
*** 818,822 ****
else:
sign = 1
! if context._rounding_decision in (ALWAYS_ROUND,):
return Decimal((sign, self._int, self._exp))._fix(context=context)
return Decimal( (sign, self._int, self._exp))
--- 819,823 ----
else:
sign = 1
! if context._rounding_decision == ALWAYS_ROUND:
return Decimal((sign, self._int, self._exp))._fix(context=context)
return Decimal( (sign, self._int, self._exp))
***************
*** 838,842 ****
sign = 0
! if context._rounding_decision in (ALWAYS_ROUND,):
ans = self._fix(context=context)
else:
--- 839,843 ----
sign = 0
! if context._rounding_decision == ALWAYS_ROUND:
ans = self._fix(context=context)
else:
***************
*** 888,892 ****
return Decimal(other) #Can't both be infinity here
! shouldround = context._rounding_decision in (ALWAYS_ROUND,)
exp = min(self._exp, other._exp)
--- 889,893 ----
return Decimal(other) #Can't both be infinity here
! shouldround = context._rounding_decision == ALWAYS_ROUND
exp = min(self._exp, other._exp)
***************
*** 1030,1034 ****
ans = Decimal((self._sign, L, self._exp))
! if round and context._rounding_decision in (ALWAYS_ROUND,):
ans = ans._fix(context=context)
return ans
--- 1031,1035 ----
ans = Decimal((self._sign, L, self._exp))
! if round and context._rounding_decision == ALWAYS_ROUND:
ans = ans._fix(context=context)
return ans
***************
*** 1059,1063 ****
resultexp = self._exp + other._exp
! shouldround = context._rounding_decision in (ALWAYS_ROUND,)
# Special case for multiplying by zero
--- 1060,1064 ----
resultexp = self._exp + other._exp
! shouldround = context._rounding_decision == ALWAYS_ROUND
# Special case for multiplying by zero
***************
*** 1090,1099 ****
accumulator = [0]*(len(self._int) + len(other._int))
! for i in range(len(op2)):
if op2[i] == 0:
continue
mult = op2[i]
carry = 0
! for j in range(len(op1)):
carry, accumulator[i+j] = divmod( mult * op1[j] + carry
+ accumulator[i+j], 10)
--- 1091,1100 ----
accumulator = [0]*(len(self._int) + len(other._int))
! for i in xrange(len(op2)):
if op2[i] == 0:
continue
mult = op2[i]
carry = 0
! for j in xrange(len(op1)):
carry, accumulator[i+j] = divmod( mult * op1[j] + carry
+ accumulator[i+j], 10)
***************
*** 1196,1200 ****
#OK, so neither = 0, INF
! shouldround = context._rounding_decision in (ALWAYS_ROUND,)
#If we're dividing into ints, and self < other, stop.
--- 1197,1201 ----
#OK, so neither = 0, INF
! shouldround = context._rounding_decision == ALWAYS_ROUND
#If we're dividing into ints, and self < other, stop.
***************
*** 1748,1752 ****
mul = Decimal(1).__div__(mul, context=context)
! shouldround = context._rounding_decision in (ALWAYS_ROUND,)
spot = 1
--- 1749,1753 ----
mul = Decimal(1).__div__(mul, context=context)
! shouldround = context._rounding_decision == ALWAYS_ROUND
spot = 1
***************
*** 1825,1835 ****
"""
if self._isnan() or other._isnan():
! if self._isnan() and other._isnan():
! return True
! return False
if self._isinfinity() or other._isinfinity():
! if self._isinfinity() and other._isinfinity():
! return True
! return False
return self._exp == other._exp
--- 1826,1832 ----
"""
if self._isnan() or other._isnan():
! return self._isnan() and other._isnan() and True
if self._isinfinity() or other._isinfinity():
! return self._isinfinity() and other._isinfinity() and True
return self._exp == other._exp
***************
*** 2044,2048 ****
if self < other:
ans = other
! shouldround = context._rounding_decision in (ALWAYS_ROUND,)
if shouldround:
ans = ans._fix(context=context)
--- 2041,2045 ----
if self < other:
ans = other
! shouldround = context._rounding_decision == ALWAYS_ROUND
if shouldround:
ans = ans._fix(context=context)
***************
*** 2068,2072 ****
ans = other
! if context._rounding_decision in (ALWAYS_ROUND,):
ans = ans._fix(context=context)
--- 2065,2069 ----
ans = other
! if context._rounding_decision == ALWAYS_ROUND:
ans = ans._fix(context=context)
***************
*** 2078,2084 ****
return True
rest = self._int[self._exp:]
! if rest == (0,)*len(rest):
! return True
! return False
def _iseven(self):
--- 2075,2079 ----
return True
rest = self._int[self._exp:]
! return rest == (0,)*len(rest)
def _iseven(self):
***************
*** 2096,2099 ****
--- 2091,2105 ----
return 0
+ #properties to immutability-near feature
+ def _get_sign(self):
+ return self._sign
+ def _get_int(self):
+ return self._int
+ def _get_exp(self):
+ return self._exp
+ sign = property(_get_sign)
+ int = property(_get_int)
+ exp = property(_get_exp)
+
# get rounding method function:
***************
*** 2309,2313 ****
def __repr__(self):
! return "(" + `self.sign` + ", " + `self.int` + ", " + `self.exp` + ")"
__str__ = __repr__
--- 2315,2319 ----
def __repr__(self):
! return "(%r, %r, %r)" % (self.sign, self.int, self.exp)
__str__ = __repr__
***************
*** 2327,2332 ****
def __cmp__(self, other):
if self.exp != other.exp:
! raise ValueError("Operands not normalized: " + `self` +
! ", " + `other`)
if self.sign != other.sign:
if self.sign == -1:
--- 2333,2337 ----
def __cmp__(self, other):
if self.exp != other.exp:
! raise ValueError("Operands not normalized: %r, %r" % (self, other))
if self.sign != other.sign:
if self.sign == -1:
***************
*** 2344,2348 ****
if len(int1) < len(int2):
return direction * -1
! for i in range(len(int1)):
if int1[i] > int2[i]:
return direction * 1
--- 2349,2353 ----
if len(int1) < len(int2):
return direction * -1
! for i in xrange(len(int1)):
if int1[i] > int2[i]:
return direction * 1
***************
*** 2374,2378 ****
carry = 0
! for x in range(len(list)):
self.int[x] -= list[x] + carry
if self.int[x] < 0:
--- 2379,2383 ----
carry = 0
! for x in xrange(len(list)):
self.int[x] -= list[x] + carry
if self.int[x] < 0:
***************
*** 2631,2635 ****
m = _parser(s)
if m is None:
! raise ValueError("Can't parse as number: " + `s`)
if m.group('sign') == "-":
--- 2636,2640 ----
m = _parser(s)
if m is None:
! raise ValueError("Can't parse as number: %r" % s)
if m.group('sign') == "-":
***************
*** 2653,2663 ****
fracpart = ""
! nfrac = len(fracpart)
! exp = exp - nfrac
mantissa = intpart + fracpart
! tmp = []
! for digit in mantissa:
! tmp.append(int(digit))
backup = tmp
while tmp and tmp[0] == 0:
--- 2658,2665 ----
fracpart = ""
! exp -= len(fracpart)
mantissa = intpart + fracpart
! tmp = map(int, mantissa)
backup = tmp
while tmp and tmp[0] == 0:
***************
*** 2709,2715 ****
assert digit >> CHUNK == 0
top = (top << CHUNK) | digit
! f = f - digit
assert 0.0 <= f < 1.0
! e = e - CHUNK
assert top > 0
--- 2711,2717 ----
assert digit >> CHUNK == 0
top = (top << CHUNK) | digit
! f -= digit
assert 0.0 <= f < 1.0
! e -= CHUNK
assert top > 0
***************
*** 2719,2728 ****
# be removed without changing the result).
while e < 0 and top & 1 == 0:
! top = top >> 1
! e = e + 1
# Transform this into an equal value top' * 10**e'.
if e > 0:
! top = top << e
e = 0
elif e < 0:
--- 2721,2730 ----
# be removed without changing the result).
while e < 0 and top & 1 == 0:
! top >>= 1
! e += 1
# Transform this into an equal value top' * 10**e'.
if e > 0:
! top <<= e
e = 0
elif e < 0:
***************
*** 2738,2742 ****
break
top = newtop
! e = e + 1
return "%s%se%d" % (sign, str(top), e)
--- 2740,2744 ----
break
top = newtop
! e += 1
return "%s%se%d" % (sign, str(top), e)
More information about the Python-checkins
mailing list