[Python-checkins] python/nondist/sandbox/decimal Decimal.py, 1.22, 1.23

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Fri Jun 25 02:17:05 EDT 2004


Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29145
Modified Files:
	Decimal.py 
Log Message:
Touchups (round 1):
* Normalize whitespace
* Fixup broken doctest in the module docstring
* Add a more informative error message for Decimal(3.14)
* Document from_float as a class method
* Rework the addition logic for speed/clarity
* Simplify and speedup zero checks
Index: Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -d -r1.22 -r1.23
*** Decimal.py	25 Jun 2004 01:29:29 -0000	1.22
--- Decimal.py	25 Jun 2004 06:17:02 -0000	1.23
***************
*** 26,30 ****
 Decimal("1")
 >>> Decimal("-.0123")
! Decimal("-.0123")
 >>> Decimal(123456)
 Decimal("123456")
--- 26,30 ----
 Decimal("1")
 >>> Decimal("-.0123")
! Decimal("-0.0123")
 >>> Decimal(123456)
 Decimal("123456")
***************
*** 133,137 ****
 import copy
 import math
! from operator import xor
 
 #Capitals: 1E+10, not 1e10
--- 133,138 ----
 import copy
 import math
! import operator
! xor = operator.xor
 
 #Capitals: 1E+10, not 1e10
***************
*** 490,497 ****
 return
 
 raise TypeError("Can't convert %r" % value)
 
 def from_float(cls, value, positions=None):
! """Static function that creates Decimal from float
 
 value must be float
--- 491,502 ----
 return
 
+ if isinstance(value, float):
+ raise TypeError("Can't convert " + repr(value) +
+ ". Try Decimal.from_float() instead.")
+ 
 raise TypeError("Can't convert %r" % value)
 
 def from_float(cls, value, positions=None):
! """Class method that creates Decimal from float
 
 value must be float
***************
*** 605,612 ****
 if isinstance(self._exp, str):
 return 1
! for digit in self._int:
! if digit != 0:
! return 1
! return 0
 
 def __cmp__(self, other, context=None):
--- 610,614 ----
 if isinstance(self._exp, str):
 return 1
! return self._int != (0,)*len(self._int)
 
 def __cmp__(self, other, context=None):
***************
*** 947,971 ****
 #Now, op1 > abs(op2) > 0
 
- carry, loan = 0, 0
 op1.int.reverse()
 op2.int.reverse()
! result.int = [0]*len(op1.int)
! for i in range(len(op1.int)):
! tmp = op1.int[i] + (op2.sign * op2.int[i]) + carry - loan
! carry, loan = (0, 0)
! if tmp > 9:
! carry = 1
! tmp = tmp - 10
! if tmp < 0:
! loan = 1
! tmp = tmp + 10
! result.int[i] = tmp
! if carry:
! result.int.append(1)
! if loan:
! raise "What are we doing here?"
! while result.int[-1] == 0:
! result.int.pop()
! result.int.reverse()
 result.exp = op1.exp
 ans = Decimal(result)
--- 949,983 ----
 #Now, op1 > abs(op2) > 0
 
 op1.int.reverse()
 op2.int.reverse()
! 
! if op2.sign == 1:
! result.int = resultint = map(operator.add, op1.int, op2.int)
! carry = 0
! for i in xrange(len(op1.int)): 
! tmp = resultint[i] + carry
! carry = 0
! if tmp > 9:
! carry = 1
! tmp -= 10
! resultint[i] = tmp
! if carry:
! resultint.append(1) 
! else:
! result.int = resultint = map(operator.sub, op1.int, op2.int)
! loan = 0
! for i in xrange(len(op1.int)):
! tmp = resultint[i] - loan
! loan = 0
! if tmp < 0:
! loan = 1
! tmp += 10
! resultint[i] = tmp
! assert not loan
! 
! while resultint[-1] == 0:
! resultint.pop()
! resultint.reverse()
! 
 result.exp = op1.exp
 ans = Decimal(result)
***************
*** 1573,1582 ****
 
 #OK, but maybe all the lost digits are 0.
! all0 = 1
! for digit in self._int[expdiff:]:
! if digit != 0:
! all0 = 0
! break
! if all0:
 ans = Decimal( (temp._sign, temp._int[:prec], temp._exp - expdiff))
 #Rounded, but not Inexact
--- 1585,1590 ----
 
 #OK, but maybe all the lost digits are 0.
! lostdigits = self._int[expdiff:]
! if lostdigits == (0,) * len(lostdigits):
 ans = Decimal( (temp._sign, temp._int[:prec], temp._exp - expdiff))
 #Rounded, but not Inexact


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /