Updates of recent changes to logging.
[python.git] / Lib / decimal.py
blobf65193524f22b00d9d35652e5a9f10f94dcd93fc
1 # Copyright (c) 2004 Python Software Foundation.
2 # All rights reserved.
4 # Written by Eric Price <eprice at tjhsst.edu>
5 # and Facundo Batista <facundo at taniquetil.com.ar>
6 # and Raymond Hettinger <python at rcn.com>
7 # and Aahz <aahz at pobox.com>
8 # and Tim Peters
10 # This module is currently Py2.3 compatible and should be kept that way
11 # unless a major compelling advantage arises. IOW, 2.3 compatibility is
12 # strongly preferred, but not guaranteed.
14 # Also, this module should be kept in sync with the latest updates of
15 # the IBM specification as it evolves. Those updates will be treated
16 # as bug fixes (deviation from the spec is a compatibility, usability
17 # bug) and will be backported. At this point the spec is stabilizing
18 # and the updates are becoming fewer, smaller, and less significant.
20 """
21 This is a Py2.3 implementation of decimal floating point arithmetic based on
22 the General Decimal Arithmetic Specification:
24 www2.hursley.ibm.com/decimal/decarith.html
26 and IEEE standard 854-1987:
28 www.cs.berkeley.edu/~ejr/projects/754/private/drafts/854-1987/dir.html
30 Decimal floating point has finite precision with arbitrarily large bounds.
32 The purpose of this module is to support arithmetic using familiar
33 "schoolhouse" rules and to avoid some of the tricky representation
34 issues associated with binary floating point. The package is especially
35 useful for financial applications or for contexts where users have
36 expectations that are at odds with binary floating point (for instance,
37 in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead
38 of the expected Decimal("0.00") returned by decimal floating point).
40 Here are some examples of using the decimal module:
42 >>> from decimal import *
43 >>> setcontext(ExtendedContext)
44 >>> Decimal(0)
45 Decimal("0")
46 >>> Decimal("1")
47 Decimal("1")
48 >>> Decimal("-.0123")
49 Decimal("-0.0123")
50 >>> Decimal(123456)
51 Decimal("123456")
52 >>> Decimal("123.45e12345678901234567890")
53 Decimal("1.2345E+12345678901234567892")
54 >>> Decimal("1.33") + Decimal("1.27")
55 Decimal("2.60")
56 >>> Decimal("12.34") + Decimal("3.87") - Decimal("18.41")
57 Decimal("-2.20")
58 >>> dig = Decimal(1)
59 >>> print dig / Decimal(3)
60 0.333333333
61 >>> getcontext().prec = 18
62 >>> print dig / Decimal(3)
63 0.333333333333333333
64 >>> print dig.sqrt()
66 >>> print Decimal(3).sqrt()
67 1.73205080756887729
68 >>> print Decimal(3) ** 123
69 4.85192780976896427E+58
70 >>> inf = Decimal(1) / Decimal(0)
71 >>> print inf
72 Infinity
73 >>> neginf = Decimal(-1) / Decimal(0)
74 >>> print neginf
75 -Infinity
76 >>> print neginf + inf
77 NaN
78 >>> print neginf * inf
79 -Infinity
80 >>> print dig / 0
81 Infinity
82 >>> getcontext().traps[DivisionByZero] = 1
83 >>> print dig / 0
84 Traceback (most recent call last):
85 ...
86 ...
87 ...
88 DivisionByZero: x / 0
89 >>> c = Context()
90 >>> c.traps[InvalidOperation] = 0
91 >>> print c.flags[InvalidOperation]
93 >>> c.divide(Decimal(0), Decimal(0))
94 Decimal("NaN")
95 >>> c.traps[InvalidOperation] = 1
96 >>> print c.flags[InvalidOperation]
98 >>> c.flags[InvalidOperation] = 0
99 >>> print c.flags[InvalidOperation]
101 >>> print c.divide(Decimal(0), Decimal(0))
102 Traceback (most recent call last):
106 InvalidOperation: 0 / 0
107 >>> print c.flags[InvalidOperation]
109 >>> c.flags[InvalidOperation] = 0
110 >>> c.traps[InvalidOperation] = 0
111 >>> print c.divide(Decimal(0), Decimal(0))
113 >>> print c.flags[InvalidOperation]
118 __all__ = [
119 # Two major classes
120 'Decimal', 'Context',
122 # Contexts
123 'DefaultContext', 'BasicContext', 'ExtendedContext',
125 # Exceptions
126 'DecimalException', 'Clamped', 'InvalidOperation', 'DivisionByZero',
127 'Inexact', 'Rounded', 'Subnormal', 'Overflow', 'Underflow',
129 # Constants for use in setting up contexts
130 'ROUND_DOWN', 'ROUND_HALF_UP', 'ROUND_HALF_EVEN', 'ROUND_CEILING',
131 'ROUND_FLOOR', 'ROUND_UP', 'ROUND_HALF_DOWN',
133 # Functions for manipulating contexts
134 'setcontext', 'getcontext', 'localcontext'
137 import copy as _copy
139 # Rounding
140 ROUND_DOWN = 'ROUND_DOWN'
141 ROUND_HALF_UP = 'ROUND_HALF_UP'
142 ROUND_HALF_EVEN = 'ROUND_HALF_EVEN'
143 ROUND_CEILING = 'ROUND_CEILING'
144 ROUND_FLOOR = 'ROUND_FLOOR'
145 ROUND_UP = 'ROUND_UP'
146 ROUND_HALF_DOWN = 'ROUND_HALF_DOWN'
148 # Rounding decision (not part of the public API)
149 NEVER_ROUND = 'NEVER_ROUND' # Round in division (non-divmod), sqrt ONLY
150 ALWAYS_ROUND = 'ALWAYS_ROUND' # Every operation rounds at end.
152 # Errors
154 class DecimalException(ArithmeticError):
155 """Base exception class.
157 Used exceptions derive from this.
158 If an exception derives from another exception besides this (such as
159 Underflow (Inexact, Rounded, Subnormal) that indicates that it is only
160 called if the others are present. This isn't actually used for
161 anything, though.
163 handle -- Called when context._raise_error is called and the
164 trap_enabler is set. First argument is self, second is the
165 context. More arguments can be given, those being after
166 the explanation in _raise_error (For example,
167 context._raise_error(NewError, '(-x)!', self._sign) would
168 call NewError().handle(context, self._sign).)
170 To define a new exception, it should be sufficient to have it derive
171 from DecimalException.
173 def handle(self, context, *args):
174 pass
177 class Clamped(DecimalException):
178 """Exponent of a 0 changed to fit bounds.
180 This occurs and signals clamped if the exponent of a result has been
181 altered in order to fit the constraints of a specific concrete
182 representation. This may occur when the exponent of a zero result would
183 be outside the bounds of a representation, or when a large normal
184 number would have an encoded exponent that cannot be represented. In
185 this latter case, the exponent is reduced to fit and the corresponding
186 number of zero digits are appended to the coefficient ("fold-down").
190 class InvalidOperation(DecimalException):
191 """An invalid operation was performed.
193 Various bad things cause this:
195 Something creates a signaling NaN
196 -INF + INF
197 0 * (+-)INF
198 (+-)INF / (+-)INF
199 x % 0
200 (+-)INF % x
201 x._rescale( non-integer )
202 sqrt(-x) , x > 0
203 0 ** 0
204 x ** (non-integer)
205 x ** (+-)INF
206 An operand is invalid
208 def handle(self, context, *args):
209 if args:
210 if args[0] == 1: # sNaN, must drop 's' but keep diagnostics
211 return Decimal( (args[1]._sign, args[1]._int, 'n') )
212 return NaN
214 class ConversionSyntax(InvalidOperation):
215 """Trying to convert badly formed string.
217 This occurs and signals invalid-operation if an string is being
218 converted to a number and it does not conform to the numeric string
219 syntax. The result is [0,qNaN].
222 def handle(self, context, *args):
223 return (0, (0,), 'n') # Passed to something which uses a tuple.
225 class DivisionByZero(DecimalException, ZeroDivisionError):
226 """Division by 0.
228 This occurs and signals division-by-zero if division of a finite number
229 by zero was attempted (during a divide-integer or divide operation, or a
230 power operation with negative right-hand operand), and the dividend was
231 not zero.
233 The result of the operation is [sign,inf], where sign is the exclusive
234 or of the signs of the operands for divide, or is 1 for an odd power of
235 -0, for power.
238 def handle(self, context, sign, double = None, *args):
239 if double is not None:
240 return (Infsign[sign],)*2
241 return Infsign[sign]
243 class DivisionImpossible(InvalidOperation):
244 """Cannot perform the division adequately.
246 This occurs and signals invalid-operation if the integer result of a
247 divide-integer or remainder operation had too many digits (would be
248 longer than precision). The result is [0,qNaN].
251 def handle(self, context, *args):
252 return (NaN, NaN)
254 class DivisionUndefined(InvalidOperation, ZeroDivisionError):
255 """Undefined result of division.
257 This occurs and signals invalid-operation if division by zero was
258 attempted (during a divide-integer, divide, or remainder operation), and
259 the dividend is also zero. The result is [0,qNaN].
262 def handle(self, context, tup=None, *args):
263 if tup is not None:
264 return (NaN, NaN) # for 0 %0, 0 // 0
265 return NaN
267 class Inexact(DecimalException):
268 """Had to round, losing information.
270 This occurs and signals inexact whenever the result of an operation is
271 not exact (that is, it needed to be rounded and any discarded digits
272 were non-zero), or if an overflow or underflow condition occurs. The
273 result in all cases is unchanged.
275 The inexact signal may be tested (or trapped) to determine if a given
276 operation (or sequence of operations) was inexact.
278 pass
280 class InvalidContext(InvalidOperation):
281 """Invalid context. Unknown rounding, for example.
283 This occurs and signals invalid-operation if an invalid context was
284 detected during an operation. This can occur if contexts are not checked
285 on creation and either the precision exceeds the capability of the
286 underlying concrete representation or an unknown or unsupported rounding
287 was specified. These aspects of the context need only be checked when
288 the values are required to be used. The result is [0,qNaN].
291 def handle(self, context, *args):
292 return NaN
294 class Rounded(DecimalException):
295 """Number got rounded (not necessarily changed during rounding).
297 This occurs and signals rounded whenever the result of an operation is
298 rounded (that is, some zero or non-zero digits were discarded from the
299 coefficient), or if an overflow or underflow condition occurs. The
300 result in all cases is unchanged.
302 The rounded signal may be tested (or trapped) to determine if a given
303 operation (or sequence of operations) caused a loss of precision.
305 pass
307 class Subnormal(DecimalException):
308 """Exponent < Emin before rounding.
310 This occurs and signals subnormal whenever the result of a conversion or
311 operation is subnormal (that is, its adjusted exponent is less than
312 Emin, before any rounding). The result in all cases is unchanged.
314 The subnormal signal may be tested (or trapped) to determine if a given
315 or operation (or sequence of operations) yielded a subnormal result.
317 pass
319 class Overflow(Inexact, Rounded):
320 """Numerical overflow.
322 This occurs and signals overflow if the adjusted exponent of a result
323 (from a conversion or from an operation that is not an attempt to divide
324 by zero), after rounding, would be greater than the largest value that
325 can be handled by the implementation (the value Emax).
327 The result depends on the rounding mode:
329 For round-half-up and round-half-even (and for round-half-down and
330 round-up, if implemented), the result of the operation is [sign,inf],
331 where sign is the sign of the intermediate result. For round-down, the
332 result is the largest finite number that can be represented in the
333 current precision, with the sign of the intermediate result. For
334 round-ceiling, the result is the same as for round-down if the sign of
335 the intermediate result is 1, or is [0,inf] otherwise. For round-floor,
336 the result is the same as for round-down if the sign of the intermediate
337 result is 0, or is [1,inf] otherwise. In all cases, Inexact and Rounded
338 will also be raised.
341 def handle(self, context, sign, *args):
342 if context.rounding in (ROUND_HALF_UP, ROUND_HALF_EVEN,
343 ROUND_HALF_DOWN, ROUND_UP):
344 return Infsign[sign]
345 if sign == 0:
346 if context.rounding == ROUND_CEILING:
347 return Infsign[sign]
348 return Decimal((sign, (9,)*context.prec,
349 context.Emax-context.prec+1))
350 if sign == 1:
351 if context.rounding == ROUND_FLOOR:
352 return Infsign[sign]
353 return Decimal( (sign, (9,)*context.prec,
354 context.Emax-context.prec+1))
357 class Underflow(Inexact, Rounded, Subnormal):
358 """Numerical underflow with result rounded to 0.
360 This occurs and signals underflow if a result is inexact and the
361 adjusted exponent of the result would be smaller (more negative) than
362 the smallest value that can be handled by the implementation (the value
363 Emin). That is, the result is both inexact and subnormal.
365 The result after an underflow will be a subnormal number rounded, if
366 necessary, so that its exponent is not less than Etiny. This may result
367 in 0 with the sign of the intermediate result and an exponent of Etiny.
369 In all cases, Inexact, Rounded, and Subnormal will also be raised.
372 # List of public traps and flags
373 _signals = [Clamped, DivisionByZero, Inexact, Overflow, Rounded,
374 Underflow, InvalidOperation, Subnormal]
376 # Map conditions (per the spec) to signals
377 _condition_map = {ConversionSyntax:InvalidOperation,
378 DivisionImpossible:InvalidOperation,
379 DivisionUndefined:InvalidOperation,
380 InvalidContext:InvalidOperation}
382 ##### Context Functions ##################################################
384 # The getcontext() and setcontext() function manage access to a thread-local
385 # current context. Py2.4 offers direct support for thread locals. If that
386 # is not available, use threading.currentThread() which is slower but will
387 # work for older Pythons. If threads are not part of the build, create a
388 # mock threading object with threading.local() returning the module namespace.
390 try:
391 import threading
392 except ImportError:
393 # Python was compiled without threads; create a mock object instead
394 import sys
395 class MockThreading(object):
396 def local(self, sys=sys):
397 return sys.modules[__name__]
398 threading = MockThreading()
399 del sys, MockThreading
401 try:
402 threading.local
404 except AttributeError:
406 # To fix reloading, force it to create a new context
407 # Old contexts have different exceptions in their dicts, making problems.
408 if hasattr(threading.currentThread(), '__decimal_context__'):
409 del threading.currentThread().__decimal_context__
411 def setcontext(context):
412 """Set this thread's context to context."""
413 if context in (DefaultContext, BasicContext, ExtendedContext):
414 context = context.copy()
415 context.clear_flags()
416 threading.currentThread().__decimal_context__ = context
418 def getcontext():
419 """Returns this thread's context.
421 If this thread does not yet have a context, returns
422 a new context and sets this thread's context.
423 New contexts are copies of DefaultContext.
425 try:
426 return threading.currentThread().__decimal_context__
427 except AttributeError:
428 context = Context()
429 threading.currentThread().__decimal_context__ = context
430 return context
432 else:
434 local = threading.local()
435 if hasattr(local, '__decimal_context__'):
436 del local.__decimal_context__
438 def getcontext(_local=local):
439 """Returns this thread's context.
441 If this thread does not yet have a context, returns
442 a new context and sets this thread's context.
443 New contexts are copies of DefaultContext.
445 try:
446 return _local.__decimal_context__
447 except AttributeError:
448 context = Context()
449 _local.__decimal_context__ = context
450 return context
452 def setcontext(context, _local=local):
453 """Set this thread's context to context."""
454 if context in (DefaultContext, BasicContext, ExtendedContext):
455 context = context.copy()
456 context.clear_flags()
457 _local.__decimal_context__ = context
459 del threading, local # Don't contaminate the namespace
461 def localcontext(ctx=None):
462 """Return a context manager for a copy of the supplied context
464 Uses a copy of the current context if no context is specified
465 The returned context manager creates a local decimal context
466 in a with statement:
467 def sin(x):
468 with localcontext() as ctx:
469 ctx.prec += 2
470 # Rest of sin calculation algorithm
471 # uses a precision 2 greater than normal
472 return +s # Convert result to normal precision
474 def sin(x):
475 with localcontext(ExtendedContext):
476 # Rest of sin calculation algorithm
477 # uses the Extended Context from the
478 # General Decimal Arithmetic Specification
479 return +s # Convert result to normal context
482 # The string below can't be included in the docstring until Python 2.6
483 # as the doctest module doesn't understand __future__ statements
485 >>> from __future__ import with_statement
486 >>> print getcontext().prec
488 >>> with localcontext():
489 ... ctx = getcontext()
490 ... ctx.prec += 2
491 ... print ctx.prec
494 >>> with localcontext(ExtendedContext):
495 ... print getcontext().prec
498 >>> print getcontext().prec
501 if ctx is None: ctx = getcontext()
502 return _ContextManager(ctx)
505 ##### Decimal class #######################################################
507 class Decimal(object):
508 """Floating point class for decimal arithmetic."""
510 __slots__ = ('_exp','_int','_sign', '_is_special')
511 # Generally, the value of the Decimal instance is given by
512 # (-1)**_sign * _int * 10**_exp
513 # Special values are signified by _is_special == True
515 # We're immutable, so use __new__ not __init__
516 def __new__(cls, value="0", context=None):
517 """Create a decimal point instance.
519 >>> Decimal('3.14') # string input
520 Decimal("3.14")
521 >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent)
522 Decimal("3.14")
523 >>> Decimal(314) # int or long
524 Decimal("314")
525 >>> Decimal(Decimal(314)) # another decimal instance
526 Decimal("314")
529 self = object.__new__(cls)
530 self._is_special = False
532 # From an internal working value
533 if isinstance(value, _WorkRep):
534 self._sign = value.sign
535 self._int = tuple(map(int, str(value.int)))
536 self._exp = int(value.exp)
537 return self
539 # From another decimal
540 if isinstance(value, Decimal):
541 self._exp = value._exp
542 self._sign = value._sign
543 self._int = value._int
544 self._is_special = value._is_special
545 return self
547 # From an integer
548 if isinstance(value, (int,long)):
549 if value >= 0:
550 self._sign = 0
551 else:
552 self._sign = 1
553 self._exp = 0
554 self._int = tuple(map(int, str(abs(value))))
555 return self
557 # tuple/list conversion (possibly from as_tuple())
558 if isinstance(value, (list,tuple)):
559 if len(value) != 3:
560 raise ValueError('Invalid arguments')
561 if value[0] not in (0,1):
562 raise ValueError('Invalid sign')
563 for digit in value[1]:
564 if not isinstance(digit, (int,long)) or digit < 0:
565 raise ValueError("The second value in the tuple must be"
566 "composed of non negative integer elements.")
567 self._sign = value[0]
568 self._int = tuple(value[1])
569 if value[2] in ('F','n','N'):
570 self._exp = value[2]
571 self._is_special = True
572 else:
573 self._exp = int(value[2])
574 return self
576 if isinstance(value, float):
577 raise TypeError("Cannot convert float to Decimal. " +
578 "First convert the float to a string")
580 # Other argument types may require the context during interpretation
581 if context is None:
582 context = getcontext()
584 # From a string
585 # REs insist on real strings, so we can too.
586 if isinstance(value, basestring):
587 if _isinfinity(value):
588 self._exp = 'F'
589 self._int = (0,)
590 self._is_special = True
591 if _isinfinity(value) == 1:
592 self._sign = 0
593 else:
594 self._sign = 1
595 return self
596 if _isnan(value):
597 sig, sign, diag = _isnan(value)
598 self._is_special = True
599 if len(diag) > context.prec: # Diagnostic info too long
600 self._sign, self._int, self._exp = \
601 context._raise_error(ConversionSyntax)
602 return self
603 if sig == 1:
604 self._exp = 'n' # qNaN
605 else: # sig == 2
606 self._exp = 'N' # sNaN
607 self._sign = sign
608 self._int = tuple(map(int, diag)) # Diagnostic info
609 return self
610 try:
611 self._sign, self._int, self._exp = _string2exact(value)
612 except ValueError:
613 self._is_special = True
614 self._sign, self._int, self._exp = \
615 context._raise_error(ConversionSyntax)
616 return self
618 raise TypeError("Cannot convert %r to Decimal" % value)
620 def _isnan(self):
621 """Returns whether the number is not actually one.
623 0 if a number
624 1 if NaN
625 2 if sNaN
627 if self._is_special:
628 exp = self._exp
629 if exp == 'n':
630 return 1
631 elif exp == 'N':
632 return 2
633 return 0
635 def _isinfinity(self):
636 """Returns whether the number is infinite
638 0 if finite or not a number
639 1 if +INF
640 -1 if -INF
642 if self._exp == 'F':
643 if self._sign:
644 return -1
645 return 1
646 return 0
648 def _check_nans(self, other = None, context=None):
649 """Returns whether the number is not actually one.
651 if self, other are sNaN, signal
652 if self, other are NaN return nan
653 return 0
655 Done before operations.
658 self_is_nan = self._isnan()
659 if other is None:
660 other_is_nan = False
661 else:
662 other_is_nan = other._isnan()
664 if self_is_nan or other_is_nan:
665 if context is None:
666 context = getcontext()
668 if self_is_nan == 2:
669 return context._raise_error(InvalidOperation, 'sNaN',
670 1, self)
671 if other_is_nan == 2:
672 return context._raise_error(InvalidOperation, 'sNaN',
673 1, other)
674 if self_is_nan:
675 return self
677 return other
678 return 0
680 def __nonzero__(self):
681 """Is the number non-zero?
683 0 if self == 0
684 1 if self != 0
686 if self._is_special:
687 return 1
688 return sum(self._int) != 0
690 def __cmp__(self, other, context=None):
691 other = _convert_other(other)
692 if other is NotImplemented:
693 return other
695 if self._is_special or other._is_special:
696 ans = self._check_nans(other, context)
697 if ans:
698 return 1 # Comparison involving NaN's always reports self > other
700 # INF = INF
701 return cmp(self._isinfinity(), other._isinfinity())
703 if not self and not other:
704 return 0 # If both 0, sign comparison isn't certain.
706 # If different signs, neg one is less
707 if other._sign < self._sign:
708 return -1
709 if self._sign < other._sign:
710 return 1
712 self_adjusted = self.adjusted()
713 other_adjusted = other.adjusted()
714 if self_adjusted == other_adjusted and \
715 self._int + (0,)*(self._exp - other._exp) == \
716 other._int + (0,)*(other._exp - self._exp):
717 return 0 # equal, except in precision. ([0]*(-x) = [])
718 elif self_adjusted > other_adjusted and self._int[0] != 0:
719 return (-1)**self._sign
720 elif self_adjusted < other_adjusted and other._int[0] != 0:
721 return -((-1)**self._sign)
723 # Need to round, so make sure we have a valid context
724 if context is None:
725 context = getcontext()
727 context = context._shallow_copy()
728 rounding = context._set_rounding(ROUND_UP) # round away from 0
730 flags = context._ignore_all_flags()
731 res = self.__sub__(other, context=context)
733 context._regard_flags(*flags)
735 context.rounding = rounding
737 if not res:
738 return 0
739 elif res._sign:
740 return -1
741 return 1
743 def __eq__(self, other):
744 if not isinstance(other, (Decimal, int, long)):
745 return NotImplemented
746 return self.__cmp__(other) == 0
748 def __ne__(self, other):
749 if not isinstance(other, (Decimal, int, long)):
750 return NotImplemented
751 return self.__cmp__(other) != 0
753 def compare(self, other, context=None):
754 """Compares one to another.
756 -1 => a < b
757 0 => a = b
758 1 => a > b
759 NaN => one is NaN
760 Like __cmp__, but returns Decimal instances.
762 other = _convert_other(other)
763 if other is NotImplemented:
764 return other
766 # Compare(NaN, NaN) = NaN
767 if (self._is_special or other and other._is_special):
768 ans = self._check_nans(other, context)
769 if ans:
770 return ans
772 return Decimal(self.__cmp__(other, context))
774 def __hash__(self):
775 """x.__hash__() <==> hash(x)"""
776 # Decimal integers must hash the same as the ints
777 # Non-integer decimals are normalized and hashed as strings
778 # Normalization assures that hash(100E-1) == hash(10)
779 if self._is_special:
780 if self._isnan():
781 raise TypeError('Cannot hash a NaN value.')
782 return hash(str(self))
783 i = int(self)
784 if self == Decimal(i):
785 return hash(i)
786 assert self.__nonzero__() # '-0' handled by integer case
787 return hash(str(self.normalize()))
789 def as_tuple(self):
790 """Represents the number as a triple tuple.
792 To show the internals exactly as they are.
794 return (self._sign, self._int, self._exp)
796 def __repr__(self):
797 """Represents the number as an instance of Decimal."""
798 # Invariant: eval(repr(d)) == d
799 return 'Decimal("%s")' % str(self)
801 def __str__(self, eng = 0, context=None):
802 """Return string representation of the number in scientific notation.
804 Captures all of the information in the underlying representation.
807 if self._is_special:
808 if self._isnan():
809 minus = '-'*self._sign
810 if self._int == (0,):
811 info = ''
812 else:
813 info = ''.join(map(str, self._int))
814 if self._isnan() == 2:
815 return minus + 'sNaN' + info
816 return minus + 'NaN' + info
817 if self._isinfinity():
818 minus = '-'*self._sign
819 return minus + 'Infinity'
821 if context is None:
822 context = getcontext()
824 tmp = map(str, self._int)
825 numdigits = len(self._int)
826 leftdigits = self._exp + numdigits
827 if eng and not self: # self = 0eX wants 0[.0[0]]eY, not [[0]0]0eY
828 if self._exp < 0 and self._exp >= -6: # short, no need for e/E
829 s = '-'*self._sign + '0.' + '0'*(abs(self._exp))
830 return s
831 # exp is closest mult. of 3 >= self._exp
832 exp = ((self._exp - 1)// 3 + 1) * 3
833 if exp != self._exp:
834 s = '0.'+'0'*(exp - self._exp)
835 else:
836 s = '0'
837 if exp != 0:
838 if context.capitals:
839 s += 'E'
840 else:
841 s += 'e'
842 if exp > 0:
843 s += '+' # 0.0e+3, not 0.0e3
844 s += str(exp)
845 s = '-'*self._sign + s
846 return s
847 if eng:
848 dotplace = (leftdigits-1)%3+1
849 adjexp = leftdigits -1 - (leftdigits-1)%3
850 else:
851 adjexp = leftdigits-1
852 dotplace = 1
853 if self._exp == 0:
854 pass
855 elif self._exp < 0 and adjexp >= 0:
856 tmp.insert(leftdigits, '.')
857 elif self._exp < 0 and adjexp >= -6:
858 tmp[0:0] = ['0'] * int(-leftdigits)
859 tmp.insert(0, '0.')
860 else:
861 if numdigits > dotplace:
862 tmp.insert(dotplace, '.')
863 elif numdigits < dotplace:
864 tmp.extend(['0']*(dotplace-numdigits))
865 if adjexp:
866 if not context.capitals:
867 tmp.append('e')
868 else:
869 tmp.append('E')
870 if adjexp > 0:
871 tmp.append('+')
872 tmp.append(str(adjexp))
873 if eng:
874 while tmp[0:1] == ['0']:
875 tmp[0:1] = []
876 if len(tmp) == 0 or tmp[0] == '.' or tmp[0].lower() == 'e':
877 tmp[0:0] = ['0']
878 if self._sign:
879 tmp.insert(0, '-')
881 return ''.join(tmp)
883 def to_eng_string(self, context=None):
884 """Convert to engineering-type string.
886 Engineering notation has an exponent which is a multiple of 3, so there
887 are up to 3 digits left of the decimal place.
889 Same rules for when in exponential and when as a value as in __str__.
891 return self.__str__(eng=1, context=context)
893 def __neg__(self, context=None):
894 """Returns a copy with the sign switched.
896 Rounds, if it has reason.
898 if self._is_special:
899 ans = self._check_nans(context=context)
900 if ans:
901 return ans
903 if not self:
904 # -Decimal('0') is Decimal('0'), not Decimal('-0')
905 sign = 0
906 elif self._sign:
907 sign = 0
908 else:
909 sign = 1
911 if context is None:
912 context = getcontext()
913 if context._rounding_decision == ALWAYS_ROUND:
914 return Decimal((sign, self._int, self._exp))._fix(context)
915 return Decimal( (sign, self._int, self._exp))
917 def __pos__(self, context=None):
918 """Returns a copy, unless it is a sNaN.
920 Rounds the number (if more then precision digits)
922 if self._is_special:
923 ans = self._check_nans(context=context)
924 if ans:
925 return ans
927 sign = self._sign
928 if not self:
929 # + (-0) = 0
930 sign = 0
932 if context is None:
933 context = getcontext()
935 if context._rounding_decision == ALWAYS_ROUND:
936 ans = self._fix(context)
937 else:
938 ans = Decimal(self)
939 ans._sign = sign
940 return ans
942 def __abs__(self, round=1, context=None):
943 """Returns the absolute value of self.
945 If the second argument is 0, do not round.
947 if self._is_special:
948 ans = self._check_nans(context=context)
949 if ans:
950 return ans
952 if not round:
953 if context is None:
954 context = getcontext()
955 context = context._shallow_copy()
956 context._set_rounding_decision(NEVER_ROUND)
958 if self._sign:
959 ans = self.__neg__(context=context)
960 else:
961 ans = self.__pos__(context=context)
963 return ans
965 def __add__(self, other, context=None):
966 """Returns self + other.
968 -INF + INF (or the reverse) cause InvalidOperation errors.
970 other = _convert_other(other)
971 if other is NotImplemented:
972 return other
974 if context is None:
975 context = getcontext()
977 if self._is_special or other._is_special:
978 ans = self._check_nans(other, context)
979 if ans:
980 return ans
982 if self._isinfinity():
983 # If both INF, same sign => same as both, opposite => error.
984 if self._sign != other._sign and other._isinfinity():
985 return context._raise_error(InvalidOperation, '-INF + INF')
986 return Decimal(self)
987 if other._isinfinity():
988 return Decimal(other) # Can't both be infinity here
990 shouldround = context._rounding_decision == ALWAYS_ROUND
992 exp = min(self._exp, other._exp)
993 negativezero = 0
994 if context.rounding == ROUND_FLOOR and self._sign != other._sign:
995 # If the answer is 0, the sign should be negative, in this case.
996 negativezero = 1
998 if not self and not other:
999 sign = min(self._sign, other._sign)
1000 if negativezero:
1001 sign = 1
1002 return Decimal( (sign, (0,), exp))
1003 if not self:
1004 exp = max(exp, other._exp - context.prec-1)
1005 ans = other._rescale(exp, watchexp=0, context=context)
1006 if shouldround:
1007 ans = ans._fix(context)
1008 return ans
1009 if not other:
1010 exp = max(exp, self._exp - context.prec-1)
1011 ans = self._rescale(exp, watchexp=0, context=context)
1012 if shouldround:
1013 ans = ans._fix(context)
1014 return ans
1016 op1 = _WorkRep(self)
1017 op2 = _WorkRep(other)
1018 op1, op2 = _normalize(op1, op2, shouldround, context.prec)
1020 result = _WorkRep()
1021 if op1.sign != op2.sign:
1022 # Equal and opposite
1023 if op1.int == op2.int:
1024 if exp < context.Etiny():
1025 exp = context.Etiny()
1026 context._raise_error(Clamped)
1027 return Decimal((negativezero, (0,), exp))
1028 if op1.int < op2.int:
1029 op1, op2 = op2, op1
1030 # OK, now abs(op1) > abs(op2)
1031 if op1.sign == 1:
1032 result.sign = 1
1033 op1.sign, op2.sign = op2.sign, op1.sign
1034 else:
1035 result.sign = 0
1036 # So we know the sign, and op1 > 0.
1037 elif op1.sign == 1:
1038 result.sign = 1
1039 op1.sign, op2.sign = (0, 0)
1040 else:
1041 result.sign = 0
1042 # Now, op1 > abs(op2) > 0
1044 if op2.sign == 0:
1045 result.int = op1.int + op2.int
1046 else:
1047 result.int = op1.int - op2.int
1049 result.exp = op1.exp
1050 ans = Decimal(result)
1051 if shouldround:
1052 ans = ans._fix(context)
1053 return ans
1055 __radd__ = __add__
1057 def __sub__(self, other, context=None):
1058 """Return self + (-other)"""
1059 other = _convert_other(other)
1060 if other is NotImplemented:
1061 return other
1063 if self._is_special or other._is_special:
1064 ans = self._check_nans(other, context=context)
1065 if ans:
1066 return ans
1068 # -Decimal(0) = Decimal(0), which we don't want since
1069 # (-0 - 0 = -0 + (-0) = -0, but -0 + 0 = 0.)
1070 # so we change the sign directly to a copy
1071 tmp = Decimal(other)
1072 tmp._sign = 1-tmp._sign
1074 return self.__add__(tmp, context=context)
1076 def __rsub__(self, other, context=None):
1077 """Return other + (-self)"""
1078 other = _convert_other(other)
1079 if other is NotImplemented:
1080 return other
1082 tmp = Decimal(self)
1083 tmp._sign = 1 - tmp._sign
1084 return other.__add__(tmp, context=context)
1086 def _increment(self, round=1, context=None):
1087 """Special case of add, adding 1eExponent
1089 Since it is common, (rounding, for example) this adds
1090 (sign)*one E self._exp to the number more efficiently than add.
1092 For example:
1093 Decimal('5.624e10')._increment() == Decimal('5.625e10')
1095 if self._is_special:
1096 ans = self._check_nans(context=context)
1097 if ans:
1098 return ans
1100 # Must be infinite, and incrementing makes no difference
1101 return Decimal(self)
1103 L = list(self._int)
1104 L[-1] += 1
1105 spot = len(L)-1
1106 while L[spot] == 10:
1107 L[spot] = 0
1108 if spot == 0:
1109 L[0:0] = [1]
1110 break
1111 L[spot-1] += 1
1112 spot -= 1
1113 ans = Decimal((self._sign, L, self._exp))
1115 if context is None:
1116 context = getcontext()
1117 if round and context._rounding_decision == ALWAYS_ROUND:
1118 ans = ans._fix(context)
1119 return ans
1121 def __mul__(self, other, context=None):
1122 """Return self * other.
1124 (+-) INF * 0 (or its reverse) raise InvalidOperation.
1126 other = _convert_other(other)
1127 if other is NotImplemented:
1128 return other
1130 if context is None:
1131 context = getcontext()
1133 resultsign = self._sign ^ other._sign
1135 if self._is_special or other._is_special:
1136 ans = self._check_nans(other, context)
1137 if ans:
1138 return ans
1140 if self._isinfinity():
1141 if not other:
1142 return context._raise_error(InvalidOperation, '(+-)INF * 0')
1143 return Infsign[resultsign]
1145 if other._isinfinity():
1146 if not self:
1147 return context._raise_error(InvalidOperation, '0 * (+-)INF')
1148 return Infsign[resultsign]
1150 resultexp = self._exp + other._exp
1151 shouldround = context._rounding_decision == ALWAYS_ROUND
1153 # Special case for multiplying by zero
1154 if not self or not other:
1155 ans = Decimal((resultsign, (0,), resultexp))
1156 if shouldround:
1157 # Fixing in case the exponent is out of bounds
1158 ans = ans._fix(context)
1159 return ans
1161 # Special case for multiplying by power of 10
1162 if self._int == (1,):
1163 ans = Decimal((resultsign, other._int, resultexp))
1164 if shouldround:
1165 ans = ans._fix(context)
1166 return ans
1167 if other._int == (1,):
1168 ans = Decimal((resultsign, self._int, resultexp))
1169 if shouldround:
1170 ans = ans._fix(context)
1171 return ans
1173 op1 = _WorkRep(self)
1174 op2 = _WorkRep(other)
1176 ans = Decimal((resultsign, map(int, str(op1.int * op2.int)), resultexp))
1177 if shouldround:
1178 ans = ans._fix(context)
1180 return ans
1181 __rmul__ = __mul__
1183 def __div__(self, other, context=None):
1184 """Return self / other."""
1185 return self._divide(other, context=context)
1186 __truediv__ = __div__
1188 def _divide(self, other, divmod = 0, context=None):
1189 """Return a / b, to context.prec precision.
1191 divmod:
1192 0 => true division
1193 1 => (a //b, a%b)
1194 2 => a //b
1195 3 => a%b
1197 Actually, if divmod is 2 or 3 a tuple is returned, but errors for
1198 computing the other value are not raised.
1200 other = _convert_other(other)
1201 if other is NotImplemented:
1202 if divmod in (0, 1):
1203 return NotImplemented
1204 return (NotImplemented, NotImplemented)
1206 if context is None:
1207 context = getcontext()
1209 sign = self._sign ^ other._sign
1211 if self._is_special or other._is_special:
1212 ans = self._check_nans(other, context)
1213 if ans:
1214 if divmod:
1215 return (ans, ans)
1216 return ans
1218 if self._isinfinity() and other._isinfinity():
1219 if divmod:
1220 return (context._raise_error(InvalidOperation,
1221 '(+-)INF // (+-)INF'),
1222 context._raise_error(InvalidOperation,
1223 '(+-)INF % (+-)INF'))
1224 return context._raise_error(InvalidOperation, '(+-)INF/(+-)INF')
1226 if self._isinfinity():
1227 if divmod == 1:
1228 return (Infsign[sign],
1229 context._raise_error(InvalidOperation, 'INF % x'))
1230 elif divmod == 2:
1231 return (Infsign[sign], NaN)
1232 elif divmod == 3:
1233 return (Infsign[sign],
1234 context._raise_error(InvalidOperation, 'INF % x'))
1235 return Infsign[sign]
1237 if other._isinfinity():
1238 if divmod:
1239 return (Decimal((sign, (0,), 0)), Decimal(self))
1240 context._raise_error(Clamped, 'Division by infinity')
1241 return Decimal((sign, (0,), context.Etiny()))
1243 # Special cases for zeroes
1244 if not self and not other:
1245 if divmod:
1246 return context._raise_error(DivisionUndefined, '0 / 0', 1)
1247 return context._raise_error(DivisionUndefined, '0 / 0')
1249 if not self:
1250 if divmod:
1251 otherside = Decimal(self)
1252 otherside._exp = min(self._exp, other._exp)
1253 return (Decimal((sign, (0,), 0)), otherside)
1254 exp = self._exp - other._exp
1255 if exp < context.Etiny():
1256 exp = context.Etiny()
1257 context._raise_error(Clamped, '0e-x / y')
1258 if exp > context.Emax:
1259 exp = context.Emax
1260 context._raise_error(Clamped, '0e+x / y')
1261 return Decimal( (sign, (0,), exp) )
1263 if not other:
1264 if divmod:
1265 return context._raise_error(DivisionByZero, 'divmod(x,0)',
1266 sign, 1)
1267 return context._raise_error(DivisionByZero, 'x / 0', sign)
1269 # OK, so neither = 0, INF or NaN
1270 shouldround = context._rounding_decision == ALWAYS_ROUND
1272 # If we're dividing into ints, and self < other, stop.
1273 # self.__abs__(0) does not round.
1274 if divmod and (self.__abs__(0, context) < other.__abs__(0, context)):
1276 if divmod == 1 or divmod == 3:
1277 exp = min(self._exp, other._exp)
1278 ans2 = self._rescale(exp, context=context, watchexp=0)
1279 if shouldround:
1280 ans2 = ans2._fix(context)
1281 return (Decimal( (sign, (0,), 0) ),
1282 ans2)
1284 elif divmod == 2:
1285 # Don't round the mod part, if we don't need it.
1286 return (Decimal( (sign, (0,), 0) ), Decimal(self))
1288 op1 = _WorkRep(self)
1289 op2 = _WorkRep(other)
1290 op1, op2, adjust = _adjust_coefficients(op1, op2)
1291 res = _WorkRep( (sign, 0, (op1.exp - op2.exp)) )
1292 if divmod and res.exp > context.prec + 1:
1293 return context._raise_error(DivisionImpossible)
1295 prec_limit = 10 ** context.prec
1296 while 1:
1297 while op2.int <= op1.int:
1298 res.int += 1
1299 op1.int -= op2.int
1300 if res.exp == 0 and divmod:
1301 if res.int >= prec_limit and shouldround:
1302 return context._raise_error(DivisionImpossible)
1303 otherside = Decimal(op1)
1304 frozen = context._ignore_all_flags()
1306 exp = min(self._exp, other._exp)
1307 otherside = otherside._rescale(exp, context=context, watchexp=0)
1308 context._regard_flags(*frozen)
1309 if shouldround:
1310 otherside = otherside._fix(context)
1311 return (Decimal(res), otherside)
1313 if op1.int == 0 and adjust >= 0 and not divmod:
1314 break
1315 if res.int >= prec_limit and shouldround:
1316 if divmod:
1317 return context._raise_error(DivisionImpossible)
1318 shouldround=1
1319 # Really, the answer is a bit higher, so adding a one to
1320 # the end will make sure the rounding is right.
1321 if op1.int != 0:
1322 res.int *= 10
1323 res.int += 1
1324 res.exp -= 1
1326 break
1327 res.int *= 10
1328 res.exp -= 1
1329 adjust += 1
1330 op1.int *= 10
1331 op1.exp -= 1
1333 if res.exp == 0 and divmod and op2.int > op1.int:
1334 # Solves an error in precision. Same as a previous block.
1336 if res.int >= prec_limit and shouldround:
1337 return context._raise_error(DivisionImpossible)
1338 otherside = Decimal(op1)
1339 frozen = context._ignore_all_flags()
1341 exp = min(self._exp, other._exp)
1342 otherside = otherside._rescale(exp, context=context)
1344 context._regard_flags(*frozen)
1346 return (Decimal(res), otherside)
1348 ans = Decimal(res)
1349 if shouldround:
1350 ans = ans._fix(context)
1351 return ans
1353 def __rdiv__(self, other, context=None):
1354 """Swaps self/other and returns __div__."""
1355 other = _convert_other(other)
1356 if other is NotImplemented:
1357 return other
1358 return other.__div__(self, context=context)
1359 __rtruediv__ = __rdiv__
1361 def __divmod__(self, other, context=None):
1363 (self // other, self % other)
1365 return self._divide(other, 1, context)
1367 def __rdivmod__(self, other, context=None):
1368 """Swaps self/other and returns __divmod__."""
1369 other = _convert_other(other)
1370 if other is NotImplemented:
1371 return other
1372 return other.__divmod__(self, context=context)
1374 def __mod__(self, other, context=None):
1376 self % other
1378 other = _convert_other(other)
1379 if other is NotImplemented:
1380 return other
1382 if self._is_special or other._is_special:
1383 ans = self._check_nans(other, context)
1384 if ans:
1385 return ans
1387 if self and not other:
1388 return context._raise_error(InvalidOperation, 'x % 0')
1390 return self._divide(other, 3, context)[1]
1392 def __rmod__(self, other, context=None):
1393 """Swaps self/other and returns __mod__."""
1394 other = _convert_other(other)
1395 if other is NotImplemented:
1396 return other
1397 return other.__mod__(self, context=context)
1399 def remainder_near(self, other, context=None):
1401 Remainder nearest to 0- abs(remainder-near) <= other/2
1403 other = _convert_other(other)
1404 if other is NotImplemented:
1405 return other
1407 if self._is_special or other._is_special:
1408 ans = self._check_nans(other, context)
1409 if ans:
1410 return ans
1411 if self and not other:
1412 return context._raise_error(InvalidOperation, 'x % 0')
1414 if context is None:
1415 context = getcontext()
1416 # If DivisionImpossible causes an error, do not leave Rounded/Inexact
1417 # ignored in the calling function.
1418 context = context._shallow_copy()
1419 flags = context._ignore_flags(Rounded, Inexact)
1420 # Keep DivisionImpossible flags
1421 (side, r) = self.__divmod__(other, context=context)
1423 if r._isnan():
1424 context._regard_flags(*flags)
1425 return r
1427 context = context._shallow_copy()
1428 rounding = context._set_rounding_decision(NEVER_ROUND)
1430 if other._sign:
1431 comparison = other.__div__(Decimal(-2), context=context)
1432 else:
1433 comparison = other.__div__(Decimal(2), context=context)
1435 context._set_rounding_decision(rounding)
1436 context._regard_flags(*flags)
1438 s1, s2 = r._sign, comparison._sign
1439 r._sign, comparison._sign = 0, 0
1441 if r < comparison:
1442 r._sign, comparison._sign = s1, s2
1443 # Get flags now
1444 self.__divmod__(other, context=context)
1445 return r._fix(context)
1446 r._sign, comparison._sign = s1, s2
1448 rounding = context._set_rounding_decision(NEVER_ROUND)
1450 (side, r) = self.__divmod__(other, context=context)
1451 context._set_rounding_decision(rounding)
1452 if r._isnan():
1453 return r
1455 decrease = not side._iseven()
1456 rounding = context._set_rounding_decision(NEVER_ROUND)
1457 side = side.__abs__(context=context)
1458 context._set_rounding_decision(rounding)
1460 s1, s2 = r._sign, comparison._sign
1461 r._sign, comparison._sign = 0, 0
1462 if r > comparison or decrease and r == comparison:
1463 r._sign, comparison._sign = s1, s2
1464 context.prec += 1
1465 numbsquant = len(side.__add__(Decimal(1), context=context)._int)
1466 if numbsquant >= context.prec:
1467 context.prec -= 1
1468 return context._raise_error(DivisionImpossible)[1]
1469 context.prec -= 1
1470 if self._sign == other._sign:
1471 r = r.__sub__(other, context=context)
1472 else:
1473 r = r.__add__(other, context=context)
1474 else:
1475 r._sign, comparison._sign = s1, s2
1477 return r._fix(context)
1479 def __floordiv__(self, other, context=None):
1480 """self // other"""
1481 return self._divide(other, 2, context)[0]
1483 def __rfloordiv__(self, other, context=None):
1484 """Swaps self/other and returns __floordiv__."""
1485 other = _convert_other(other)
1486 if other is NotImplemented:
1487 return other
1488 return other.__floordiv__(self, context=context)
1490 def __float__(self):
1491 """Float representation."""
1492 return float(str(self))
1494 def __int__(self):
1495 """Converts self to an int, truncating if necessary."""
1496 if self._is_special:
1497 if self._isnan():
1498 context = getcontext()
1499 return context._raise_error(InvalidContext)
1500 elif self._isinfinity():
1501 raise OverflowError("Cannot convert infinity to long")
1502 if self._exp >= 0:
1503 s = ''.join(map(str, self._int)) + '0'*self._exp
1504 else:
1505 s = ''.join(map(str, self._int))[:self._exp]
1506 if s == '':
1507 s = '0'
1508 sign = '-'*self._sign
1509 return int(sign + s)
1511 def __long__(self):
1512 """Converts to a long.
1514 Equivalent to long(int(self))
1516 return long(self.__int__())
1518 def _fix(self, context):
1519 """Round if it is necessary to keep self within prec precision.
1521 Rounds and fixes the exponent. Does not raise on a sNaN.
1523 Arguments:
1524 self - Decimal instance
1525 context - context used.
1527 if self._is_special:
1528 return self
1529 if context is None:
1530 context = getcontext()
1531 prec = context.prec
1532 ans = self._fixexponents(context)
1533 if len(ans._int) > prec:
1534 ans = ans._round(prec, context=context)
1535 ans = ans._fixexponents(context)
1536 return ans
1538 def _fixexponents(self, context):
1539 """Fix the exponents and return a copy with the exponent in bounds.
1540 Only call if known to not be a special value.
1542 folddown = context._clamp
1543 Emin = context.Emin
1544 ans = self
1545 ans_adjusted = ans.adjusted()
1546 if ans_adjusted < Emin:
1547 Etiny = context.Etiny()
1548 if ans._exp < Etiny:
1549 if not ans:
1550 ans = Decimal(self)
1551 ans._exp = Etiny
1552 context._raise_error(Clamped)
1553 return ans
1554 ans = ans._rescale(Etiny, context=context)
1555 # It isn't zero, and exp < Emin => subnormal
1556 context._raise_error(Subnormal)
1557 if context.flags[Inexact]:
1558 context._raise_error(Underflow)
1559 else:
1560 if ans:
1561 # Only raise subnormal if non-zero.
1562 context._raise_error(Subnormal)
1563 else:
1564 Etop = context.Etop()
1565 if folddown and ans._exp > Etop:
1566 context._raise_error(Clamped)
1567 ans = ans._rescale(Etop, context=context)
1568 else:
1569 Emax = context.Emax
1570 if ans_adjusted > Emax:
1571 if not ans:
1572 ans = Decimal(self)
1573 ans._exp = Emax
1574 context._raise_error(Clamped)
1575 return ans
1576 context._raise_error(Inexact)
1577 context._raise_error(Rounded)
1578 c = context._raise_error(Overflow, 'above Emax', ans._sign)
1579 return c
1580 return ans
1582 def _round(self, prec=None, rounding=None, context=None):
1583 """Returns a rounded version of self.
1585 You can specify the precision or rounding method. Otherwise, the
1586 context determines it.
1589 if self._is_special:
1590 ans = self._check_nans(context=context)
1591 if ans:
1592 return ans
1594 if self._isinfinity():
1595 return Decimal(self)
1597 if context is None:
1598 context = getcontext()
1600 if rounding is None:
1601 rounding = context.rounding
1602 if prec is None:
1603 prec = context.prec
1605 if not self:
1606 if prec <= 0:
1607 dig = (0,)
1608 exp = len(self._int) - prec + self._exp
1609 else:
1610 dig = (0,) * prec
1611 exp = len(self._int) + self._exp - prec
1612 ans = Decimal((self._sign, dig, exp))
1613 context._raise_error(Rounded)
1614 return ans
1616 if prec == 0:
1617 temp = Decimal(self)
1618 temp._int = (0,)+temp._int
1619 prec = 1
1620 elif prec < 0:
1621 exp = self._exp + len(self._int) - prec - 1
1622 temp = Decimal( (self._sign, (0, 1), exp))
1623 prec = 1
1624 else:
1625 temp = Decimal(self)
1627 numdigits = len(temp._int)
1628 if prec == numdigits:
1629 return temp
1631 # See if we need to extend precision
1632 expdiff = prec - numdigits
1633 if expdiff > 0:
1634 tmp = list(temp._int)
1635 tmp.extend([0] * expdiff)
1636 ans = Decimal( (temp._sign, tmp, temp._exp - expdiff))
1637 return ans
1639 # OK, but maybe all the lost digits are 0.
1640 lostdigits = self._int[expdiff:]
1641 if lostdigits == (0,) * len(lostdigits):
1642 ans = Decimal( (temp._sign, temp._int[:prec], temp._exp - expdiff))
1643 # Rounded, but not Inexact
1644 context._raise_error(Rounded)
1645 return ans
1647 # Okay, let's round and lose data
1649 this_function = getattr(temp, self._pick_rounding_function[rounding])
1650 # Now we've got the rounding function
1652 if prec != context.prec:
1653 context = context._shallow_copy()
1654 context.prec = prec
1655 ans = this_function(prec, expdiff, context)
1656 context._raise_error(Rounded)
1657 context._raise_error(Inexact, 'Changed in rounding')
1659 return ans
1661 _pick_rounding_function = {}
1663 def _round_down(self, prec, expdiff, context):
1664 """Also known as round-towards-0, truncate."""
1665 return Decimal( (self._sign, self._int[:prec], self._exp - expdiff) )
1667 def _round_half_up(self, prec, expdiff, context, tmp = None):
1668 """Rounds 5 up (away from 0)"""
1670 if tmp is None:
1671 tmp = Decimal( (self._sign,self._int[:prec], self._exp - expdiff))
1672 if self._int[prec] >= 5:
1673 tmp = tmp._increment(round=0, context=context)
1674 if len(tmp._int) > prec:
1675 return Decimal( (tmp._sign, tmp._int[:-1], tmp._exp + 1))
1676 return tmp
1678 def _round_half_even(self, prec, expdiff, context):
1679 """Round 5 to even, rest to nearest."""
1681 tmp = Decimal( (self._sign, self._int[:prec], self._exp - expdiff))
1682 half = (self._int[prec] == 5)
1683 if half:
1684 for digit in self._int[prec+1:]:
1685 if digit != 0:
1686 half = 0
1687 break
1688 if half:
1689 if self._int[prec-1] & 1 == 0:
1690 return tmp
1691 return self._round_half_up(prec, expdiff, context, tmp)
1693 def _round_half_down(self, prec, expdiff, context):
1694 """Round 5 down"""
1696 tmp = Decimal( (self._sign, self._int[:prec], self._exp - expdiff))
1697 half = (self._int[prec] == 5)
1698 if half:
1699 for digit in self._int[prec+1:]:
1700 if digit != 0:
1701 half = 0
1702 break
1703 if half:
1704 return tmp
1705 return self._round_half_up(prec, expdiff, context, tmp)
1707 def _round_up(self, prec, expdiff, context):
1708 """Rounds away from 0."""
1709 tmp = Decimal( (self._sign, self._int[:prec], self._exp - expdiff) )
1710 for digit in self._int[prec:]:
1711 if digit != 0:
1712 tmp = tmp._increment(round=1, context=context)
1713 if len(tmp._int) > prec:
1714 return Decimal( (tmp._sign, tmp._int[:-1], tmp._exp + 1))
1715 else:
1716 return tmp
1717 return tmp
1719 def _round_ceiling(self, prec, expdiff, context):
1720 """Rounds up (not away from 0 if negative.)"""
1721 if self._sign:
1722 return self._round_down(prec, expdiff, context)
1723 else:
1724 return self._round_up(prec, expdiff, context)
1726 def _round_floor(self, prec, expdiff, context):
1727 """Rounds down (not towards 0 if negative)"""
1728 if not self._sign:
1729 return self._round_down(prec, expdiff, context)
1730 else:
1731 return self._round_up(prec, expdiff, context)
1733 def __pow__(self, n, modulo = None, context=None):
1734 """Return self ** n (mod modulo)
1736 If modulo is None (default), don't take it mod modulo.
1738 n = _convert_other(n)
1739 if n is NotImplemented:
1740 return n
1742 if context is None:
1743 context = getcontext()
1745 if self._is_special or n._is_special or n.adjusted() > 8:
1746 # Because the spot << doesn't work with really big exponents
1747 if n._isinfinity() or n.adjusted() > 8:
1748 return context._raise_error(InvalidOperation, 'x ** INF')
1750 ans = self._check_nans(n, context)
1751 if ans:
1752 return ans
1754 if not n._isinteger():
1755 return context._raise_error(InvalidOperation, 'x ** (non-integer)')
1757 if not self and not n:
1758 return context._raise_error(InvalidOperation, '0 ** 0')
1760 if not n:
1761 return Decimal(1)
1763 if self == Decimal(1):
1764 return Decimal(1)
1766 sign = self._sign and not n._iseven()
1767 n = int(n)
1769 if self._isinfinity():
1770 if modulo:
1771 return context._raise_error(InvalidOperation, 'INF % x')
1772 if n > 0:
1773 return Infsign[sign]
1774 return Decimal( (sign, (0,), 0) )
1776 # With ludicrously large exponent, just raise an overflow
1777 # and return inf.
1778 if not modulo and n > 0 and \
1779 (self._exp + len(self._int) - 1) * n > context.Emax and self:
1781 tmp = Decimal('inf')
1782 tmp._sign = sign
1783 context._raise_error(Rounded)
1784 context._raise_error(Inexact)
1785 context._raise_error(Overflow, 'Big power', sign)
1786 return tmp
1788 elength = len(str(abs(n)))
1789 firstprec = context.prec
1791 if not modulo and firstprec + elength + 1 > DefaultContext.Emax:
1792 return context._raise_error(Overflow, 'Too much precision.', sign)
1794 mul = Decimal(self)
1795 val = Decimal(1)
1796 context = context._shallow_copy()
1797 context.prec = firstprec + elength + 1
1798 if n < 0:
1799 # n is a long now, not Decimal instance
1800 n = -n
1801 mul = Decimal(1).__div__(mul, context=context)
1803 spot = 1
1804 while spot <= n:
1805 spot <<= 1
1807 spot >>= 1
1808 # spot is the highest power of 2 less than n
1809 while spot:
1810 val = val.__mul__(val, context=context)
1811 if val._isinfinity():
1812 val = Infsign[sign]
1813 break
1814 if spot & n:
1815 val = val.__mul__(mul, context=context)
1816 if modulo is not None:
1817 val = val.__mod__(modulo, context=context)
1818 spot >>= 1
1819 context.prec = firstprec
1821 if context._rounding_decision == ALWAYS_ROUND:
1822 return val._fix(context)
1823 return val
1825 def __rpow__(self, other, context=None):
1826 """Swaps self/other and returns __pow__."""
1827 other = _convert_other(other)
1828 if other is NotImplemented:
1829 return other
1830 return other.__pow__(self, context=context)
1832 def normalize(self, context=None):
1833 """Normalize- strip trailing 0s, change anything equal to 0 to 0e0"""
1835 if self._is_special:
1836 ans = self._check_nans(context=context)
1837 if ans:
1838 return ans
1840 dup = self._fix(context)
1841 if dup._isinfinity():
1842 return dup
1844 if not dup:
1845 return Decimal( (dup._sign, (0,), 0) )
1846 end = len(dup._int)
1847 exp = dup._exp
1848 while dup._int[end-1] == 0:
1849 exp += 1
1850 end -= 1
1851 return Decimal( (dup._sign, dup._int[:end], exp) )
1854 def quantize(self, exp, rounding=None, context=None, watchexp=1):
1855 """Quantize self so its exponent is the same as that of exp.
1857 Similar to self._rescale(exp._exp) but with error checking.
1859 if self._is_special or exp._is_special:
1860 ans = self._check_nans(exp, context)
1861 if ans:
1862 return ans
1864 if exp._isinfinity() or self._isinfinity():
1865 if exp._isinfinity() and self._isinfinity():
1866 return self # if both are inf, it is OK
1867 if context is None:
1868 context = getcontext()
1869 return context._raise_error(InvalidOperation,
1870 'quantize with one INF')
1871 return self._rescale(exp._exp, rounding, context, watchexp)
1873 def same_quantum(self, other):
1874 """Test whether self and other have the same exponent.
1876 same as self._exp == other._exp, except NaN == sNaN
1878 if self._is_special or other._is_special:
1879 if self._isnan() or other._isnan():
1880 return self._isnan() and other._isnan() and True
1881 if self._isinfinity() or other._isinfinity():
1882 return self._isinfinity() and other._isinfinity() and True
1883 return self._exp == other._exp
1885 def _rescale(self, exp, rounding=None, context=None, watchexp=1):
1886 """Rescales so that the exponent is exp.
1888 exp = exp to scale to (an integer)
1889 rounding = rounding version
1890 watchexp: if set (default) an error is returned if exp is greater
1891 than Emax or less than Etiny.
1893 if context is None:
1894 context = getcontext()
1896 if self._is_special:
1897 if self._isinfinity():
1898 return context._raise_error(InvalidOperation, 'rescale with an INF')
1900 ans = self._check_nans(context=context)
1901 if ans:
1902 return ans
1904 if watchexp and (context.Emax < exp or context.Etiny() > exp):
1905 return context._raise_error(InvalidOperation, 'rescale(a, INF)')
1907 if not self:
1908 ans = Decimal(self)
1909 ans._int = (0,)
1910 ans._exp = exp
1911 return ans
1913 diff = self._exp - exp
1914 digits = len(self._int) + diff
1916 if watchexp and digits > context.prec:
1917 return context._raise_error(InvalidOperation, 'Rescale > prec')
1919 tmp = Decimal(self)
1920 tmp._int = (0,) + tmp._int
1921 digits += 1
1923 if digits < 0:
1924 tmp._exp = -digits + tmp._exp
1925 tmp._int = (0,1)
1926 digits = 1
1927 tmp = tmp._round(digits, rounding, context=context)
1929 if tmp._int[0] == 0 and len(tmp._int) > 1:
1930 tmp._int = tmp._int[1:]
1931 tmp._exp = exp
1933 tmp_adjusted = tmp.adjusted()
1934 if tmp and tmp_adjusted < context.Emin:
1935 context._raise_error(Subnormal)
1936 elif tmp and tmp_adjusted > context.Emax:
1937 return context._raise_error(InvalidOperation, 'rescale(a, INF)')
1938 return tmp
1940 def to_integral(self, rounding=None, context=None):
1941 """Rounds to the nearest integer, without raising inexact, rounded."""
1942 if self._is_special:
1943 ans = self._check_nans(context=context)
1944 if ans:
1945 return ans
1946 if self._exp >= 0:
1947 return self
1948 if context is None:
1949 context = getcontext()
1950 flags = context._ignore_flags(Rounded, Inexact)
1951 ans = self._rescale(0, rounding, context=context)
1952 context._regard_flags(flags)
1953 return ans
1955 def sqrt(self, context=None):
1956 """Return the square root of self.
1958 Uses a converging algorithm (Xn+1 = 0.5*(Xn + self / Xn))
1959 Should quadratically approach the right answer.
1961 if self._is_special:
1962 ans = self._check_nans(context=context)
1963 if ans:
1964 return ans
1966 if self._isinfinity() and self._sign == 0:
1967 return Decimal(self)
1969 if not self:
1970 # exponent = self._exp / 2, using round_down.
1971 # if self._exp < 0:
1972 # exp = (self._exp+1) // 2
1973 # else:
1974 exp = (self._exp) // 2
1975 if self._sign == 1:
1976 # sqrt(-0) = -0
1977 return Decimal( (1, (0,), exp))
1978 else:
1979 return Decimal( (0, (0,), exp))
1981 if context is None:
1982 context = getcontext()
1984 if self._sign == 1:
1985 return context._raise_error(InvalidOperation, 'sqrt(-x), x > 0')
1987 tmp = Decimal(self)
1989 expadd = tmp._exp // 2
1990 if tmp._exp & 1:
1991 tmp._int += (0,)
1992 tmp._exp = 0
1993 else:
1994 tmp._exp = 0
1996 context = context._shallow_copy()
1997 flags = context._ignore_all_flags()
1998 firstprec = context.prec
1999 context.prec = 3
2000 if tmp.adjusted() & 1 == 0:
2001 ans = Decimal( (0, (8,1,9), tmp.adjusted() - 2) )
2002 ans = ans.__add__(tmp.__mul__(Decimal((0, (2,5,9), -2)),
2003 context=context), context=context)
2004 ans._exp -= 1 + tmp.adjusted() // 2
2005 else:
2006 ans = Decimal( (0, (2,5,9), tmp._exp + len(tmp._int)- 3) )
2007 ans = ans.__add__(tmp.__mul__(Decimal((0, (8,1,9), -3)),
2008 context=context), context=context)
2009 ans._exp -= 1 + tmp.adjusted() // 2
2011 # ans is now a linear approximation.
2012 Emax, Emin = context.Emax, context.Emin
2013 context.Emax, context.Emin = DefaultContext.Emax, DefaultContext.Emin
2015 half = Decimal('0.5')
2017 maxp = firstprec + 2
2018 rounding = context._set_rounding(ROUND_HALF_EVEN)
2019 while 1:
2020 context.prec = min(2*context.prec - 2, maxp)
2021 ans = half.__mul__(ans.__add__(tmp.__div__(ans, context=context),
2022 context=context), context=context)
2023 if context.prec == maxp:
2024 break
2026 # Round to the answer's precision-- the only error can be 1 ulp.
2027 context.prec = firstprec
2028 prevexp = ans.adjusted()
2029 ans = ans._round(context=context)
2031 # Now, check if the other last digits are better.
2032 context.prec = firstprec + 1
2033 # In case we rounded up another digit and we should actually go lower.
2034 if prevexp != ans.adjusted():
2035 ans._int += (0,)
2036 ans._exp -= 1
2039 lower = ans.__sub__(Decimal((0, (5,), ans._exp-1)), context=context)
2040 context._set_rounding(ROUND_UP)
2041 if lower.__mul__(lower, context=context) > (tmp):
2042 ans = ans.__sub__(Decimal((0, (1,), ans._exp)), context=context)
2044 else:
2045 upper = ans.__add__(Decimal((0, (5,), ans._exp-1)),context=context)
2046 context._set_rounding(ROUND_DOWN)
2047 if upper.__mul__(upper, context=context) < tmp:
2048 ans = ans.__add__(Decimal((0, (1,), ans._exp)),context=context)
2050 ans._exp += expadd
2052 context.prec = firstprec
2053 context.rounding = rounding
2054 ans = ans._fix(context)
2056 rounding = context._set_rounding_decision(NEVER_ROUND)
2057 if not ans.__mul__(ans, context=context) == self:
2058 # Only rounded/inexact if here.
2059 context._regard_flags(flags)
2060 context._raise_error(Rounded)
2061 context._raise_error(Inexact)
2062 else:
2063 # Exact answer, so let's set the exponent right.
2064 # if self._exp < 0:
2065 # exp = (self._exp +1)// 2
2066 # else:
2067 exp = self._exp // 2
2068 context.prec += ans._exp - exp
2069 ans = ans._rescale(exp, context=context)
2070 context.prec = firstprec
2071 context._regard_flags(flags)
2072 context.Emax, context.Emin = Emax, Emin
2074 return ans._fix(context)
2076 def max(self, other, context=None):
2077 """Returns the larger value.
2079 like max(self, other) except if one is not a number, returns
2080 NaN (and signals if one is sNaN). Also rounds.
2082 other = _convert_other(other)
2083 if other is NotImplemented:
2084 return other
2086 if self._is_special or other._is_special:
2087 # If one operand is a quiet NaN and the other is number, then the
2088 # number is always returned
2089 sn = self._isnan()
2090 on = other._isnan()
2091 if sn or on:
2092 if on == 1 and sn != 2:
2093 return self
2094 if sn == 1 and on != 2:
2095 return other
2096 return self._check_nans(other, context)
2098 ans = self
2099 c = self.__cmp__(other)
2100 if c == 0:
2101 # If both operands are finite and equal in numerical value
2102 # then an ordering is applied:
2104 # If the signs differ then max returns the operand with the
2105 # positive sign and min returns the operand with the negative sign
2107 # If the signs are the same then the exponent is used to select
2108 # the result.
2109 if self._sign != other._sign:
2110 if self._sign:
2111 ans = other
2112 elif self._exp < other._exp and not self._sign:
2113 ans = other
2114 elif self._exp > other._exp and self._sign:
2115 ans = other
2116 elif c == -1:
2117 ans = other
2119 if context is None:
2120 context = getcontext()
2121 if context._rounding_decision == ALWAYS_ROUND:
2122 return ans._fix(context)
2123 return ans
2125 def min(self, other, context=None):
2126 """Returns the smaller value.
2128 Like min(self, other) except if one is not a number, returns
2129 NaN (and signals if one is sNaN). Also rounds.
2131 other = _convert_other(other)
2132 if other is NotImplemented:
2133 return other
2135 if self._is_special or other._is_special:
2136 # If one operand is a quiet NaN and the other is number, then the
2137 # number is always returned
2138 sn = self._isnan()
2139 on = other._isnan()
2140 if sn or on:
2141 if on == 1 and sn != 2:
2142 return self
2143 if sn == 1 and on != 2:
2144 return other
2145 return self._check_nans(other, context)
2147 ans = self
2148 c = self.__cmp__(other)
2149 if c == 0:
2150 # If both operands are finite and equal in numerical value
2151 # then an ordering is applied:
2153 # If the signs differ then max returns the operand with the
2154 # positive sign and min returns the operand with the negative sign
2156 # If the signs are the same then the exponent is used to select
2157 # the result.
2158 if self._sign != other._sign:
2159 if other._sign:
2160 ans = other
2161 elif self._exp > other._exp and not self._sign:
2162 ans = other
2163 elif self._exp < other._exp and self._sign:
2164 ans = other
2165 elif c == 1:
2166 ans = other
2168 if context is None:
2169 context = getcontext()
2170 if context._rounding_decision == ALWAYS_ROUND:
2171 return ans._fix(context)
2172 return ans
2174 def _isinteger(self):
2175 """Returns whether self is an integer"""
2176 if self._exp >= 0:
2177 return True
2178 rest = self._int[self._exp:]
2179 return rest == (0,)*len(rest)
2181 def _iseven(self):
2182 """Returns 1 if self is even. Assumes self is an integer."""
2183 if self._exp > 0:
2184 return 1
2185 return self._int[-1+self._exp] & 1 == 0
2187 def adjusted(self):
2188 """Return the adjusted exponent of self"""
2189 try:
2190 return self._exp + len(self._int) - 1
2191 # If NaN or Infinity, self._exp is string
2192 except TypeError:
2193 return 0
2195 # Support for pickling, copy, and deepcopy
2196 def __reduce__(self):
2197 return (self.__class__, (str(self),))
2199 def __copy__(self):
2200 if type(self) == Decimal:
2201 return self # I'm immutable; therefore I am my own clone
2202 return self.__class__(str(self))
2204 def __deepcopy__(self, memo):
2205 if type(self) == Decimal:
2206 return self # My components are also immutable
2207 return self.__class__(str(self))
2209 ##### Context class #######################################################
2212 # get rounding method function:
2213 rounding_functions = [name for name in Decimal.__dict__.keys()
2214 if name.startswith('_round_')]
2215 for name in rounding_functions:
2216 # name is like _round_half_even, goes to the global ROUND_HALF_EVEN value.
2217 globalname = name[1:].upper()
2218 val = globals()[globalname]
2219 Decimal._pick_rounding_function[val] = name
2221 del name, val, globalname, rounding_functions
2223 class _ContextManager(object):
2224 """Context manager class to support localcontext().
2226 Sets a copy of the supplied context in __enter__() and restores
2227 the previous decimal context in __exit__()
2229 def __init__(self, new_context):
2230 self.new_context = new_context.copy()
2231 def __enter__(self):
2232 self.saved_context = getcontext()
2233 setcontext(self.new_context)
2234 return self.new_context
2235 def __exit__(self, t, v, tb):
2236 setcontext(self.saved_context)
2238 class Context(object):
2239 """Contains the context for a Decimal instance.
2241 Contains:
2242 prec - precision (for use in rounding, division, square roots..)
2243 rounding - rounding type (how you round)
2244 _rounding_decision - ALWAYS_ROUND, NEVER_ROUND -- do you round?
2245 traps - If traps[exception] = 1, then the exception is
2246 raised when it is caused. Otherwise, a value is
2247 substituted in.
2248 flags - When an exception is caused, flags[exception] is incremented.
2249 (Whether or not the trap_enabler is set)
2250 Should be reset by user of Decimal instance.
2251 Emin - Minimum exponent
2252 Emax - Maximum exponent
2253 capitals - If 1, 1*10^1 is printed as 1E+1.
2254 If 0, printed as 1e1
2255 _clamp - If 1, change exponents if too high (Default 0)
2258 def __init__(self, prec=None, rounding=None,
2259 traps=None, flags=None,
2260 _rounding_decision=None,
2261 Emin=None, Emax=None,
2262 capitals=None, _clamp=0,
2263 _ignored_flags=None):
2264 if flags is None:
2265 flags = []
2266 if _ignored_flags is None:
2267 _ignored_flags = []
2268 if not isinstance(flags, dict):
2269 flags = dict([(s,s in flags) for s in _signals])
2270 del s
2271 if traps is not None and not isinstance(traps, dict):
2272 traps = dict([(s,s in traps) for s in _signals])
2273 del s
2274 for name, val in locals().items():
2275 if val is None:
2276 setattr(self, name, _copy.copy(getattr(DefaultContext, name)))
2277 else:
2278 setattr(self, name, val)
2279 del self.self
2281 def __repr__(self):
2282 """Show the current context."""
2283 s = []
2284 s.append('Context(prec=%(prec)d, rounding=%(rounding)s, '
2285 'Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d'
2286 % vars(self))
2287 names = [f.__name__ for f, v in self.flags.items() if v]
2288 s.append('flags=[' + ', '.join(names) + ']')
2289 names = [t.__name__ for t, v in self.traps.items() if v]
2290 s.append('traps=[' + ', '.join(names) + ']')
2291 return ', '.join(s) + ')'
2293 def clear_flags(self):
2294 """Reset all flags to zero"""
2295 for flag in self.flags:
2296 self.flags[flag] = 0
2298 def _shallow_copy(self):
2299 """Returns a shallow copy from self."""
2300 nc = Context(self.prec, self.rounding, self.traps, self.flags,
2301 self._rounding_decision, self.Emin, self.Emax,
2302 self.capitals, self._clamp, self._ignored_flags)
2303 return nc
2305 def copy(self):
2306 """Returns a deep copy from self."""
2307 nc = Context(self.prec, self.rounding, self.traps.copy(),
2308 self.flags.copy(), self._rounding_decision, self.Emin,
2309 self.Emax, self.capitals, self._clamp, self._ignored_flags)
2310 return nc
2311 __copy__ = copy
2313 def _raise_error(self, condition, explanation = None, *args):
2314 """Handles an error
2316 If the flag is in _ignored_flags, returns the default response.
2317 Otherwise, it increments the flag, then, if the corresponding
2318 trap_enabler is set, it reaises the exception. Otherwise, it returns
2319 the default value after incrementing the flag.
2321 error = _condition_map.get(condition, condition)
2322 if error in self._ignored_flags:
2323 # Don't touch the flag
2324 return error().handle(self, *args)
2326 self.flags[error] += 1
2327 if not self.traps[error]:
2328 # The errors define how to handle themselves.
2329 return condition().handle(self, *args)
2331 # Errors should only be risked on copies of the context
2332 # self._ignored_flags = []
2333 raise error, explanation
2335 def _ignore_all_flags(self):
2336 """Ignore all flags, if they are raised"""
2337 return self._ignore_flags(*_signals)
2339 def _ignore_flags(self, *flags):
2340 """Ignore the flags, if they are raised"""
2341 # Do not mutate-- This way, copies of a context leave the original
2342 # alone.
2343 self._ignored_flags = (self._ignored_flags + list(flags))
2344 return list(flags)
2346 def _regard_flags(self, *flags):
2347 """Stop ignoring the flags, if they are raised"""
2348 if flags and isinstance(flags[0], (tuple,list)):
2349 flags = flags[0]
2350 for flag in flags:
2351 self._ignored_flags.remove(flag)
2353 def __hash__(self):
2354 """A Context cannot be hashed."""
2355 # We inherit object.__hash__, so we must deny this explicitly
2356 raise TypeError("Cannot hash a Context.")
2358 def Etiny(self):
2359 """Returns Etiny (= Emin - prec + 1)"""
2360 return int(self.Emin - self.prec + 1)
2362 def Etop(self):
2363 """Returns maximum exponent (= Emax - prec + 1)"""
2364 return int(self.Emax - self.prec + 1)
2366 def _set_rounding_decision(self, type):
2367 """Sets the rounding decision.
2369 Sets the rounding decision, and returns the current (previous)
2370 rounding decision. Often used like:
2372 context = context._shallow_copy()
2373 # That so you don't change the calling context
2374 # if an error occurs in the middle (say DivisionImpossible is raised).
2376 rounding = context._set_rounding_decision(NEVER_ROUND)
2377 instance = instance / Decimal(2)
2378 context._set_rounding_decision(rounding)
2380 This will make it not round for that operation.
2383 rounding = self._rounding_decision
2384 self._rounding_decision = type
2385 return rounding
2387 def _set_rounding(self, type):
2388 """Sets the rounding type.
2390 Sets the rounding type, and returns the current (previous)
2391 rounding type. Often used like:
2393 context = context.copy()
2394 # so you don't change the calling context
2395 # if an error occurs in the middle.
2396 rounding = context._set_rounding(ROUND_UP)
2397 val = self.__sub__(other, context=context)
2398 context._set_rounding(rounding)
2400 This will make it round up for that operation.
2402 rounding = self.rounding
2403 self.rounding= type
2404 return rounding
2406 def create_decimal(self, num='0'):
2407 """Creates a new Decimal instance but using self as context."""
2408 d = Decimal(num, context=self)
2409 return d._fix(self)
2411 # Methods
2412 def abs(self, a):
2413 """Returns the absolute value of the operand.
2415 If the operand is negative, the result is the same as using the minus
2416 operation on the operand. Otherwise, the result is the same as using
2417 the plus operation on the operand.
2419 >>> ExtendedContext.abs(Decimal('2.1'))
2420 Decimal("2.1")
2421 >>> ExtendedContext.abs(Decimal('-100'))
2422 Decimal("100")
2423 >>> ExtendedContext.abs(Decimal('101.5'))
2424 Decimal("101.5")
2425 >>> ExtendedContext.abs(Decimal('-101.5'))
2426 Decimal("101.5")
2428 return a.__abs__(context=self)
2430 def add(self, a, b):
2431 """Return the sum of the two operands.
2433 >>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))
2434 Decimal("19.00")
2435 >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
2436 Decimal("1.02E+4")
2438 return a.__add__(b, context=self)
2440 def _apply(self, a):
2441 return str(a._fix(self))
2443 def compare(self, a, b):
2444 """Compares values numerically.
2446 If the signs of the operands differ, a value representing each operand
2447 ('-1' if the operand is less than zero, '0' if the operand is zero or
2448 negative zero, or '1' if the operand is greater than zero) is used in
2449 place of that operand for the comparison instead of the actual
2450 operand.
2452 The comparison is then effected by subtracting the second operand from
2453 the first and then returning a value according to the result of the
2454 subtraction: '-1' if the result is less than zero, '0' if the result is
2455 zero or negative zero, or '1' if the result is greater than zero.
2457 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3'))
2458 Decimal("-1")
2459 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1'))
2460 Decimal("0")
2461 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10'))
2462 Decimal("0")
2463 >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1'))
2464 Decimal("1")
2465 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3'))
2466 Decimal("1")
2467 >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1'))
2468 Decimal("-1")
2470 return a.compare(b, context=self)
2472 def divide(self, a, b):
2473 """Decimal division in a specified context.
2475 >>> ExtendedContext.divide(Decimal('1'), Decimal('3'))
2476 Decimal("0.333333333")
2477 >>> ExtendedContext.divide(Decimal('2'), Decimal('3'))
2478 Decimal("0.666666667")
2479 >>> ExtendedContext.divide(Decimal('5'), Decimal('2'))
2480 Decimal("2.5")
2481 >>> ExtendedContext.divide(Decimal('1'), Decimal('10'))
2482 Decimal("0.1")
2483 >>> ExtendedContext.divide(Decimal('12'), Decimal('12'))
2484 Decimal("1")
2485 >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2'))
2486 Decimal("4.00")
2487 >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0'))
2488 Decimal("1.20")
2489 >>> ExtendedContext.divide(Decimal('1000'), Decimal('100'))
2490 Decimal("10")
2491 >>> ExtendedContext.divide(Decimal('1000'), Decimal('1'))
2492 Decimal("1000")
2493 >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
2494 Decimal("1.20E+6")
2496 return a.__div__(b, context=self)
2498 def divide_int(self, a, b):
2499 """Divides two numbers and returns the integer part of the result.
2501 >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3'))
2502 Decimal("0")
2503 >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3'))
2504 Decimal("3")
2505 >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3'))
2506 Decimal("3")
2508 return a.__floordiv__(b, context=self)
2510 def divmod(self, a, b):
2511 return a.__divmod__(b, context=self)
2513 def max(self, a,b):
2514 """max compares two values numerically and returns the maximum.
2516 If either operand is a NaN then the general rules apply.
2517 Otherwise, the operands are compared as as though by the compare
2518 operation. If they are numerically equal then the left-hand operand
2519 is chosen as the result. Otherwise the maximum (closer to positive
2520 infinity) of the two operands is chosen as the result.
2522 >>> ExtendedContext.max(Decimal('3'), Decimal('2'))
2523 Decimal("3")
2524 >>> ExtendedContext.max(Decimal('-10'), Decimal('3'))
2525 Decimal("3")
2526 >>> ExtendedContext.max(Decimal('1.0'), Decimal('1'))
2527 Decimal("1")
2528 >>> ExtendedContext.max(Decimal('7'), Decimal('NaN'))
2529 Decimal("7")
2531 return a.max(b, context=self)
2533 def min(self, a,b):
2534 """min compares two values numerically and returns the minimum.
2536 If either operand is a NaN then the general rules apply.
2537 Otherwise, the operands are compared as as though by the compare
2538 operation. If they are numerically equal then the left-hand operand
2539 is chosen as the result. Otherwise the minimum (closer to negative
2540 infinity) of the two operands is chosen as the result.
2542 >>> ExtendedContext.min(Decimal('3'), Decimal('2'))
2543 Decimal("2")
2544 >>> ExtendedContext.min(Decimal('-10'), Decimal('3'))
2545 Decimal("-10")
2546 >>> ExtendedContext.min(Decimal('1.0'), Decimal('1'))
2547 Decimal("1.0")
2548 >>> ExtendedContext.min(Decimal('7'), Decimal('NaN'))
2549 Decimal("7")
2551 return a.min(b, context=self)
2553 def minus(self, a):
2554 """Minus corresponds to unary prefix minus in Python.
2556 The operation is evaluated using the same rules as subtract; the
2557 operation minus(a) is calculated as subtract('0', a) where the '0'
2558 has the same exponent as the operand.
2560 >>> ExtendedContext.minus(Decimal('1.3'))
2561 Decimal("-1.3")
2562 >>> ExtendedContext.minus(Decimal('-1.3'))
2563 Decimal("1.3")
2565 return a.__neg__(context=self)
2567 def multiply(self, a, b):
2568 """multiply multiplies two operands.
2570 If either operand is a special value then the general rules apply.
2571 Otherwise, the operands are multiplied together ('long multiplication'),
2572 resulting in a number which may be as long as the sum of the lengths
2573 of the two operands.
2575 >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3'))
2576 Decimal("3.60")
2577 >>> ExtendedContext.multiply(Decimal('7'), Decimal('3'))
2578 Decimal("21")
2579 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8'))
2580 Decimal("0.72")
2581 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0'))
2582 Decimal("-0.0")
2583 >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321'))
2584 Decimal("4.28135971E+11")
2586 return a.__mul__(b, context=self)
2588 def normalize(self, a):
2589 """normalize reduces an operand to its simplest form.
2591 Essentially a plus operation with all trailing zeros removed from the
2592 result.
2594 >>> ExtendedContext.normalize(Decimal('2.1'))
2595 Decimal("2.1")
2596 >>> ExtendedContext.normalize(Decimal('-2.0'))
2597 Decimal("-2")
2598 >>> ExtendedContext.normalize(Decimal('1.200'))
2599 Decimal("1.2")
2600 >>> ExtendedContext.normalize(Decimal('-120'))
2601 Decimal("-1.2E+2")
2602 >>> ExtendedContext.normalize(Decimal('120.00'))
2603 Decimal("1.2E+2")
2604 >>> ExtendedContext.normalize(Decimal('0.00'))
2605 Decimal("0")
2607 return a.normalize(context=self)
2609 def plus(self, a):
2610 """Plus corresponds to unary prefix plus in Python.
2612 The operation is evaluated using the same rules as add; the
2613 operation plus(a) is calculated as add('0', a) where the '0'
2614 has the same exponent as the operand.
2616 >>> ExtendedContext.plus(Decimal('1.3'))
2617 Decimal("1.3")
2618 >>> ExtendedContext.plus(Decimal('-1.3'))
2619 Decimal("-1.3")
2621 return a.__pos__(context=self)
2623 def power(self, a, b, modulo=None):
2624 """Raises a to the power of b, to modulo if given.
2626 The right-hand operand must be a whole number whose integer part (after
2627 any exponent has been applied) has no more than 9 digits and whose
2628 fractional part (if any) is all zeros before any rounding. The operand
2629 may be positive, negative, or zero; if negative, the absolute value of
2630 the power is used, and the left-hand operand is inverted (divided into
2631 1) before use.
2633 If the increased precision needed for the intermediate calculations
2634 exceeds the capabilities of the implementation then an Invalid
2635 operation condition is raised.
2637 If, when raising to a negative power, an underflow occurs during the
2638 division into 1, the operation is not halted at that point but
2639 continues.
2641 >>> ExtendedContext.power(Decimal('2'), Decimal('3'))
2642 Decimal("8")
2643 >>> ExtendedContext.power(Decimal('2'), Decimal('-3'))
2644 Decimal("0.125")
2645 >>> ExtendedContext.power(Decimal('1.7'), Decimal('8'))
2646 Decimal("69.7575744")
2647 >>> ExtendedContext.power(Decimal('Infinity'), Decimal('-2'))
2648 Decimal("0")
2649 >>> ExtendedContext.power(Decimal('Infinity'), Decimal('-1'))
2650 Decimal("0")
2651 >>> ExtendedContext.power(Decimal('Infinity'), Decimal('0'))
2652 Decimal("1")
2653 >>> ExtendedContext.power(Decimal('Infinity'), Decimal('1'))
2654 Decimal("Infinity")
2655 >>> ExtendedContext.power(Decimal('Infinity'), Decimal('2'))
2656 Decimal("Infinity")
2657 >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('-2'))
2658 Decimal("0")
2659 >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('-1'))
2660 Decimal("-0")
2661 >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('0'))
2662 Decimal("1")
2663 >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('1'))
2664 Decimal("-Infinity")
2665 >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('2'))
2666 Decimal("Infinity")
2667 >>> ExtendedContext.power(Decimal('0'), Decimal('0'))
2668 Decimal("NaN")
2670 return a.__pow__(b, modulo, context=self)
2672 def quantize(self, a, b):
2673 """Returns a value equal to 'a' (rounded), having the exponent of 'b'.
2675 The coefficient of the result is derived from that of the left-hand
2676 operand. It may be rounded using the current rounding setting (if the
2677 exponent is being increased), multiplied by a positive power of ten (if
2678 the exponent is being decreased), or is unchanged (if the exponent is
2679 already equal to that of the right-hand operand).
2681 Unlike other operations, if the length of the coefficient after the
2682 quantize operation would be greater than precision then an Invalid
2683 operation condition is raised. This guarantees that, unless there is
2684 an error condition, the exponent of the result of a quantize is always
2685 equal to that of the right-hand operand.
2687 Also unlike other operations, quantize will never raise Underflow, even
2688 if the result is subnormal and inexact.
2690 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
2691 Decimal("2.170")
2692 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
2693 Decimal("2.17")
2694 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
2695 Decimal("2.2")
2696 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
2697 Decimal("2")
2698 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
2699 Decimal("0E+1")
2700 >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
2701 Decimal("-Infinity")
2702 >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
2703 Decimal("NaN")
2704 >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
2705 Decimal("-0")
2706 >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
2707 Decimal("-0E+5")
2708 >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
2709 Decimal("NaN")
2710 >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
2711 Decimal("NaN")
2712 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
2713 Decimal("217.0")
2714 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
2715 Decimal("217")
2716 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
2717 Decimal("2.2E+2")
2718 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
2719 Decimal("2E+2")
2721 return a.quantize(b, context=self)
2723 def remainder(self, a, b):
2724 """Returns the remainder from integer division.
2726 The result is the residue of the dividend after the operation of
2727 calculating integer division as described for divide-integer, rounded
2728 to precision digits if necessary. The sign of the result, if
2729 non-zero, is the same as that of the original dividend.
2731 This operation will fail under the same conditions as integer division
2732 (that is, if integer division on the same two operands would fail, the
2733 remainder cannot be calculated).
2735 >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3'))
2736 Decimal("2.1")
2737 >>> ExtendedContext.remainder(Decimal('10'), Decimal('3'))
2738 Decimal("1")
2739 >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3'))
2740 Decimal("-1")
2741 >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1'))
2742 Decimal("0.2")
2743 >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3'))
2744 Decimal("0.1")
2745 >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3'))
2746 Decimal("1.0")
2748 return a.__mod__(b, context=self)
2750 def remainder_near(self, a, b):
2751 """Returns to be "a - b * n", where n is the integer nearest the exact
2752 value of "x / b" (if two integers are equally near then the even one
2753 is chosen). If the result is equal to 0 then its sign will be the
2754 sign of a.
2756 This operation will fail under the same conditions as integer division
2757 (that is, if integer division on the same two operands would fail, the
2758 remainder cannot be calculated).
2760 >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3'))
2761 Decimal("-0.9")
2762 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6'))
2763 Decimal("-2")
2764 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3'))
2765 Decimal("1")
2766 >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3'))
2767 Decimal("-1")
2768 >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1'))
2769 Decimal("0.2")
2770 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3'))
2771 Decimal("0.1")
2772 >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
2773 Decimal("-0.3")
2775 return a.remainder_near(b, context=self)
2777 def same_quantum(self, a, b):
2778 """Returns True if the two operands have the same exponent.
2780 The result is never affected by either the sign or the coefficient of
2781 either operand.
2783 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001'))
2784 False
2785 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01'))
2786 True
2787 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1'))
2788 False
2789 >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf'))
2790 True
2792 return a.same_quantum(b)
2794 def sqrt(self, a):
2795 """Square root of a non-negative number to context precision.
2797 If the result must be inexact, it is rounded using the round-half-even
2798 algorithm.
2800 >>> ExtendedContext.sqrt(Decimal('0'))
2801 Decimal("0")
2802 >>> ExtendedContext.sqrt(Decimal('-0'))
2803 Decimal("-0")
2804 >>> ExtendedContext.sqrt(Decimal('0.39'))
2805 Decimal("0.624499800")
2806 >>> ExtendedContext.sqrt(Decimal('100'))
2807 Decimal("10")
2808 >>> ExtendedContext.sqrt(Decimal('1'))
2809 Decimal("1")
2810 >>> ExtendedContext.sqrt(Decimal('1.0'))
2811 Decimal("1.0")
2812 >>> ExtendedContext.sqrt(Decimal('1.00'))
2813 Decimal("1.0")
2814 >>> ExtendedContext.sqrt(Decimal('7'))
2815 Decimal("2.64575131")
2816 >>> ExtendedContext.sqrt(Decimal('10'))
2817 Decimal("3.16227766")
2818 >>> ExtendedContext.prec
2821 return a.sqrt(context=self)
2823 def subtract(self, a, b):
2824 """Return the difference between the two operands.
2826 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07'))
2827 Decimal("0.23")
2828 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30'))
2829 Decimal("0.00")
2830 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07'))
2831 Decimal("-0.77")
2833 return a.__sub__(b, context=self)
2835 def to_eng_string(self, a):
2836 """Converts a number to a string, using scientific notation.
2838 The operation is not affected by the context.
2840 return a.to_eng_string(context=self)
2842 def to_sci_string(self, a):
2843 """Converts a number to a string, using scientific notation.
2845 The operation is not affected by the context.
2847 return a.__str__(context=self)
2849 def to_integral(self, a):
2850 """Rounds to an integer.
2852 When the operand has a negative exponent, the result is the same
2853 as using the quantize() operation using the given operand as the
2854 left-hand-operand, 1E+0 as the right-hand-operand, and the precision
2855 of the operand as the precision setting, except that no flags will
2856 be set. The rounding mode is taken from the context.
2858 >>> ExtendedContext.to_integral(Decimal('2.1'))
2859 Decimal("2")
2860 >>> ExtendedContext.to_integral(Decimal('100'))
2861 Decimal("100")
2862 >>> ExtendedContext.to_integral(Decimal('100.0'))
2863 Decimal("100")
2864 >>> ExtendedContext.to_integral(Decimal('101.5'))
2865 Decimal("102")
2866 >>> ExtendedContext.to_integral(Decimal('-101.5'))
2867 Decimal("-102")
2868 >>> ExtendedContext.to_integral(Decimal('10E+5'))
2869 Decimal("1.0E+6")
2870 >>> ExtendedContext.to_integral(Decimal('7.89E+77'))
2871 Decimal("7.89E+77")
2872 >>> ExtendedContext.to_integral(Decimal('-Inf'))
2873 Decimal("-Infinity")
2875 return a.to_integral(context=self)
2877 class _WorkRep(object):
2878 __slots__ = ('sign','int','exp')
2879 # sign: 0 or 1
2880 # int: int or long
2881 # exp: None, int, or string
2883 def __init__(self, value=None):
2884 if value is None:
2885 self.sign = None
2886 self.int = 0
2887 self.exp = None
2888 elif isinstance(value, Decimal):
2889 self.sign = value._sign
2890 cum = 0
2891 for digit in value._int:
2892 cum = cum * 10 + digit
2893 self.int = cum
2894 self.exp = value._exp
2895 else:
2896 # assert isinstance(value, tuple)
2897 self.sign = value[0]
2898 self.int = value[1]
2899 self.exp = value[2]
2901 def __repr__(self):
2902 return "(%r, %r, %r)" % (self.sign, self.int, self.exp)
2904 __str__ = __repr__
2908 def _normalize(op1, op2, shouldround = 0, prec = 0):
2909 """Normalizes op1, op2 to have the same exp and length of coefficient.
2911 Done during addition.
2913 # Yes, the exponent is a long, but the difference between exponents
2914 # must be an int-- otherwise you'd get a big memory problem.
2915 numdigits = int(op1.exp - op2.exp)
2916 if numdigits < 0:
2917 numdigits = -numdigits
2918 tmp = op2
2919 other = op1
2920 else:
2921 tmp = op1
2922 other = op2
2925 if shouldround and numdigits > prec + 1:
2926 # Big difference in exponents - check the adjusted exponents
2927 tmp_len = len(str(tmp.int))
2928 other_len = len(str(other.int))
2929 if numdigits > (other_len + prec + 1 - tmp_len):
2930 # If the difference in adjusted exps is > prec+1, we know
2931 # other is insignificant, so might as well put a 1 after the
2932 # precision (since this is only for addition). Also stops
2933 # use of massive longs.
2935 extend = prec + 2 - tmp_len
2936 if extend <= 0:
2937 extend = 1
2938 tmp.int *= 10 ** extend
2939 tmp.exp -= extend
2940 other.int = 1
2941 other.exp = tmp.exp
2942 return op1, op2
2944 tmp.int *= 10 ** numdigits
2945 tmp.exp -= numdigits
2946 return op1, op2
2948 def _adjust_coefficients(op1, op2):
2949 """Adjust op1, op2 so that op2.int * 10 > op1.int >= op2.int.
2951 Returns the adjusted op1, op2 as well as the change in op1.exp-op2.exp.
2953 Used on _WorkRep instances during division.
2955 adjust = 0
2956 # If op1 is smaller, make it larger
2957 while op2.int > op1.int:
2958 op1.int *= 10
2959 op1.exp -= 1
2960 adjust += 1
2962 # If op2 is too small, make it larger
2963 while op1.int >= (10 * op2.int):
2964 op2.int *= 10
2965 op2.exp -= 1
2966 adjust -= 1
2968 return op1, op2, adjust
2970 ##### Helper Functions ####################################################
2972 def _convert_other(other):
2973 """Convert other to Decimal.
2975 Verifies that it's ok to use in an implicit construction.
2977 if isinstance(other, Decimal):
2978 return other
2979 if isinstance(other, (int, long)):
2980 return Decimal(other)
2981 return NotImplemented
2983 _infinity_map = {
2984 'inf' : 1,
2985 'infinity' : 1,
2986 '+inf' : 1,
2987 '+infinity' : 1,
2988 '-inf' : -1,
2989 '-infinity' : -1
2992 def _isinfinity(num):
2993 """Determines whether a string or float is infinity.
2995 +1 for negative infinity; 0 for finite ; +1 for positive infinity
2997 num = str(num).lower()
2998 return _infinity_map.get(num, 0)
3000 def _isnan(num):
3001 """Determines whether a string or float is NaN
3003 (1, sign, diagnostic info as string) => NaN
3004 (2, sign, diagnostic info as string) => sNaN
3005 0 => not a NaN
3007 num = str(num).lower()
3008 if not num:
3009 return 0
3011 # Get the sign, get rid of trailing [+-]
3012 sign = 0
3013 if num[0] == '+':
3014 num = num[1:]
3015 elif num[0] == '-': # elif avoids '+-nan'
3016 num = num[1:]
3017 sign = 1
3019 if num.startswith('nan'):
3020 if len(num) > 3 and not num[3:].isdigit(): # diagnostic info
3021 return 0
3022 return (1, sign, num[3:].lstrip('0'))
3023 if num.startswith('snan'):
3024 if len(num) > 4 and not num[4:].isdigit():
3025 return 0
3026 return (2, sign, num[4:].lstrip('0'))
3027 return 0
3030 ##### Setup Specific Contexts ############################################
3032 # The default context prototype used by Context()
3033 # Is mutable, so that new contexts can have different default values
3035 DefaultContext = Context(
3036 prec=28, rounding=ROUND_HALF_EVEN,
3037 traps=[DivisionByZero, Overflow, InvalidOperation],
3038 flags=[],
3039 _rounding_decision=ALWAYS_ROUND,
3040 Emax=999999999,
3041 Emin=-999999999,
3042 capitals=1
3045 # Pre-made alternate contexts offered by the specification
3046 # Don't change these; the user should be able to select these
3047 # contexts and be able to reproduce results from other implementations
3048 # of the spec.
3050 BasicContext = Context(
3051 prec=9, rounding=ROUND_HALF_UP,
3052 traps=[DivisionByZero, Overflow, InvalidOperation, Clamped, Underflow],
3053 flags=[],
3056 ExtendedContext = Context(
3057 prec=9, rounding=ROUND_HALF_EVEN,
3058 traps=[],
3059 flags=[],
3063 ##### Useful Constants (internal use only) ################################
3065 # Reusable defaults
3066 Inf = Decimal('Inf')
3067 negInf = Decimal('-Inf')
3069 # Infsign[sign] is infinity w/ that sign
3070 Infsign = (Inf, negInf)
3072 NaN = Decimal('NaN')
3075 ##### crud for parsing strings #############################################
3076 import re
3078 # There's an optional sign at the start, and an optional exponent
3079 # at the end. The exponent has an optional sign and at least one
3080 # digit. In between, must have either at least one digit followed
3081 # by an optional fraction, or a decimal point followed by at least
3082 # one digit. Yuck.
3084 _parser = re.compile(r"""
3085 # \s*
3086 (?P<sign>[-+])?
3088 (?P<int>\d+) (\. (?P<frac>\d*))?
3090 \. (?P<onlyfrac>\d+)
3092 ([eE](?P<exp>[-+]? \d+))?
3093 # \s*
3095 """, re.VERBOSE).match # Uncomment the \s* to allow leading or trailing spaces.
3097 del re
3099 def _string2exact(s):
3100 """Return sign, n, p s.t.
3102 Float string value == -1**sign * n * 10**p exactly
3104 m = _parser(s)
3105 if m is None:
3106 raise ValueError("invalid literal for Decimal: %r" % s)
3108 if m.group('sign') == "-":
3109 sign = 1
3110 else:
3111 sign = 0
3113 exp = m.group('exp')
3114 if exp is None:
3115 exp = 0
3116 else:
3117 exp = int(exp)
3119 intpart = m.group('int')
3120 if intpart is None:
3121 intpart = ""
3122 fracpart = m.group('onlyfrac')
3123 else:
3124 fracpart = m.group('frac')
3125 if fracpart is None:
3126 fracpart = ""
3128 exp -= len(fracpart)
3130 mantissa = intpart + fracpart
3131 tmp = map(int, mantissa)
3132 backup = tmp
3133 while tmp and tmp[0] == 0:
3134 del tmp[0]
3136 # It's a zero
3137 if not tmp:
3138 if backup:
3139 return (sign, tuple(backup), exp)
3140 return (sign, (0,), exp)
3141 mantissa = tuple(tmp)
3143 return (sign, mantissa, exp)
3146 if __name__ == '__main__':
3147 import doctest, sys
3148 doctest.testmod(sys.modules[__name__])