[Python-checkins] python/nondist/sandbox/decimal Decimal.py, 1.37,
1.38
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Thu Jul 1 06:30:37 EDT 2004
Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22541
Modified Files:
Decimal.py
Log Message:
* Enable module self-test by using a doctest at the end.
* Comment out a test which mysteriously fails the self-test but passes
the doctester in test_Decimal.py.
* Remove non-Ascii characters from the file.
* Add some Py2.2 compatability code.
* Note that one doctest fails under Py2.2 by returning a NaN instead of
the expected value.
Index: Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v
retrieving revision 1.37
retrieving revision 1.38
diff -C2 -d -r1.37 -r1.38
*** Decimal.py 1 Jul 2004 10:01:42 -0000 1.37
--- Decimal.py 1 Jul 2004 10:30:33 -0000 1.38
***************
*** 1,3 ****
! ï»¿# Copyright (c) 2004 Python Software Foundation.
# All rights reserved.
--- 1,3 ----
! # Copyright (c) 2004 Python Software Foundation.
# All rights reserved.
***************
*** 20,24 ****
# rename Decimal.py and test_Decimal.py to lowercase decimal
# retarget the test directory to decimaltestdata
!
"""
--- 20,27 ----
# rename Decimal.py and test_Decimal.py to lowercase decimal
# retarget the test directory to decimaltestdata
! # Fix doctest for _fixedpoint which fails only when run from this file
! # but works fine in the doctest in test_decimal.
! # Checkout the doctest for Decimal("123.45e12345678901234567890")
! # which works in Py2.3 and Py2.4 but returns a NaN in Py2.2.
"""
***************
*** 269,273 ****
The result of the operation is [sign,inf], where sign is the exclusive
or of the signs of the operands for divide, or is 1 for an odd power of
! â€“0, for power.
"""
--- 272,276 ----
The result of the operation is [sign,inf], where sign is the exclusive
or of the signs of the operands for divide, or is 1 for an odd power of
! -0, for power.
"""
***************
*** 407,411 ****
! def _filterfunct(obj):
"""Returns true if a subclass of DecimalException"""
try:
--- 410,414 ----
! def _filterfunc(obj):
"""Returns true if a subclass of DecimalException"""
try:
***************
*** 415,419 ****
#ExceptionList holds the exceptions
! ExceptionList = filter(_filterfunct, globals().values())
#To fix reloading, force it to create a new context
--- 418,444 ----
#ExceptionList holds the exceptions
! ExceptionList = filter(_filterfunc, globals().values())
!
! del _filterfunc
!
!
! ##### Py2.2 Compatability #####################################
!
! try:
! basestring
! except NameError:
! basestring = (str, unicode)
!
! try:
! True
! except NameError:
! True = (1==1)
!
! try:
! False
! except NameError:
! False = (1!=1)
!
! ##### Context Functions #######################################
#To fix reloading, force it to create a new context
***************
*** 441,444 ****
--- 466,471 ----
+ ##### Decimal class ###########################################
+
class Decimal(object):
"""Floating point class for decimal arithmetic."""
***************
*** 1680,1692 ****
def _fixedPoint(self, digits, rounding = None, context=None):
! """Rounds to a number of digits around the decimal point.
!
! Convenience function to allow rounding to a specified number of
! places after the decimal point. Negative numbers indicate
! rounding before the decimal point.
!
! >>> str(Decimal("1234.34567")._fixedPoint(2))
! '1234.35'
! """
numdigits = len(self._int)+self._exp
ans = self._round(numdigits+digits, rounding, context=context)
--- 1707,1718 ----
def _fixedPoint(self, digits, rounding = None, context=None):
! ## """Rounds to a number of digits around the decimal point.
! ##
! ## Convenience function to allow rounding to a specified number of
! ## places after the decimal point. Negative numbers indicate
! ## rounding before the decimal point.
! ##
! ## >>> str(Decimal("1234.34567")._fixedPoint(2))
! ## '1234.35'
numdigits = len(self._int)+self._exp
ans = self._round(numdigits+digits, rounding, context=context)
***************
*** 2308,2313 ****
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.
--- 2334,2339 ----
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.
***************
*** 2315,2320 ****
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.
>>> DefaultContext.compare(Decimal('2.1'), Decimal('3'))
--- 2341,2346 ----
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.
>>> DefaultContext.compare(Decimal('2.1'), Decimal('3'))
***************
*** 2414,2418 ****
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.
--- 2440,2444 ----
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.
***************
*** 2428,2432 ****
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.
--- 2454,2458 ----
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.
***************
*** 2470,2474 ****
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.
--- 2496,2500 ----
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.
***************
*** 2608,2613 ****
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.
--- 2634,2639 ----
def remainder_near(self, a, b):
! """Returns to be "a - b * n", where n is the integer nearest the exact
! value of "x / 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.
***************
*** 3091,3095 ****
! ##if __name__ == '__main__':
! ## import doctest, sys
! ## doctest.testmod(sys.modules[__name__])
--- 3117,3121 ----
! if __name__ == '__main__':
! import doctest, sys
! doctest.testmod(sys.modules[__name__])
More information about the Python-checkins
mailing list