[Python-checkins] python/nondist/sandbox/decimal Decimal.py, 1.35, 1.36

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Thu Jul 1 04:21:31 EDT 2004


Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1300

Modified Files:
	Decimal.py 
Log Message:
* Have context doctests use the DefaultContext instead of creating new ones.
* Simplify the thicket of constants non specifically required by the spec.



Index: Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v
retrieving revision 1.35
retrieving revision 1.36
diff -C2 -d -r1.35 -r1.36
*** Decimal.py	1 Jul 2004 01:47:03 -0000	1.35
--- Decimal.py	1 Jul 2004 08:21:28 -0000	1.36
***************
*** 14,20 ****
  #    Consider having a SimpleDecimal subclass implementing X3.274 semantics
  #    Add an __all__ attribute
- #    Improve docstrings and add more doctests
  #    Improve the Context API
  #    Provide a clean way of attaching monetary format representations
  
  """
--- 14,20 ----
  #    Consider having a SimpleDecimal subclass implementing X3.274 semantics
  #    Add an __all__ attribute
  #    Improve the Context API
  #    Provide a clean way of attaching monetary format representations
+ #    Review all exposed constants for utility vs. namespace clutter
  
  """
***************
*** 128,169 ****
  xor = operator.xor
  
- #Capitals:  1E+10, not 1e10
- CAPITALS = 1
- 
- # Clamp:  When finishing operation, change exponents if too high:
- # maxExponent: 10
- # precision: 4
- # clamp: 1
- # add 0 1e10 -> 1.000e10
- 
- CLAMP = 0
- 
- # Default Context
- 
- BASIC_DEFAULT_CONTEXT = 'basic'
- 
- # Basic default context:
- #
- # flags:          all set to 0
- # trap-enablers:  inexact, rounded, and subnormal are set to 0;
- #                 all others are set to 1
- # precision:      is set to 9
- # rounding:       is set to round-half-up
- 
- EXTENDED_DEFAULT_CONTEXT = 'extended'
- 
- # Extended default context:
- # flags:          all set to 0
- # trap-enablers:  all set to 0 (IEEE 854 7)
- # precision:      is set to the designated single precision
- # rounding:       is set to round-half-even (IEEE 854 4.1)
- 
- DEFAULT_CONTEXT = EXTENDED_DEFAULT_CONTEXT
- 
  #Precision
  
! BASIC_DEFAULT_PRECISION = 9
! EXTENDED_DEFAULT_PRECISION = 9
! 
  DEFAULT_MAX_EXPONENT = 999999999
  DEFAULT_MIN_EXPONENT = -999999999
--- 128,135 ----
  xor = operator.xor
  
  #Precision
+ SINGLE_PRECISION = 9
  
! #Exponent Range
  DEFAULT_MAX_EXPONENT = 999999999
  DEFAULT_MIN_EXPONENT = -999999999
***************
*** 178,194 ****
  ROUND_HALF_DOWN = 'half_down'
  
- BASIC_DEFAULT_ROUNDING = ROUND_HALF_UP
- EXTENDED_DEFAULT_ROUNDING = ROUND_HALF_EVEN
- 
  #Rounding decision
- 
  NEVER_ROUND = 'never'    # Round in division (non-divmod), sqrt ONLY
  ALWAYS_ROUND = 'always'  # Every operation rounds at end.
  
- 
- BASIC_DEFAULT_ROUNDING_DECISION = ALWAYS_ROUND
- EXTENDED_DEFAULT_ROUNDING_DECISION = ALWAYS_ROUND
- 
- 
  #Errors
  
--- 144,151 ----
***************
*** 1442,1445 ****
--- 1399,1404 ----
      def __int__(self):
          """Converts self to a int, truncating if necessary."""
+         # XXX This should be implemented in terms of tested
+         # functions in the standard
          if self._isnan():
              context = getcontext()
***************
*** 1674,1677 ****
--- 1633,1637 ----
                      return tmp
          return tmp
+ 
      def _round_ceiling(self, prec, expdiff, context):
          """Rounds up (not away from 0 if negative.)"""
***************
*** 2144,2147 ****
--- 2104,2108 ----
      Decimal._pick_rounding_function[val] = name
  
+ DefaultLock = threading.Lock()
  
  class Context(object):
***************
*** 2158,2171 ****
               (Whether or not the trap_enabler is set)
               Should be reset by user of Decimal instance.
!     Emin -   Minimum exponent
!     Emax -   Maximum exponent
!     capitals -      If 1, 1*10^1 is printed as 1E+1.  If 0, printed as 1e1
      """
      def __init__(self, prec=None, rounding=None,
                   trap_enablers=None, flags=None,
                   _rounding_decision=None,
!                  Emin=None, Emax=None,
!                  capitals=None, _clamp=None,
!                  _ignored_flags=None):
          DefaultLock.acquire()
          for name, val in locals().items():
--- 2119,2135 ----
               (Whether or not the trap_enabler is set)
               Should be reset by user of Decimal instance.
!     Emin -   Minimum exponent (defaults to -999999999)
!     Emax -   Maximum exponent (defaults to 999999999)
!     capitals -      If 1, 1*10^1 is printed as 1E+1.
!                     If 0, printed as 1e1
!                     (Defaults to 1)
!     clamp - If 1, change exponents if too high (Default 0)
      """
      def __init__(self, prec=None, rounding=None,
                   trap_enablers=None, flags=None,
                   _rounding_decision=None,
!                  Emin=DEFAULT_MIN_EXPONENT, Emax=DEFAULT_MAX_EXPONENT,
!                  capitals=1, _clamp=0,
!                  _ignored_flags=[]):
          DefaultLock.acquire()
          for name, val in locals().items():
***************
*** 2285,2295 ****
          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")
          """
--- 2249,2259 ----
          the plus operation on the operand.
  
!         >>> DefaultContext.abs(Decimal('2.1'))
          Decimal("2.1")
!         >>> DefaultContext.abs(Decimal('-100'))
          Decimal("100")
!         >>> DefaultContext.abs(Decimal('101.5'))
          Decimal("101.5")
!         >>> DefaultContext.abs(Decimal('-101.5'))
          Decimal("101.5")
          """
***************
*** 2299,2305 ****
          """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")
          """
--- 2263,2269 ----
          """Return the sum of the two operands.
  
!         >>> DefaultContext.add(Decimal('12'), Decimal('7.00'))
          Decimal("19.00")
!         >>> DefaultContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
          Decimal("1.02E+4")
          """
***************
*** 2323,2337 ****
          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")
          """
--- 2287,2301 ----
          zero or negative zero, or ’1’ if the result is greater than zero.
  
!         >>> DefaultContext.compare(Decimal('2.1'), Decimal('3'))
          Decimal("-1")
!         >>> DefaultContext.compare(Decimal('2.1'), Decimal('2.1'))
          Decimal("0")
!         >>> DefaultContext.compare(Decimal('2.1'), Decimal('2.10'))
          Decimal("0")
!         >>> DefaultContext.compare(Decimal('3'), Decimal('2.1'))
          Decimal("1")
!         >>> DefaultContext.compare(Decimal('2.1'), Decimal('-3'))
          Decimal("1")
!         >>> DefaultContext.compare(Decimal('-3'), Decimal('2.1'))
          Decimal("-1")
          """
***************
*** 2341,2363 ****
          """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")
          """
--- 2305,2327 ----
          """Decimal division in a specified context.
  
!         >>> DefaultContext.divide(Decimal('1'), Decimal('3'))
          Decimal("0.333333333")
!         >>> DefaultContext.divide(Decimal('2'), Decimal('3'))
          Decimal("0.666666667")
!         >>> DefaultContext.divide(Decimal('5'), Decimal('2'))
          Decimal("2.5")
!         >>> DefaultContext.divide(Decimal('1'), Decimal('10'))
          Decimal("0.1")
!         >>> DefaultContext.divide(Decimal('12'), Decimal('12'))
          Decimal("1")
!         >>> DefaultContext.divide(Decimal('8.00'), Decimal('2'))
          Decimal("4.00")
!         >>> DefaultContext.divide(Decimal('2.400'), Decimal('2.0'))
          Decimal("1.20")
!         >>> DefaultContext.divide(Decimal('1000'), Decimal('100'))
          Decimal("10")
!         >>> DefaultContext.divide(Decimal('1000'), Decimal('1'))
          Decimal("1000")
!         >>> DefaultContext.divide(Decimal('2.40E+6'), Decimal('2'))
          Decimal("1.20E+6")
          """
***************
*** 2367,2375 ****
          """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")
          """
--- 2331,2339 ----
          """Divides two numbers and returns the integer part of the result.
  
!         >>> DefaultContext.divide_int(Decimal('2'), Decimal('3'))
          Decimal("0")
!         >>> DefaultContext.divide_int(Decimal('10'), Decimal('3'))
          Decimal("3")
!         >>> DefaultContext.divide_int(Decimal('1'), Decimal('0.3'))
          Decimal("3")
          """
***************
*** 2388,2396 ****
          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")
          """
--- 2352,2360 ----
          infinity) of the two operands is chosen as the result.
  
!         >>> DefaultContext.max(Decimal('3'), Decimal('2'))
          Decimal("3")
!         >>> DefaultContext.max(Decimal('-10'), Decimal('3'))
          Decimal("3")
!         >>> DefaultContext.max(Decimal('1.0'), Decimal('1'))
          Decimal("1.0")
          """
***************
*** 2406,2414 ****
          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")
          """
--- 2370,2378 ----
          infinity) of the two operands is chosen as the result.
  
!         >>> DefaultContext.min(Decimal('3'), Decimal('2'))
          Decimal("2")
!         >>> DefaultContext.min(Decimal('-10'), Decimal('3'))
          Decimal("-10")
!         >>> DefaultContext.min(Decimal('1.0'), Decimal('1'))
          Decimal("1.0")
          """
***************
*** 2422,2428 ****
          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")
          """
--- 2386,2392 ----
          has the same exponent as the operand.
  
!         >>> DefaultContext.minus(Decimal('1.3'))
          Decimal("-1.3")
!         >>> DefaultContext.minus(Decimal('-1.3'))
          Decimal("1.3")
          """
***************
*** 2437,2449 ****
          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")
          """
--- 2401,2413 ----
          of the two operands.
  
!         >>> DefaultContext.multiply(Decimal('1.20'), Decimal('3'))
          Decimal("3.60")
!         >>> DefaultContext.multiply(Decimal('7'), Decimal('3'))
          Decimal("21")
!         >>> DefaultContext.multiply(Decimal('0.9'), Decimal('0.8'))
          Decimal("0.72")
!         >>> DefaultContext.multiply(Decimal('0.9'), Decimal('-0'))
          Decimal("-0.0")
!         >>> DefaultContext.multiply(Decimal('654321'), Decimal('654321'))
          Decimal("4.28135971E+11")
          """
***************
*** 2456,2470 ****
          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")
          """
--- 2420,2434 ----
          result.
  
!         >>> DefaultContext.normalize(Decimal('2.1'))
          Decimal("2.1")
!         >>> DefaultContext.normalize(Decimal('-2.0'))
          Decimal("-2")
!         >>> DefaultContext.normalize(Decimal('1.200'))
          Decimal("1.2")
!         >>> DefaultContext.normalize(Decimal('-120'))
          Decimal("-1.2E+2")
!         >>> DefaultContext.normalize(Decimal('120.00'))
          Decimal("1.2E+2")
!         >>> DefaultContext.normalize(Decimal('0.00'))
          Decimal("0")
          """
***************
*** 2478,2484 ****
          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")
          """
--- 2442,2448 ----
          has the same exponent as the operand.
  
!         >>> DefaultContext.plus(Decimal('1.3'))
          Decimal("1.3")
!         >>> DefaultContext.plus(Decimal('-1.3'))
          Decimal("-1.3")
          """
***************
*** 2503,2533 ****
          continues.
  
!         >>> Context(prec=9).power(Decimal('2'), Decimal('3'))
          Decimal("8")
!         >>> Context(prec=9).power(Decimal('2'), Decimal('-3'))
          Decimal("0.125")
!         >>> Context(prec=9).power(Decimal('1.7'), Decimal('8'))
          Decimal("69.7575744")
!         >>> Context(prec=9).power(Decimal('Infinity'), Decimal('-2'))
          Decimal("0")
!         >>> Context(prec=9).power(Decimal('Infinity'), Decimal('-1'))
          Decimal("0")
!         >>> Context(prec=9).power(Decimal('Infinity'), Decimal('0'))
          Decimal("1")
!         >>> Context(prec=9).power(Decimal('Infinity'), Decimal('1'))
          Decimal("Infinity")
!         >>> Context(prec=9).power(Decimal('Infinity'), Decimal('2'))
          Decimal("Infinity")
!         >>> Context(prec=9).power(Decimal('-Infinity'), Decimal('-2'))
          Decimal("0")
!         >>> Context(prec=9).power(Decimal('-Infinity'), Decimal('-1'))
          Decimal("-0")
!         >>> Context(prec=9).power(Decimal('-Infinity'), Decimal('0'))
          Decimal("1")
!         >>> Context(prec=9).power(Decimal('-Infinity'), Decimal('1'))
          Decimal("-Infinity")
!         >>> Context(prec=9).power(Decimal('-Infinity'), Decimal('2'))
          Decimal("Infinity")
!         >>> Context(prec=9).power(Decimal('0'), Decimal('0'))
          Decimal("NaN")
          """
--- 2467,2497 ----
          continues.
  
!         >>> DefaultContext.power(Decimal('2'), Decimal('3'))
          Decimal("8")
!         >>> DefaultContext.power(Decimal('2'), Decimal('-3'))
          Decimal("0.125")
!         >>> DefaultContext.power(Decimal('1.7'), Decimal('8'))
          Decimal("69.7575744")
!         >>> DefaultContext.power(Decimal('Infinity'), Decimal('-2'))
          Decimal("0")
!         >>> DefaultContext.power(Decimal('Infinity'), Decimal('-1'))
          Decimal("0")
!         >>> DefaultContext.power(Decimal('Infinity'), Decimal('0'))
          Decimal("1")
!         >>> DefaultContext.power(Decimal('Infinity'), Decimal('1'))
          Decimal("Infinity")
!         >>> DefaultContext.power(Decimal('Infinity'), Decimal('2'))
          Decimal("Infinity")
!         >>> DefaultContext.power(Decimal('-Infinity'), Decimal('-2'))
          Decimal("0")
!         >>> DefaultContext.power(Decimal('-Infinity'), Decimal('-1'))
          Decimal("-0")
!         >>> DefaultContext.power(Decimal('-Infinity'), Decimal('0'))
          Decimal("1")
!         >>> DefaultContext.power(Decimal('-Infinity'), Decimal('1'))
          Decimal("-Infinity")
!         >>> DefaultContext.power(Decimal('-Infinity'), Decimal('2'))
          Decimal("Infinity")
!         >>> DefaultContext.power(Decimal('0'), Decimal('0'))
          Decimal("NaN")
          """
***************
*** 2552,2584 ****
          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")
          """
--- 2516,2548 ----
          if the result is subnormal and inexact.
  
!         >>> DefaultContext.quantize(Decimal('2.17'), Decimal('0.001'))
          Decimal("2.170")
!         >>> DefaultContext.quantize(Decimal('2.17'), Decimal('0.01'))
          Decimal("2.17")
!         >>> DefaultContext.quantize(Decimal('2.17'), Decimal('0.1'))
          Decimal("2.2")
!         >>> DefaultContext.quantize(Decimal('2.17'), Decimal('1e+0'))
          Decimal("2")
!         >>> DefaultContext.quantize(Decimal('2.17'), Decimal('1e+1'))
          Decimal("0E+1")
!         >>> DefaultContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
          Decimal("-Infinity")
!         >>> DefaultContext.quantize(Decimal('2'), Decimal('Infinity'))
          Decimal("NaN")
!         >>> DefaultContext.quantize(Decimal('-0.1'), Decimal('1'))
          Decimal("-0")
!         >>> DefaultContext.quantize(Decimal('-0'), Decimal('1e+5'))
          Decimal("-0E+5")
!         >>> DefaultContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
          Decimal("NaN")
!         >>> DefaultContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
          Decimal("NaN")
!         >>> DefaultContext.quantize(Decimal('217'), Decimal('1e-1'))
          Decimal("217.0")
!         >>> DefaultContext.quantize(Decimal('217'), Decimal('1e-0'))
          Decimal("217")
!         >>> DefaultContext.quantize(Decimal('217'), Decimal('1e+1'))
          Decimal("2.2E+2")
!         >>> DefaultContext.quantize(Decimal('217'), Decimal('1e+2'))
          Decimal("2E+2")
          """
***************
*** 2597,2611 ****
          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")
          """
--- 2561,2575 ----
          remainder cannot be calculated).
  
!         >>> DefaultContext.remainder(Decimal('2.1'), Decimal('3'))
          Decimal("2.1")
!         >>> DefaultContext.remainder(Decimal('10'), Decimal('3'))
          Decimal("1")
!         >>> DefaultContext.remainder(Decimal('-10'), Decimal('3'))
          Decimal("-1")
!         >>> DefaultContext.remainder(Decimal('10.2'), Decimal('1'))
          Decimal("0.2")
!         >>> DefaultContext.remainder(Decimal('10'), Decimal('0.3'))
          Decimal("0.1")
!         >>> DefaultContext.remainder(Decimal('3.6'), Decimal('1.3'))
          Decimal("1.0")
          """
***************
*** 2622,2638 ****
          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")
          """
--- 2586,2602 ----
          remainder cannot be calculated).
  
!         >>> DefaultContext.remainder_near(Decimal('2.1'), Decimal('3'))
          Decimal("-0.9")
!         >>> DefaultContext.remainder_near(Decimal('10'), Decimal('6'))
          Decimal("-2")
!         >>> DefaultContext.remainder_near(Decimal('10'), Decimal('3'))
          Decimal("1")
!         >>> DefaultContext.remainder_near(Decimal('-10'), Decimal('3'))
          Decimal("-1")
!         >>> DefaultContext.remainder_near(Decimal('10.2'), Decimal('1'))
          Decimal("0.2")
!         >>> DefaultContext.remainder_near(Decimal('10'), Decimal('0.3'))
          Decimal("0.1")
!         >>> DefaultContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
          Decimal("-0.3")
          """
***************
*** 2645,2655 ****
          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
          """
--- 2609,2619 ----
          either operand.
  
!         >>> DefaultContext.same_quantum(Decimal('2.17'), Decimal('0.001'))
          False
!         >>> DefaultContext.same_quantum(Decimal('2.17'), Decimal('0.01'))
          True
!         >>> DefaultContext.same_quantum(Decimal('2.17'), Decimal('1'))
          False
!         >>> DefaultContext.same_quantum(Decimal('Inf'), Decimal('-Inf'))
          True
          """
***************
*** 2662,2682 ****
          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")
          """
--- 2626,2646 ----
          algorithm.
  
!         >>> DefaultContext.sqrt(Decimal('0'))
          Decimal("0")
!         >>> DefaultContext.sqrt(Decimal('-0'))
          Decimal("-0")
!         >>> DefaultContext.sqrt(Decimal('0.39'))
          Decimal("0.624499800")
!         >>> DefaultContext.sqrt(Decimal('100'))
          Decimal("10")
!         >>> DefaultContext.sqrt(Decimal('1'))
          Decimal("1")
!         >>> DefaultContext.sqrt(Decimal('1.0'))
          Decimal("1.0")
!         >>> DefaultContext.sqrt(Decimal('1.00'))
          Decimal("1.0")
!         >>> DefaultContext.sqrt(Decimal('7'))
          Decimal("2.64575131")
!         >>> DefaultContext.sqrt(Decimal('10'))
          Decimal("3.16227766")
          """
***************
*** 2686,2694 ****
          """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")
          """
--- 2650,2658 ----
          """Return the sum of the two operands.
  
!         >>> DefaultContext.subtract(Decimal('1.3'), Decimal('1.07'))
          Decimal("0.23")
!         >>> DefaultContext.subtract(Decimal('1.3'), Decimal('1.30'))
          Decimal("0.00")
!         >>> DefaultContext.subtract(Decimal('1.3'), Decimal('2.07'))
          Decimal("-0.77")
          """
***************
*** 2718,2736 ****
          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")
          """
--- 2682,2700 ----
          be set. The rounding mode is taken from the context.
  
!         >>> DefaultContext.to_integral(Decimal('2.1'))
          Decimal("2")
!         >>> DefaultContext.to_integral(Decimal('100'))
          Decimal("100")
!         >>> DefaultContext.to_integral(Decimal('100.0'))
          Decimal("100")
!         >>> DefaultContext.to_integral(Decimal('101.5'))
          Decimal("102")
!         >>> DefaultContext.to_integral(Decimal('-101.5'))
          Decimal("-102")
!         >>> DefaultContext.to_integral(Decimal('10E+5'))
          Decimal("1.0E+6")
!         >>> DefaultContext.to_integral(Decimal('7.89E+77'))
          Decimal("7.89E+77")
!         >>> DefaultContext.to_integral(Decimal('-Inf'))
          Decimal("-Infinity")
          """
***************
*** 2924,2999 ****
      return op1, op2, adjust
  
! 
! BASIC_DEFAULT_TRAPS = {}
! BASIC_DEFAULT_FLAGS = {}
! 
! EXTENDED_DEFAULT_TRAPS = {}
! EXTENDED_DEFAULT_FLAGS = {}
! 
! for exception in ExceptionList:
!     BASIC_DEFAULT_TRAPS[exception] = exception.default
!     BASIC_DEFAULT_FLAGS[exception] = 0
!     EXTENDED_DEFAULT_TRAPS[exception] = 0
!     EXTENDED_DEFAULT_FLAGS[exception] = 0
! 
! 
! DEFAULT_PRECISION = EXTENDED_DEFAULT_PRECISION
! DEFAULT_ROUNDING = EXTENDED_DEFAULT_ROUNDING
! DEFAULT_TRAPS = EXTENDED_DEFAULT_TRAPS
! DEFAULT_FLAGS= EXTENDED_DEFAULT_FLAGS
! DEFAULT_ROUNDING_DECISION = EXTENDED_DEFAULT_ROUNDING_DECISION
! 
! IGNORED_FLAGS = []
! 
! DefaultLock = threading.Lock()
! DefaultContext = Context(DEFAULT_PRECISION, DEFAULT_ROUNDING,
!                          DEFAULT_TRAPS.copy(),
!                          DEFAULT_FLAGS.copy(), DEFAULT_ROUNDING_DECISION,
!                          DEFAULT_MIN_EXPONENT, DEFAULT_MAX_EXPONENT,
!                          CAPITALS, CLAMP, IGNORED_FLAGS)
! 
! #Used in SetDefaultContext
! DefaultAttributeDict= {'DEFAULT_PRECISION' : 'prec',
!                        'DEFAULT_TRAPS' : 'trap_enablers',
!                        'DEFAULT_FLAGS' : 'flags',
!                        'DEFAULT_ROUNDING' : 'rounding',
!                        'DEFAULT_ROUNDING_DECISION' : '_rounding_decision',
!                        'DEFAULT_MIN_EXPONENT' : 'Emin',
!                        'DEFAULT_MAX_EXPONENT' : 'Emax'}
! 
! 
! def SetDefaultContext(context=None):
!     """Changes DefaultContext to the DEFAULT_*
! 
!     If context (default None) is BASIC_DEFAULT_CONTEXT or
!     EXTENDED_DEFAULT_CONTEXT, sets DEFAULT_* to BASIC_DEFAULT_* or
!     EXTENDED_DEFAULT_*.
! 
!     Whether that happened or not, change DefaultContext to use those.
!     """
!     global DEFAULT_CONTEXT, DEFAULT_PRECISION, DEFAULT_TRAPS
!     global DEFAULT_FLAGS, DEFAULT_ROUNDING, DEFAULT_ROUNDING_DECISION
!     global DEFAULT_MIN_EXPONENT, DEFAULT_MAX_EXPONENT
!     DEFAULT_CONTEXT = context
!     if DEFAULT_CONTEXT == BASIC_DEFAULT_CONTEXT:
!         DEFAULT_PRECISION = BASIC_DEFAULT_PRECISION
!         DEFAULT_ROUNDING = BASIC_DEFAULT_ROUNDING
!         DEFAULT_TRAPS = BASIC_DEFAULT_TRAPS
!         DEFAULT_FLAGS= BASIC_DEFAULT_FLAGS
!         DEFAULT_ROUNDING_DECISION = BASIC_DEFAULT_ROUNDING_DECISION
! 
!     elif DEFAULT_CONTEXT == EXTENDED_DEFAULT_CONTEXT:
!         DEFAULT_PRECISION = EXTENDED_DEFAULT_PRECISION
!         DEFAULT_ROUNDING = EXTENDED_DEFAULT_ROUNDING
!         DEFAULT_TRAPS = EXTENDED_DEFAULT_TRAPS
!         DEFAULT_FLAGS= EXTENDED_DEFAULT_FLAGS
!         DEFAULT_ROUNDING_DECISION = EXTENDED_DEFAULT_ROUNDING_DECISION
! 
!     for key, val in DefaultAttributeDict.items():
!         setattr(DefaultContext, val,
!                 copy.copy(globals()[key]))
! 
! 
! SetDefaultContext(DEFAULT_CONTEXT)
  
  _infinity_map = {
--- 2888,2892 ----
      return op1, op2, adjust
  
! ##### Helper Functions ########################################
  
  _infinity_map = {
***************
*** 3043,3046 ****
--- 2936,2984 ----
      return 0
  
+ 
+ ##### Setup Specific Contexts ################################
+ 
+ def _zero_exceptions():
+     "Helper function mapping all exceptions to zero."
+     d = {}
+     for exception in ExceptionList:
+         d[exception] = 0
+     return d
+ 
+ # The default context prototype used by Context()
+ # Is mutable, so than new contexts can have different default values
+ 
+ DefaultContext = Context(
+         prec=SINGLE_PRECISION, rounding=ROUND_HALF_EVEN,
+         trap_enablers=_zero_exceptions(),
+         flags=_zero_exceptions(),
+         _rounding_decision=ALWAYS_ROUND,
+ )
+ 
+ # Pre-made alternate contexts offered by the specification
+ # Don't change these; the user should be able to select these
+ # contexts and be able to reproduce results from other implementations
+ # of the spec.
+ 
+ _basic_traps = _zero_exceptions()
+ _basic_traps.update({Inexact:1, Rounded:1, Subnormal:1})
+ 
+ BasicDefaultContext = Context(
+         prec=9, rounding=ROUND_HALF_UP,
+         trap_enablers=_basic_traps,
+         flags=_zero_exceptions(),
+         _rounding_decision=ALWAYS_ROUND,
+ )
+ 
+ ExtendedDefaultContext = Context(
+         prec=SINGLE_PRECISION, rounding=ROUND_HALF_EVEN,
+         trap_enablers=_zero_exceptions(),
+         flags=_zero_exceptions(),
+         _rounding_decision=ALWAYS_ROUND,
+ )
+ 
+ 
+ ##### Useful Constants ######################################
+ 
  #Reusable defaults
  Inf = Decimal('Inf')
***************
*** 3052,3056 ****
  NaN = Decimal('NaN')
  
! # crud for parsing strings
  import re
  
--- 2990,2995 ----
  NaN = Decimal('NaN')
  
! 
! ##### crud for parsing strings #################################
  import re
  




More information about the Python-checkins mailing list