python/nondist/sandbox/decimal Decimal.py, 1.29, 1.30 test_Decimal.py, 1.23, 1.24

Update of /cvsroot/python/python/nondist/sandbox/decimal In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22565 Modified Files: Decimal.py test_Decimal.py Log Message: * Part II: docstrings and doctests * removed Lost Digits which was unused Index: Decimal.py =================================================================== RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v retrieving revision 1.29 retrieving revision 1.30 diff -C2 -d -r1.29 -r1.30 *** Decimal.py 30 Jun 2004 05:38:25 -0000 1.29 --- Decimal.py 30 Jun 2004 07:19:21 -0000 1.30 *************** *** 297,304 **** return NaN - class LostDigits(DecimalException): - """Actually, this is not used for the extended version.""" - pass - class Rounded(DecimalException): """Number got rounded (not necessarily changed during rounding)""" --- 297,300 ---- *************** *** 2363,2372 **** def power(self, a, b, modulo=None): return a.__pow__(b, modulo, context=self) def remainder(self, a, b): return a.__mod__(b, context=self) def remainder_near(self, a, b): return a.remainder_near(b, context=self) ! def quantize(self, a, b): ! return a.quantize(b, context=self) def same_quantum(self, a, b): return a.same_quantum(b) --- 2359,2468 ---- def power(self, a, b, modulo=None): return a.__pow__(b, modulo, context=self) + + def quantize(self, a, b): + """Returns a value equal to 'a' (rounded) and having the exponent of 'b'. + + The coefficient of the result is derived from that of the left-hand + operand. It may be rounded using the current rounding setting (if the + exponent is being increased), multiplied by a positive power of ten (if + the exponent is being decreased), or is unchanged (if the exponent is + already equal to that of the right-hand operand). + + Unlike other operations, if the length of the coefficient after the + quantize operation would be greater than precision then an Invalid + operation condition is raised. This guarantees that, unless there is an + error condition, the exponent of the result of a quantize is always + equal to that of the right-hand operand. + + Also unlike other operations, quantize will never raise Underflow, even + if the result is subnormal and inexact. + + >>> Context(prec=9).quantize(Decimal('2.17'), Decimal('0.001')) + Decimal("2.170") + >>> Context(prec=9).quantize(Decimal('2.17'), Decimal('0.01')) + Decimal("2.17") + >>> Context(prec=9).quantize(Decimal('2.17'), Decimal('0.1')) + Decimal("2.2") + >>> Context(prec=9).quantize(Decimal('2.17'), Decimal('1e+0')) + Decimal("2") + >>> Context(prec=9).quantize(Decimal('2.17'), Decimal('1e+1')) + Decimal("0E+1") + >>> Context(prec=9).quantize(Decimal('-Inf'), Decimal('Infinity')) + Decimal("-Infinity") + >>> Context(prec=9).quantize(Decimal('2'), Decimal('Infinity')) + Decimal("NaN") + >>> Context(prec=9).quantize(Decimal('-0.1'), Decimal('1')) + Decimal("-0") + >>> Context(prec=9).quantize(Decimal('-0'), Decimal('1e+5')) + Decimal("-0E+5") + >>> Context(prec=9).quantize(Decimal('+35236450.6'), Decimal('1e-2')) + Decimal("NaN") + >>> Context(prec=9).quantize(Decimal('-35236450.6'), Decimal('1e-2')) + Decimal("NaN") + >>> Context(prec=9).quantize(Decimal('217'), Decimal('1e-1')) + Decimal("217.0") + >>> Context(prec=9).quantize(Decimal('217'), Decimal('1e-0')) + Decimal("217") + >>> Context(prec=9).quantize(Decimal('217'), Decimal('1e+1')) + Decimal("2.2E+2") + >>> Context(prec=9).quantize(Decimal('217'), Decimal('1e+2')) + Decimal("2E+2") + """ + return a.quantize(b, context=self) + def remainder(self, a, b): + """Returns the remainder from integer division. + + The result is the residue of the dividend after the operation of + calculating integer division as described for divide-integer, rounded to + precision digits if necessary. The sign of the result, if non-zero, is + the same as that of the original dividend. + + This operation will fail under the same conditions as integer division + (that is, if integer division on the same two operands would fail, the + remainder cannot be calculated). + + >>> Context(prec=9).remainder(Decimal('2.1'), Decimal('3')) + Decimal("2.1") + >>> Context(prec=9).remainder(Decimal('10'), Decimal('3')) + Decimal("1") + >>> Context(prec=9).remainder(Decimal('-10'), Decimal('3')) + Decimal("-1") + >>> Context(prec=9).remainder(Decimal('10.2'), Decimal('1')) + Decimal("0.2") + >>> Context(prec=9).remainder(Decimal('10'), Decimal('0.3')) + Decimal("0.1") + >>> Context(prec=9).remainder(Decimal('3.6'), Decimal('1.3')) + Decimal("1.0") + """ return a.__mod__(b, context=self) + def remainder_near(self, a, b): + """Returns to be a – b × n, where n is the integer nearest the exact + value of a ÷ b (if two integers are equally near then the even one + is chosen). If the result is equal to 0 then its sign will be the + sign of a. + + This operation will fail under the same conditions as integer division + (that is, if integer division on the same two operands would fail, the + remainder cannot be calculated). + + >>> Context(prec=9).remainder_near(Decimal('2.1'), Decimal('3')) + Decimal("-0.9") + >>> Context(prec=9).remainder_near(Decimal('10'), Decimal('6')) + Decimal("-2") + >>> Context(prec=9).remainder_near(Decimal('10'), Decimal('3')) + Decimal("1") + >>> Context(prec=9).remainder_near(Decimal('-10'), Decimal('3')) + Decimal("-1") + >>> Context(prec=9).remainder_near(Decimal('10.2'), Decimal('1')) + Decimal("0.2") + >>> Context(prec=9).remainder_near(Decimal('10'), Decimal('0.3')) + Decimal("0.1") + >>> Context(prec=9).remainder_near(Decimal('3.6'), Decimal('1.3')) + Decimal("-0.3") + """ return a.remainder_near(b, context=self) ! def same_quantum(self, a, b): return a.same_quantum(b) *************** *** 2760,2761 **** --- 2856,2862 ---- return (sign, mantissa, exp) + + + ##if __name__ == '__main__': + ## import doctest, sys + ## doctest.testmod(sys.modules[__name__]) Index: test_Decimal.py =================================================================== RCS file: /cvsroot/python/python/nondist/sandbox/decimal/test_Decimal.py,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** test_Decimal.py 30 Jun 2004 04:22:54 -0000 1.23 --- test_Decimal.py 30 Jun 2004 07:19:21 -0000 1.24 *************** *** 56,60 **** 'invalid_context' : InvalidContext, 'invalid_operation' : InvalidOperation, - 'lost_digits' : LostDigits, 'overflow' : Overflow, 'rounded' : Rounded, --- 56,59 ---- *************** *** 344,351 **** def test_remainderNear(self): - """Tests the Decimal class on Cowlishaw's remainderNear tests. - - See www2.hursley.ibm.com/decimal/dectest.zip to download the suite. - """ self.eval_file(dir + 'remainderNear' + '.decTest') --- 343,346 ----
participants (1)
-
rhettinger@users.sourceforge.net