[Python-checkins] python/nondist/sandbox/decimal Decimal.py, 1.26,
1.27 test_Decimal.py, 1.21, 1.22
rhettinger at users.sourceforge.net
rhettinger at users.sourceforge.net
Tue Jun 29 21:06:05 EDT 2004
Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv864
Modified Files:
Decimal.py test_Decimal.py
Log Message:
Neaten up the code a bit
Index: Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v
retrieving revision 1.26
retrieving revision 1.27
diff -C2 -d -r1.26 -r1.27
*** Decimal.py 29 Jun 2004 20:49:50 -0000 1.26
--- Decimal.py 30 Jun 2004 01:06:02 -0000 1.27
***************
*** 15,18 ****
--- 15,19 ----
# Add an __all__ attribute
# Improve docstrings and add more doctests
+ # Improve the Context API
"""
***************
*** 545,550 ****
self._sign, self._int, self._exp = _string2exact(value)
except ValueError:
! self._sign, self._int, self._exp = \
! context._raise_error(ConversionSyntax)
return
--- 546,550 ----
self._sign, self._int, self._exp = _string2exact(value)
except ValueError:
! self._sign, self._int, self._exp = context._raise_error(ConversionSyntax)
return
***************
*** 2572,2578 ****
NaN = Decimal('NaN')
- # Code ripped more-or-less directly from FixedPoint.py
- # _floatToString modified by Tim Peters to give exact results
-
# crud for parsing strings
import re
--- 2572,2575 ----
***************
*** 2604,2608 ****
m = _parser(s)
if m is None:
! raise ValueError("Can't parse as number: %r" % s)
if m.group('sign') == "-":
--- 2601,2605 ----
m = _parser(s)
if m is None:
! raise ValueError("invalid literal for Decimal: %r" % s)
if m.group('sign') == "-":
***************
*** 2642,2713 ****
return (sign, mantissa, exp)
-
-
- def _floatToString(x):
- """Return float x as exact decimal string.
-
- The string is of the form:
- "-", if and only if x is < 0.
- One or more decimal digits. The last digit is not 0 unless x is 0.
- "e"
- The exponent, a (possibly signed) integer
- """
-
- import math
-
- if x == 0:
- return "0e0"
-
- sign = ""
- if x < 0:
- sign = "-"
- x = -x
-
- f, e = math.frexp(x)
- assert 0.5 <= f < 1.0
- # x = f * 2**e exactly
-
- # Suck up CHUNK bits at a time; 28 is enough so that we suck
- # up all bits in 2 iterations for all known binary double-
- # precision formats, and small enough to fit in an int.
- CHUNK = 28
- top = 0L
- # invariant: x = (top + f) * 2**e exactly
- while f:
- f = math.ldexp(f, CHUNK)
- digit = int(f)
- assert digit >> CHUNK == 0
- top = (top << CHUNK) | digit
- f -= digit
- assert 0.0 <= f < 1.0
- e -= CHUNK
-
- assert top > 0
-
- # Now x = top * 2**e exactly. Get rid of trailing 0 bits if e < 0
- # (purely to increase efficiency a little later -- this loop can
- # be removed without changing the result).
- while e < 0 and top & 1 == 0:
- top >>= 1
- e += 1
-
- # Transform this into an equal value top' * 10**e'.
- if e > 0:
- top <<= e
- e = 0
- elif e < 0:
- # Exact is top/2**-e. Multiply top and bottom by 5**-e to
- # get top*5**-e/10**-e = top*5**-e * 10**e
- top = top * 5**-e
-
- # Nuke trailing (decimal) zeroes.
- while 1:
- assert top > 0
- newtop, rem = divmod(top, 10)
- if rem:
- break
- top = newtop
- e += 1
-
- return "%s%se%d" % (sign, str(top), e)
-
--- 2639,2640 ----
Index: test_Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/test_Decimal.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** test_Decimal.py 29 Jun 2004 20:49:51 -0000 1.21
--- test_Decimal.py 30 Jun 2004 01:06:02 -0000 1.22
***************
*** 15,18 ****
--- 15,22 ----
test the pythonic behaviour according to PEP 327.
+ Cowlishaw's tests can be downloaded from:
+
+ www2.hursley.ibm.com/decimal/dectest.zip
+
This test module can be called from command line with one parameter (Arithmetic
or Behaviour) to test each part, or without parameter to test both parts. If
***************
*** 283,416 ****
def test_abs(self):
- """Tests the Decimal class on Cowlishaw's abs tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'abs' + '.decTest')
def test_add(self):
- """Tests the Decimal class on Cowlishaw's add tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'add' + '.decTest')
def test_base(self):
- """Tests the Decimal class on Cowlishaw's base tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'base' + '.decTest')
def test_clamp(self):
- """Tests the Decimal class on Cowlishaw's clamp tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'clamp' + '.decTest')
def test_compare(self):
- """Tests the Decimal class on Cowlishaw's compare tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'compare' + '.decTest')
def test_divide(self):
- """Tests the Decimal class on Cowlishaw's divide tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'divide' + '.decTest')
def test_divideint(self):
- """Tests the Decimal class on Cowlishaw's divideint tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'divideint' + '.decTest')
def test_inexact(self):
- """Tests the Decimal class on Cowlishaw's inexact tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'inexact' + '.decTest')
def test_max(self):
- """Tests the Decimal class on Cowlishaw's max tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'max' + '.decTest')
def test_min(self):
- """Tests the Decimal class on Cowlishaw's min tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'min' + '.decTest')
def test_minus(self):
- """Tests the Decimal class on Cowlishaw's minus tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'minus' + '.decTest')
def test_multiply(self):
- """Tests the Decimal class on Cowlishaw's multiply tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir+'multiply'+'.decTest')
def test_normalize(self):
- """Tests the Decimal class on Cowlishaw's normalize tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'normalize' + '.decTest')
def test_plus(self):
- """Tests the Decimal class on Cowlishaw's plus tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'plus' + '.decTest')
def test_power(self):
- """Tests the Decimal class on Cowlishaw's power tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'power' + '.decTest')
def test_quantize(self):
- """Tests the Decimal class on Cowlishaw's quantize tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'quantize' + '.decTest')
def test_randomBound32(self):
- """Tests the Decimal class on Cowlishaw's randomBound32 tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'randomBound32' + '.decTest')
def test_randoms(self):
- """Tests the Decimal class on Cowlishaw's randoms tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'randoms' + '.decTest')
def test_remainder(self):
- """Tests the Decimal class on Cowlishaw's remainder tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'remainder' + '.decTest')
--- 287,344 ----
***************
*** 423,469 ****
def test_rounding(self):
- """Tests the Decimal class on Cowlishaw's rounding tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'rounding' + '.decTest')
def test_samequantum(self):
- """Tests the Decimal class on Cowlishaw's samequantum tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'samequantum' + '.decTest')
def test_squareroot(self):
- """Tests the Decimal class on Cowlishaw's squareroot tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'squareroot' + '.decTest')
def test_subtract(self):
- """Tests the Decimal class on Cowlishaw's subtract tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'subtract' + '.decTest')
def test_tointegral(self):
- """Tests the Decimal class on Cowlishaw's tointegral tests.
-
- See www2.hursley.ibm.com/decimal/dectest.zip to download the suite.
- """
self.eval_file(dir + 'tointegral' + '.decTest')
! #
# The following classes test the behaviour of Decimal according to PEP 327
! #
! # - Explicit Construction
! # - Implicit Construction
! # - Arithmetic Operators
! # - Use of Context
! # - Usability
! #
class DecimalExplicitConstructionTest(unittest.TestCase):
--- 351,371 ----
def test_rounding(self):
self.eval_file(dir + 'rounding' + '.decTest')
def test_samequantum(self):
self.eval_file(dir + 'samequantum' + '.decTest')
def test_squareroot(self):
self.eval_file(dir + 'squareroot' + '.decTest')
def test_subtract(self):
self.eval_file(dir + 'subtract' + '.decTest')
def test_tointegral(self):
self.eval_file(dir + 'tointegral' + '.decTest')
!
# The following classes test the behaviour of Decimal according to PEP 327
!
class DecimalExplicitConstructionTest(unittest.TestCase):
***************
*** 501,523 ****
#empty
! d = Decimal('')
! self.assertEqual(str(d), 'NaN')
#int
! d = Decimal('45')
! self.assertEqual(str(d), '45')
#float
! d = Decimal('45.34')
! self.assertEqual(str(d), '45.34')
#engineer notation
! d = Decimal('45e2')
! self.assertEqual(str(d), '4.5E+3')
- # XXX this should raise a ValueError for an invalid literal
#just not a number
! d = Decimal('ugly')
! self.assertEqual(str(d), 'NaN')
def test_from_tuples(self):
--- 403,419 ----
#empty
! self.assertEqual(str(Decimal('')), 'NaN')
#int
! self.assertEqual(str(Decimal('45')), '45')
#float
! self.assertEqual(str(Decimal('45.34')), '45.34')
#engineer notation
! self.assertEqual(str(Decimal('45e2')), '4.5E+3')
#just not a number
! self.assertEqual(str(Decimal('ugly')), 'NaN')
def test_from_tuples(self):
More information about the Python-checkins
mailing list