python/nondist/sandbox/decimal Decimal.py, 1.28, 1.29

Update of /cvsroot/python/python/nondist/sandbox/decimal In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7869 Modified Files: Decimal.py Log Message: Part I: Add docstrings and doctests adapted from the spec. Index: Decimal.py =================================================================== RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** Decimal.py 30 Jun 2004 04:22:54 -0000 1.28 --- Decimal.py 30 Jun 2004 05:38:25 -0000 1.29 *************** *** 1,3 **** ! # Copyright (c) 2004 Python Software Foundation. # All rights reserved. --- 1,3 ---- ! # Copyright (c) 2004 Python Software Foundation. # All rights reserved. *************** *** 2216,2236 **** --- 2216,2364 ---- return a.compare(b, context=self) def divide(self, a, b): + """Decimal division in a specified context. + + >>> Context(prec=9).divide(Decimal('1'), Decimal('3')) + Decimal("0.333333333") + >>> Context(prec=9).divide(Decimal('2'), Decimal('3')) + Decimal("0.666666667") + >>> Context(prec=9).divide(Decimal('5'), Decimal('2')) + Decimal("2.5") + >>> Context(prec=9).divide(Decimal('1'), Decimal('10')) + Decimal("0.1") + >>> Context(prec=9).divide(Decimal('12'), Decimal('12')) + Decimal("1") + >>> Context(prec=9).divide(Decimal('8.00'), Decimal('2')) + Decimal("4.00") + >>> Context(prec=9).divide(Decimal('2.400'), Decimal('2.0')) + Decimal("1.20") + >>> Context(prec=9).divide(Decimal('1000'), Decimal('100')) + Decimal("10") + >>> Context(prec=9).divide(Decimal('1000'), Decimal('1')) + Decimal("1000") + >>> Context(prec=9).divide(Decimal('2.40E+6'), Decimal('2')) + Decimal("1.20E+6") + """ return a.__div__(b, context=self) + def divide_int(self, a, b): + """Divides two numbers and returns the integer part of the result. + + >>> Context(prec=9).divide_int(Decimal('2'), Decimal('3')) + Decimal("0") + >>> Context(prec=9).divide_int(Decimal('10'), Decimal('3')) + Decimal("3") + >>> Context(prec=9).divide_int(Decimal('1'), Decimal('0.3')) + Decimal("3") + """ return a.__floordiv__(b, context=self) + 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. + + If either operand is a NaN then the general rules apply. + Otherwise, the operands are compared as as though by the compare + operation. If they are numerically equal then the left-hand operand + is chosen as the result. Otherwise the maximum (closer to positive + infinity) of the two operands is chosen as the result. + + >>> Context(prec=9).max(Decimal('3'), Decimal('2')) + Decimal("3") + >>> Context(prec=9).max(Decimal('-10'), Decimal('3')) + Decimal("3") + >>> Context(prec=9).max(Decimal('1.0'), Decimal('1')) + Decimal("1.0") + """ return a.max(b, context=self) + def min(self, a,b): + """min compares two values numerically and returns the minimum. + + If either operand is a NaN then the general rules apply. + Otherwise, the operands are compared as as though by the compare + operation. If they are numerically equal then the left-hand operand + is chosen as the result. Otherwise the minimum (closer to negative + infinity) of the two operands is chosen as the result. + + >>> Context(prec=9).min(Decimal('3'), Decimal('2')) + Decimal("2") + >>> Context(prec=9).min(Decimal('-10'), Decimal('3')) + Decimal("-10") + >>> Context(prec=9).min(Decimal('1.0'), Decimal('1')) + Decimal("1.0") + """ return a.min(b, context=self) + def minus(self, a): + """Minus corresponds to unary prefix minus in Python. + + The operation is evaluated using the same rules as subtract; the + operation minus(a) is calculated as subtract(’0’, a) where the ’0’ + has the same exponent as the operand. + + >>> Context(prec=9).minus(Decimal('1.3')) + Decimal("-1.3") + >>> Context(prec=9).minus(Decimal('-1.3')) + Decimal("1.3") + """ return a.__neg__(context=self) + def multiply(self, a, b): + """multiply multiplies two operands. + + If either operand is a special value then the general rules apply. + Otherwise, the operands are multiplied together (‘long multiplication’), + resulting in a number which may be as long as the sum of the lengths + of the two operands. + + >>> Context(prec=9).multiply(Decimal('1.20'), Decimal('3')) + Decimal("3.60") + >>> Context(prec=9).multiply(Decimal('7'), Decimal('3')) + Decimal("21") + >>> Context(prec=9).multiply(Decimal('0.9'), Decimal('0.8')) + Decimal("0.72") + >>> Context(prec=9).multiply(Decimal('0.9'), Decimal('-0')) + Decimal("-0.0") + >>> Context(prec=9).multiply(Decimal('654321'), Decimal('654321')) + Decimal("4.28135971E+11") + """ return a.__mul__(b, context=self) + def normalize(self, a): + """normalize reduces its operand to its simplest form. + + Essentially a plus operation with all trailing zeros removed from the + result. + + >>> Context(prec=9).normalize(Decimal('2.1')) + Decimal("2.1") + >>> Context(prec=9).normalize(Decimal('-2.0')) + Decimal("-2") + >>> Context(prec=9).normalize(Decimal('1.200')) + Decimal("1.2") + >>> Context(prec=9).normalize(Decimal('-120')) + Decimal("-1.2E+2") + >>> Context(prec=9).normalize(Decimal('120.00')) + Decimal("1.2E+2") + >>> Context(prec=9).normalize(Decimal('0.00')) + Decimal("0") + """ return a.normalize(context=self) + def plus(self, a): + """Plus corresponds to unary prefix plus in Python. + + The operation is evaluated using the same rules as add; the + operation plus(a) is calculated as add(’0’, a) where the ’0’ + has the same exponent as the operand. + + >>> Context(prec=9).plus(Decimal('1.3')) + Decimal("1.3") + >>> Context(prec=9).plus(Decimal('-1.3')) + Decimal("-1.3") + """ return a.__pos__(context=self) + def power(self, a, b, modulo=None): return a.__pow__(b, modulo, context=self)
participants (1)
-
rhettinger@users.sourceforge.net