[Python-checkins] python/nondist/sandbox/decimal Decimal.py, 1.32, 1.33

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Wed Jun 30 14:49:10 EDT 2004


Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26046
Modified Files:
	Decimal.py 
Log Message:
* Part III: docstrings and doctests
* Localize some inner loop variables
Index: Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v
retrieving revision 1.32
retrieving revision 1.33
diff -C2 -d -r1.32 -r1.33
*** Decimal.py	30 Jun 2004 08:16:42 -0000	1.32
--- Decimal.py	30 Jun 2004 18:49:07 -0000	1.33
***************
*** 1038,1041 ****
--- 1038,1042 ----
 op1, op2 = op2, op1
 
+ _divmod = divmod
 accumulator = [0]*(len(self._int) + len(other._int))
 for i in xrange(len(op2)):
***************
*** 1045,1049 ****
 carry = 0
 for j in xrange(len(op1)):
! carry, accumulator[i+j] = divmod( mult * op1[j] + carry
 + accumulator[i+j], 10)
 
--- 1046,1050 ----
 carry = 0
 for j in xrange(len(op1)):
! carry, accumulator[i+j] = _divmod( mult * op1[j] + carry
 + accumulator[i+j], 10)
 
***************
*** 2204,2214 ****
--- 2205,2267 ----
 #Methods
 def abs(self, a):
+ """Returns the absolute value of the operand.
+ 
+ If the operand is negative, the result is the same as using the minus
+ operation on the operand. Otherwise, the result is the same as using
+ the plus operation on the operand.
+ 
+ >>> Context(prec=9).abs(Decimal('2.1'))
+ Decimal("2.1")
+ >>> Context(prec=9).abs(Decimal('-100'))
+ Decimal("100")
+ >>> Context(prec=9).abs(Decimal('101.5'))
+ Decimal("101.5")
+ >>> Context(prec=9).abs(Decimal('-101.5'))
+ Decimal("101.5")
+ """
 return a.__abs__(context=self)
+ 
 def add(self, a, b):
+ """Return the sum of the two operands.
+ 
+ >>> Context(prec=9).add(Decimal('12'), Decimal('7.00'))
+ Decimal("19.00")
+ >>> Context(prec=9).add(Decimal('1E+2'), Decimal('1.01E+4'))
+ Decimal("1.02E+4")
+ """
 return a.__add__(b, context=self)
+ 
 def _apply(self, a):
 return str(a._fix(context=self))
+ 
 def compare(self, a, b):
+ """Compares values numerically.
+ 
+ If the signs of the operands differ, a value representing each operand
+ (’-1’ if the operand is less than zero, ’0’ if the operand is zero or
+ negative zero, or ’1’ if the operand is greater than zero) is used in
+ place of that operand for the comparison instead of the actual
+ operand.
+ 
+ The comparison is then effected by subtracting the second operand from
+ the first and then returning a value according to the result of the
+ subtraction: ’-1’ if the result is less than zero, ’0’ if the result is
+ zero or negative zero, or ’1’ if the result is greater than zero.
+ 
+ >>> Context(prec=9).compare(Decimal('2.1'), Decimal('3'))
+ Decimal("-1")
+ >>> Context(prec=9).compare(Decimal('2.1'), Decimal('2.1'))
+ Decimal("0")
+ >>> Context(prec=9).compare(Decimal('2.1'), Decimal('2.10'))
+ Decimal("0")
+ >>> Context(prec=9).compare(Decimal('3'), Decimal('2.1'))
+ Decimal("1")
+ >>> Context(prec=9).compare(Decimal('2.1'), Decimal('-3'))
+ Decimal("1")
+ >>> Context(prec=9).compare(Decimal('-3'), Decimal('2.1'))
+ Decimal("-1")
+ """
 return a.compare(b, context=self)
+ 
 def divide(self, a, b):
 """Decimal division in a specified context.
***************
*** 2251,2254 ****
--- 2304,2308 ----
 def divmod(self, a, b):
 return a.__divmod__(b, context=self)
+ 
 def max(self, a,b):
 """max compares two values numerically and returns the maximum.
***************
*** 2466,2483 ****
--- 2520,2617 ----
 
 def same_quantum(self, a, b):
+ """Returns True if the two operands have the same exponent.
+ 
+ The result is never affected by either the sign or the coefficient of
+ either operand.
+ 
+ >>> Context(prec=9).same_quantum(Decimal('2.17'), Decimal('0.001'))
+ False
+ >>> Context(prec=9).same_quantum(Decimal('2.17'), Decimal('0.01'))
+ True
+ >>> Context(prec=9).same_quantum(Decimal('2.17'), Decimal('1'))
+ False
+ >>> Context(prec=9).same_quantum(Decimal('Inf'), Decimal('-Inf'))
+ True
+ """
 return a.same_quantum(b)
+ 
 def sqrt(self, a):
+ """Returns the square root of a non-negative number to context precision.
+ 
+ If the result must be inexact, it is rounded using the round-half-even
+ algorithm.
+ 
+ >>> Context(prec=9).sqrt(Decimal('0'))
+ Decimal("0")
+ >>> Context(prec=9).sqrt(Decimal('-0'))
+ Decimal("-0")
+ >>> Context(prec=9).sqrt(Decimal('0.39'))
+ Decimal("0.624499800")
+ >>> Context(prec=9).sqrt(Decimal('100'))
+ Decimal("10")
+ >>> Context(prec=9).sqrt(Decimal('1'))
+ Decimal("1")
+ >>> Context(prec=9).sqrt(Decimal('1.0'))
+ Decimal("1.0")
+ >>> Context(prec=9).sqrt(Decimal('1.00'))
+ Decimal("1.0")
+ >>> Context(prec=9).sqrt(Decimal('7'))
+ Decimal("2.64575131")
+ >>> Context(prec=9).sqrt(Decimal('10'))
+ Decimal("3.16227766")
+ """
 return a.sqrt(context=self)
+ 
 def subtract(self, a, b):
+ """Return the sum of the two operands.
+ 
+ >>> Context(prec=9).subtract(Decimal('1.3'), Decimal('1.07'))
+ Decimal("0.23")
+ >>> Context(prec=9).subtract(Decimal('1.3'), Decimal('1.30'))
+ Decimal("0.00")
+ >>> Context(prec=9).subtract(Decimal('1.3'), Decimal('2.07'))
+ Decimal("-0.77")
+ """
 return a.__sub__(b, context=self)
+ 
 def to_eng_string(self, a):
 return a.to_eng_string(context=self)
+ 
 def to_sci_string(self, a):
 return a.__str__(context=self)
+ 
 def to_integral(self, a):
+ """Rounds to an integer.
+ 
+ When the operand has a negative exponent, the result is the same
+ as using the quantize() operation using the given operand as the
+ left-hand-operand, 1E+0 as the right-hand-operand, and the precision
+ of the operand as the precision setting, except that no flags will
+ be set. The rounding mode is taken from the context.
+ 
+ >>> Context(prec=9).to_integral(Decimal('2.1'))
+ Decimal("2")
+ >>> Context(prec=9).to_integral(Decimal('100'))
+ Decimal("100")
+ >>> Context(prec=9).to_integral(Decimal('100.0'))
+ Decimal("100")
+ >>> Context(prec=9).to_integral(Decimal('101.5'))
+ Decimal("102")
+ >>> Context(prec=9).to_integral(Decimal('-101.5'))
+ Decimal("-102")
+ >>> Context(prec=9).to_integral(Decimal('10E+5'))
+ Decimal("1.0E+6")
+ >>> Context(prec=9).to_integral(Decimal('7.89E+77'))
+ Decimal("7.89E+77")
+ >>> Context(prec=9).to_integral(Decimal('-Inf'))
+ Decimal("-Infinity")
+ """
 return a.to_integral(context=self)
 
 class _WorkRep(object):
 __slots__ = ('sign','int','exp')
+ # sign: -1 None 1
+ # int: list
+ # exp: None, int, or string
 
 def __init__(self, value=None):
***************
*** 2559,2582 ****
 """
 
! self.int.reverse()
 alist.reverse()
 
 carry = 0
 for x in xrange(len(alist)):
! self.int[x] -= alist[x] + carry
! if self.int[x] < 0:
 carry = 1
! self.int[x] += 10
 else:
 carry = 0
 if carry:
! self.int[x+1] -= 1
! last = len(self.int)-1
! while len(self.int) > 1 and self.int[last] == 0:
 last -= 1
 if last == 0:
 break
! self.int[last+1:]=[]
! self.int.reverse()
 alist.reverse()
 return
--- 2693,2717 ----
 """
 
! selfint = self.int
! selfint.reverse()
 alist.reverse()
 
 carry = 0
 for x in xrange(len(alist)):
! selfint[x] -= alist[x] + carry
! if selfint[x] < 0:
 carry = 1
! selfint[x] += 10
 else:
 carry = 0
 if carry:
! selfint[x+1] -= 1
! last = len(selfint)-1
! while len(selfint) > 1 and selfint[last] == 0:
 last -= 1
 if last == 0:
 break
! selfint[last+1:]=[]
! selfint.reverse()
 alist.reverse()
 return


More information about the Python-checkins mailing list

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