1 # Copyright (c) 2004 Python Software Foundation.
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>
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.
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 the module is to support arithmetic using familiar
33 "schoolhouse" rules and to avoid the some of 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)
52 >>> Decimal("123.45e12345678901234567890")
53 Decimal("1.2345E+12345678901234567892")
54 >>> Decimal("1.33") + Decimal("1.27")
56 >>> Decimal("12.34") + Decimal("3.87") - Decimal("18.41")
59 >>> print dig / Decimal(3)
61 >>> getcontext().prec = 18
62 >>> print dig / Decimal(3)
66 >>> print Decimal(3).sqrt()
68 >>> print Decimal(3) ** 123
69 4.85192780976896427E+58
70 >>> inf = Decimal(1) / Decimal(0)
73 >>> neginf = Decimal(-1) / Decimal(0)
76 >>> print neginf + inf
78 >>> print neginf * inf
82 >>> getcontext().traps[DivisionByZero] = 1
84 Traceback (most recent call last):
90 >>> c.traps[InvalidOperation] = 0
91 >>> print c.flags[InvalidOperation]
93 >>> c.divide(Decimal(0), Decimal(0))
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]
120 'Decimal', 'Context',
123 'DefaultContext', 'BasicContext', 'ExtendedContext',
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'
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.
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
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
):
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
201 x._rescale( non-integer )
206 An operand is invalid
208 def handle(self
, context
, *args
):
210 if args
[0] == 1: #sNaN, must drop 's' but keep diagnostics
211 return Decimal( (args
[1]._sign
, args
[1]._int
, 'n') )
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):
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
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
238 def handle(self
, context
, sign
, double
= None, *args
):
239 if double
is not None:
240 return (Infsign
[sign
],)*2
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
):
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
):
264 return (NaN
, NaN
) #for 0 %0, 0 // 0
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.
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
):
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.
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.
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
341 def handle(self
, context
, sign
, *args
):
342 if context
.rounding
in (ROUND_HALF_UP
, ROUND_HALF_EVEN
,
343 ROUND_HALF_DOWN
, ROUND_UP
):
346 if context
.rounding
== ROUND_CEILING
:
348 return Decimal((sign
, (9,)*context
.prec
,
349 context
.Emax
-context
.prec
+1))
351 if context
.rounding
== ROUND_FLOOR
:
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.
393 # Python was compiled without threads; create a mock object instead
396 def local(self
, sys
=sys
):
397 return sys
.modules
[__name__
]
398 threading
= MockThreading()
399 del sys
, MockThreading
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
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.
426 return threading
.currentThread().__decimal
_context
__
427 except AttributeError:
429 threading
.currentThread().__decimal
_context
__ = context
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.
446 return _local
.__decimal
_context
__
447 except AttributeError:
449 _local
.__decimal
_context
__ = 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
462 ##### Decimal class ###########################################
464 class Decimal(object):
465 """Floating point class for decimal arithmetic."""
467 __slots__
= ('_exp','_int','_sign', '_is_special')
468 # Generally, the value of the Decimal instance is given by
469 # (-1)**_sign * _int * 10**_exp
470 # Special values are signified by _is_special == True
472 # We're immutable, so use __new__ not __init__
473 def __new__(cls
, value
="0", context
=None):
474 """Create a decimal point instance.
476 >>> Decimal('3.14') # string input
478 >>> Decimal((0, (3, 1, 4), -2)) # tuple input (sign, digit_tuple, exponent)
480 >>> Decimal(314) # int or long
482 >>> Decimal(Decimal(314)) # another decimal instance
486 self
= object.__new
__(cls
)
487 self
._is
_special
= False
489 # From an internal working value
490 if isinstance(value
, _WorkRep
):
491 self
._sign
= value
.sign
492 self
._int
= tuple(map(int, str(value
.int)))
493 self
._exp
= int(value
.exp
)
496 # From another decimal
497 if isinstance(value
, Decimal
):
498 self
._exp
= value
._exp
499 self
._sign
= value
._sign
500 self
._int
= value
._int
501 self
._is
_special
= value
._is
_special
505 if isinstance(value
, (int,long)):
511 self
._int
= tuple(map(int, str(abs(value
))))
514 # tuple/list conversion (possibly from as_tuple())
515 if isinstance(value
, (list,tuple)):
517 raise ValueError, 'Invalid arguments'
518 if value
[0] not in (0,1):
519 raise ValueError, 'Invalid sign'
520 for digit
in value
[1]:
521 if not isinstance(digit
, (int,long)) or digit
< 0:
522 raise ValueError, "The second value in the tuple must be composed of non negative integer elements."
524 self
._sign
= value
[0]
525 self
._int
= tuple(value
[1])
526 if value
[2] in ('F','n','N'):
528 self
._is
_special
= True
530 self
._exp
= int(value
[2])
533 if isinstance(value
, float):
534 raise TypeError("Cannot convert float to Decimal. " +
535 "First convert the float to a string")
537 # Other argument types may require the context during interpretation
539 context
= getcontext()
542 # REs insist on real strings, so we can too.
543 if isinstance(value
, basestring
):
544 if _isinfinity(value
):
547 self
._is
_special
= True
548 if _isinfinity(value
) == 1:
554 sig
, sign
, diag
= _isnan(value
)
555 self
._is
_special
= True
556 if len(diag
) > context
.prec
: #Diagnostic info too long
557 self
._sign
, self
._int
, self
._exp
= \
558 context
._raise
_error
(ConversionSyntax
)
561 self
._exp
= 'n' #qNaN
563 self
._exp
= 'N' #sNaN
565 self
._int
= tuple(map(int, diag
)) #Diagnostic info
568 self
._sign
, self
._int
, self
._exp
= _string2exact(value
)
570 self
._is
_special
= True
571 self
._sign
, self
._int
, self
._exp
= context
._raise
_error
(ConversionSyntax
)
574 raise TypeError("Cannot convert %r to Decimal" % value
)
577 """Returns whether the number is not actually one.
591 def _isinfinity(self
):
592 """Returns whether the number is infinite
594 0 if finite or not a number
604 def _check_nans(self
, other
= None, context
=None):
605 """Returns whether the number is not actually one.
607 if self, other are sNaN, signal
608 if self, other are NaN return nan
611 Done before operations.
614 self_is_nan
= self
._isnan
()
618 other_is_nan
= other
._isnan
()
620 if self_is_nan
or other_is_nan
:
622 context
= getcontext()
625 return context
._raise
_error
(InvalidOperation
, 'sNaN',
627 if other_is_nan
== 2:
628 return context
._raise
_error
(InvalidOperation
, 'sNaN',
636 def __nonzero__(self
):
637 """Is the number non-zero?
644 return sum(self
._int
) != 0
646 def __cmp__(self
, other
, context
=None):
647 other
= _convert_other(other
)
648 if other
is NotImplemented:
651 if self
._is
_special
or other
._is
_special
:
652 ans
= self
._check
_nans
(other
, context
)
654 return 1 # Comparison involving NaN's always reports self > other
657 return cmp(self
._isinfinity
(), other
._isinfinity
())
659 if not self
and not other
:
660 return 0 #If both 0, sign comparison isn't certain.
662 #If different signs, neg one is less
663 if other
._sign
< self
._sign
:
665 if self
._sign
< other
._sign
:
668 self_adjusted
= self
.adjusted()
669 other_adjusted
= other
.adjusted()
670 if self_adjusted
== other_adjusted
and \
671 self
._int
+ (0,)*(self
._exp
- other
._exp
) == \
672 other
._int
+ (0,)*(other
._exp
- self
._exp
):
673 return 0 #equal, except in precision. ([0]*(-x) = [])
674 elif self_adjusted
> other_adjusted
and self
._int
[0] != 0:
675 return (-1)**self
._sign
676 elif self_adjusted
< other_adjusted
and other
._int
[0] != 0:
677 return -((-1)**self
._sign
)
679 # Need to round, so make sure we have a valid context
681 context
= getcontext()
683 context
= context
._shallow
_copy
()
684 rounding
= context
._set
_rounding
(ROUND_UP
) #round away from 0
686 flags
= context
._ignore
_all
_flags
()
687 res
= self
.__sub
__(other
, context
=context
)
689 context
._regard
_flags
(*flags
)
691 context
.rounding
= rounding
699 def __eq__(self
, other
):
700 if not isinstance(other
, (Decimal
, int, long)):
701 return NotImplemented
702 return self
.__cmp
__(other
) == 0
704 def __ne__(self
, other
):
705 if not isinstance(other
, (Decimal
, int, long)):
706 return NotImplemented
707 return self
.__cmp
__(other
) != 0
709 def compare(self
, other
, context
=None):
710 """Compares one to another.
716 Like __cmp__, but returns Decimal instances.
718 other
= _convert_other(other
)
719 if other
is NotImplemented:
722 #compare(NaN, NaN) = NaN
723 if (self
._is
_special
or other
and other
._is
_special
):
724 ans
= self
._check
_nans
(other
, context
)
728 return Decimal(self
.__cmp
__(other
, context
))
731 """x.__hash__() <==> hash(x)"""
732 # Decimal integers must hash the same as the ints
733 # Non-integer decimals are normalized and hashed as strings
734 # Normalization assures that hast(100E-1) == hash(10)
737 raise TypeError('Cannot hash a NaN value.')
738 return hash(str(self
))
740 if self
== Decimal(i
):
742 assert self
.__nonzero
__() # '-0' handled by integer case
743 return hash(str(self
.normalize()))
746 """Represents the number as a triple tuple.
748 To show the internals exactly as they are.
750 return (self
._sign
, self
._int
, self
._exp
)
753 """Represents the number as an instance of Decimal."""
754 # Invariant: eval(repr(d)) == d
755 return 'Decimal("%s")' % str(self
)
757 def __str__(self
, eng
= 0, context
=None):
758 """Return string representation of the number in scientific notation.
760 Captures all of the information in the underlying representation.
765 minus
= '-'*self
._sign
766 if self
._int
== (0,):
769 info
= ''.join(map(str, self
._int
))
770 if self
._isnan
() == 2:
771 return minus
+ 'sNaN' + info
772 return minus
+ 'NaN' + info
773 if self
._isinfinity
():
774 minus
= '-'*self
._sign
775 return minus
+ 'Infinity'
778 context
= getcontext()
780 tmp
= map(str, self
._int
)
781 numdigits
= len(self
._int
)
782 leftdigits
= self
._exp
+ numdigits
783 if eng
and not self
: #self = 0eX wants 0[.0[0]]eY, not [[0]0]0eY
784 if self
._exp
< 0 and self
._exp
>= -6: #short, no need for e/E
785 s
= '-'*self
._sign
+ '0.' + '0'*(abs(self
._exp
))
787 #exp is closest mult. of 3 >= self._exp
788 exp
= ((self
._exp
- 1)// 3 + 1) * 3
790 s
= '0.'+'0'*(exp
- self
._exp
)
799 s
+= '+' #0.0e+3, not 0.0e3
801 s
= '-'*self
._sign
+ s
804 dotplace
= (leftdigits
-1)%3+1
805 adjexp
= leftdigits
-1 - (leftdigits
-1)%3
807 adjexp
= leftdigits
-1
811 elif self
._exp
< 0 and adjexp
>= 0:
812 tmp
.insert(leftdigits
, '.')
813 elif self
._exp
< 0 and adjexp
>= -6:
814 tmp
[0:0] = ['0'] * int(-leftdigits
)
817 if numdigits
> dotplace
:
818 tmp
.insert(dotplace
, '.')
819 elif numdigits
< dotplace
:
820 tmp
.extend(['0']*(dotplace
-numdigits
))
822 if not context
.capitals
:
828 tmp
.append(str(adjexp
))
830 while tmp
[0:1] == ['0']:
832 if len(tmp
) == 0 or tmp
[0] == '.' or tmp
[0].lower() == 'e':
839 def to_eng_string(self
, context
=None):
840 """Convert to engineering-type string.
842 Engineering notation has an exponent which is a multiple of 3, so there
843 are up to 3 digits left of the decimal place.
845 Same rules for when in exponential and when as a value as in __str__.
847 return self
.__str
__(eng
=1, context
=context
)
849 def __neg__(self
, context
=None):
850 """Returns a copy with the sign switched.
852 Rounds, if it has reason.
855 ans
= self
._check
_nans
(context
=context
)
860 # -Decimal('0') is Decimal('0'), not Decimal('-0')
868 context
= getcontext()
869 if context
._rounding
_decision
== ALWAYS_ROUND
:
870 return Decimal((sign
, self
._int
, self
._exp
))._fix
(context
)
871 return Decimal( (sign
, self
._int
, self
._exp
))
873 def __pos__(self
, context
=None):
874 """Returns a copy, unless it is a sNaN.
876 Rounds the number (if more then precision digits)
879 ans
= self
._check
_nans
(context
=context
)
889 context
= getcontext()
891 if context
._rounding
_decision
== ALWAYS_ROUND
:
892 ans
= self
._fix
(context
)
898 def __abs__(self
, round=1, context
=None):
899 """Returns the absolute value of self.
901 If the second argument is 0, do not round.
904 ans
= self
._check
_nans
(context
=context
)
910 context
= getcontext()
911 context
= context
._shallow
_copy
()
912 context
._set
_rounding
_decision
(NEVER_ROUND
)
915 ans
= self
.__neg
__(context
=context
)
917 ans
= self
.__pos
__(context
=context
)
921 def __add__(self
, other
, context
=None):
922 """Returns self + other.
924 -INF + INF (or the reverse) cause InvalidOperation errors.
926 other
= _convert_other(other
)
927 if other
is NotImplemented:
931 context
= getcontext()
933 if self
._is
_special
or other
._is
_special
:
934 ans
= self
._check
_nans
(other
, context
)
938 if self
._isinfinity
():
939 #If both INF, same sign => same as both, opposite => error.
940 if self
._sign
!= other
._sign
and other
._isinfinity
():
941 return context
._raise
_error
(InvalidOperation
, '-INF + INF')
943 if other
._isinfinity
():
944 return Decimal(other
) #Can't both be infinity here
946 shouldround
= context
._rounding
_decision
== ALWAYS_ROUND
948 exp
= min(self
._exp
, other
._exp
)
950 if context
.rounding
== ROUND_FLOOR
and self
._sign
!= other
._sign
:
951 #If the answer is 0, the sign should be negative, in this case.
954 if not self
and not other
:
955 sign
= min(self
._sign
, other
._sign
)
958 return Decimal( (sign
, (0,), exp
))
960 exp
= max(exp
, other
._exp
- context
.prec
-1)
961 ans
= other
._rescale
(exp
, watchexp
=0, context
=context
)
963 ans
= ans
._fix
(context
)
966 exp
= max(exp
, self
._exp
- context
.prec
-1)
967 ans
= self
._rescale
(exp
, watchexp
=0, context
=context
)
969 ans
= ans
._fix
(context
)
973 op2
= _WorkRep(other
)
974 op1
, op2
= _normalize(op1
, op2
, shouldround
, context
.prec
)
977 if op1
.sign
!= op2
.sign
:
979 if op1
.int == op2
.int:
980 if exp
< context
.Etiny():
981 exp
= context
.Etiny()
982 context
._raise
_error
(Clamped
)
983 return Decimal((negativezero
, (0,), exp
))
984 if op1
.int < op2
.int:
986 #OK, now abs(op1) > abs(op2)
989 op1
.sign
, op2
.sign
= op2
.sign
, op1
.sign
992 #So we know the sign, and op1 > 0.
995 op1
.sign
, op2
.sign
= (0, 0)
998 #Now, op1 > abs(op2) > 0
1001 result
.int = op1
.int + op2
.int
1003 result
.int = op1
.int - op2
.int
1005 result
.exp
= op1
.exp
1006 ans
= Decimal(result
)
1008 ans
= ans
._fix
(context
)
1013 def __sub__(self
, other
, context
=None):
1014 """Return self + (-other)"""
1015 other
= _convert_other(other
)
1016 if other
is NotImplemented:
1019 if self
._is
_special
or other
._is
_special
:
1020 ans
= self
._check
_nans
(other
, context
=context
)
1024 # -Decimal(0) = Decimal(0), which we don't want since
1025 # (-0 - 0 = -0 + (-0) = -0, but -0 + 0 = 0.)
1026 # so we change the sign directly to a copy
1027 tmp
= Decimal(other
)
1028 tmp
._sign
= 1-tmp
._sign
1030 return self
.__add
__(tmp
, context
=context
)
1032 def __rsub__(self
, other
, context
=None):
1033 """Return other + (-self)"""
1034 other
= _convert_other(other
)
1035 if other
is NotImplemented:
1039 tmp
._sign
= 1 - tmp
._sign
1040 return other
.__add
__(tmp
, context
=context
)
1042 def _increment(self
, round=1, context
=None):
1043 """Special case of add, adding 1eExponent
1045 Since it is common, (rounding, for example) this adds
1046 (sign)*one E self._exp to the number more efficiently than add.
1049 Decimal('5.624e10')._increment() == Decimal('5.625e10')
1051 if self
._is
_special
:
1052 ans
= self
._check
_nans
(context
=context
)
1056 return Decimal(self
) # Must be infinite, and incrementing makes no difference
1061 while L
[spot
] == 10:
1068 ans
= Decimal((self
._sign
, L
, self
._exp
))
1071 context
= getcontext()
1072 if round and context
._rounding
_decision
== ALWAYS_ROUND
:
1073 ans
= ans
._fix
(context
)
1076 def __mul__(self
, other
, context
=None):
1077 """Return self * other.
1079 (+-) INF * 0 (or its reverse) raise InvalidOperation.
1081 other
= _convert_other(other
)
1082 if other
is NotImplemented:
1086 context
= getcontext()
1088 resultsign
= self
._sign ^ other
._sign
1090 if self
._is
_special
or other
._is
_special
:
1091 ans
= self
._check
_nans
(other
, context
)
1095 if self
._isinfinity
():
1097 return context
._raise
_error
(InvalidOperation
, '(+-)INF * 0')
1098 return Infsign
[resultsign
]
1100 if other
._isinfinity
():
1102 return context
._raise
_error
(InvalidOperation
, '0 * (+-)INF')
1103 return Infsign
[resultsign
]
1105 resultexp
= self
._exp
+ other
._exp
1106 shouldround
= context
._rounding
_decision
== ALWAYS_ROUND
1108 # Special case for multiplying by zero
1109 if not self
or not other
:
1110 ans
= Decimal((resultsign
, (0,), resultexp
))
1112 #Fixing in case the exponent is out of bounds
1113 ans
= ans
._fix
(context
)
1116 # Special case for multiplying by power of 10
1117 if self
._int
== (1,):
1118 ans
= Decimal((resultsign
, other
._int
, resultexp
))
1120 ans
= ans
._fix
(context
)
1122 if other
._int
== (1,):
1123 ans
= Decimal((resultsign
, self
._int
, resultexp
))
1125 ans
= ans
._fix
(context
)
1128 op1
= _WorkRep(self
)
1129 op2
= _WorkRep(other
)
1131 ans
= Decimal( (resultsign
, map(int, str(op1
.int * op2
.int)), resultexp
))
1133 ans
= ans
._fix
(context
)
1138 def __div__(self
, other
, context
=None):
1139 """Return self / other."""
1140 return self
._divide
(other
, context
=context
)
1141 __truediv__
= __div__
1143 def _divide(self
, other
, divmod = 0, context
=None):
1144 """Return a / b, to context.prec precision.
1152 Actually, if divmod is 2 or 3 a tuple is returned, but errors for
1153 computing the other value are not raised.
1155 other
= _convert_other(other
)
1156 if other
is NotImplemented:
1157 if divmod in (0, 1):
1158 return NotImplemented
1159 return (NotImplemented, NotImplemented)
1162 context
= getcontext()
1164 sign
= self
._sign ^ other
._sign
1166 if self
._is
_special
or other
._is
_special
:
1167 ans
= self
._check
_nans
(other
, context
)
1173 if self
._isinfinity
() and other
._isinfinity
():
1175 return (context
._raise
_error
(InvalidOperation
,
1176 '(+-)INF // (+-)INF'),
1177 context
._raise
_error
(InvalidOperation
,
1178 '(+-)INF % (+-)INF'))
1179 return context
._raise
_error
(InvalidOperation
, '(+-)INF/(+-)INF')
1181 if self
._isinfinity
():
1183 return (Infsign
[sign
],
1184 context
._raise
_error
(InvalidOperation
, 'INF % x'))
1186 return (Infsign
[sign
], NaN
)
1188 return (Infsign
[sign
],
1189 context
._raise
_error
(InvalidOperation
, 'INF % x'))
1190 return Infsign
[sign
]
1192 if other
._isinfinity
():
1194 return (Decimal((sign
, (0,), 0)), Decimal(self
))
1195 context
._raise
_error
(Clamped
, 'Division by infinity')
1196 return Decimal((sign
, (0,), context
.Etiny()))
1198 # Special cases for zeroes
1199 if not self
and not other
:
1201 return context
._raise
_error
(DivisionUndefined
, '0 / 0', 1)
1202 return context
._raise
_error
(DivisionUndefined
, '0 / 0')
1206 otherside
= Decimal(self
)
1207 otherside
._exp
= min(self
._exp
, other
._exp
)
1208 return (Decimal((sign
, (0,), 0)), otherside
)
1209 exp
= self
._exp
- other
._exp
1210 if exp
< context
.Etiny():
1211 exp
= context
.Etiny()
1212 context
._raise
_error
(Clamped
, '0e-x / y')
1213 if exp
> context
.Emax
:
1215 context
._raise
_error
(Clamped
, '0e+x / y')
1216 return Decimal( (sign
, (0,), exp
) )
1220 return context
._raise
_error
(DivisionByZero
, 'divmod(x,0)',
1222 return context
._raise
_error
(DivisionByZero
, 'x / 0', sign
)
1224 #OK, so neither = 0, INF or NaN
1226 shouldround
= context
._rounding
_decision
== ALWAYS_ROUND
1228 #If we're dividing into ints, and self < other, stop.
1229 #self.__abs__(0) does not round.
1230 if divmod and (self
.__abs
__(0, context
) < other
.__abs
__(0, context
)):
1232 if divmod == 1 or divmod == 3:
1233 exp
= min(self
._exp
, other
._exp
)
1234 ans2
= self
._rescale
(exp
, context
=context
, watchexp
=0)
1236 ans2
= ans2
._fix(context
)
1237 return (Decimal( (sign
, (0,), 0) ),
1241 #Don't round the mod part, if we don't need it.
1242 return (Decimal( (sign
, (0,), 0) ), Decimal(self
))
1244 op1
= _WorkRep(self
)
1245 op2
= _WorkRep(other
)
1246 op1
, op2
, adjust
= _adjust_coefficients(op1
, op2
)
1247 res
= _WorkRep( (sign
, 0, (op1
.exp
- op2
.exp
)) )
1248 if divmod and res
.exp
> context
.prec
+ 1:
1249 return context
._raise
_error
(DivisionImpossible
)
1251 prec_limit
= 10 ** context
.prec
1253 while op2
.int <= op1
.int:
1256 if res
.exp
== 0 and divmod:
1257 if res
.int >= prec_limit
and shouldround
:
1258 return context
._raise
_error
(DivisionImpossible
)
1259 otherside
= Decimal(op1
)
1260 frozen
= context
._ignore
_all
_flags
()
1262 exp
= min(self
._exp
, other
._exp
)
1263 otherside
= otherside
._rescale
(exp
, context
=context
, watchexp
=0)
1264 context
._regard
_flags
(*frozen
)
1266 otherside
= otherside
._fix
(context
)
1267 return (Decimal(res
), otherside
)
1269 if op1
.int == 0 and adjust
>= 0 and not divmod:
1271 if res
.int >= prec_limit
and shouldround
:
1273 return context
._raise
_error
(DivisionImpossible
)
1275 # Really, the answer is a bit higher, so adding a one to
1276 # the end will make sure the rounding is right.
1289 if res
.exp
== 0 and divmod and op2
.int > op1
.int:
1290 #Solves an error in precision. Same as a previous block.
1292 if res
.int >= prec_limit
and shouldround
:
1293 return context
._raise
_error
(DivisionImpossible
)
1294 otherside
= Decimal(op1
)
1295 frozen
= context
._ignore
_all
_flags
()
1297 exp
= min(self
._exp
, other
._exp
)
1298 otherside
= otherside
._rescale
(exp
, context
=context
)
1300 context
._regard
_flags
(*frozen
)
1302 return (Decimal(res
), otherside
)
1306 ans
= ans
._fix
(context
)
1309 def __rdiv__(self
, other
, context
=None):
1310 """Swaps self/other and returns __div__."""
1311 other
= _convert_other(other
)
1312 if other
is NotImplemented:
1314 return other
.__div
__(self
, context
=context
)
1315 __rtruediv__
= __rdiv__
1317 def __divmod__(self
, other
, context
=None):
1319 (self // other, self % other)
1321 return self
._divide
(other
, 1, context
)
1323 def __rdivmod__(self
, other
, context
=None):
1324 """Swaps self/other and returns __divmod__."""
1325 other
= _convert_other(other
)
1326 if other
is NotImplemented:
1328 return other
.__divmod
__(self
, context
=context
)
1330 def __mod__(self
, other
, context
=None):
1334 other
= _convert_other(other
)
1335 if other
is NotImplemented:
1338 if self
._is
_special
or other
._is
_special
:
1339 ans
= self
._check
_nans
(other
, context
)
1343 if self
and not other
:
1344 return context
._raise
_error
(InvalidOperation
, 'x % 0')
1346 return self
._divide
(other
, 3, context
)[1]
1348 def __rmod__(self
, other
, context
=None):
1349 """Swaps self/other and returns __mod__."""
1350 other
= _convert_other(other
)
1351 if other
is NotImplemented:
1353 return other
.__mod
__(self
, context
=context
)
1355 def remainder_near(self
, other
, context
=None):
1357 Remainder nearest to 0- abs(remainder-near) <= other/2
1359 other
= _convert_other(other
)
1360 if other
is NotImplemented:
1363 if self
._is
_special
or other
._is
_special
:
1364 ans
= self
._check
_nans
(other
, context
)
1367 if self
and not other
:
1368 return context
._raise
_error
(InvalidOperation
, 'x % 0')
1371 context
= getcontext()
1372 # If DivisionImpossible causes an error, do not leave Rounded/Inexact
1373 # ignored in the calling function.
1374 context
= context
._shallow
_copy
()
1375 flags
= context
._ignore
_flags
(Rounded
, Inexact
)
1376 #keep DivisionImpossible flags
1377 (side
, r
) = self
.__divmod
__(other
, context
=context
)
1380 context
._regard
_flags
(*flags
)
1383 context
= context
._shallow
_copy
()
1384 rounding
= context
._set
_rounding
_decision
(NEVER_ROUND
)
1387 comparison
= other
.__div
__(Decimal(-2), context
=context
)
1389 comparison
= other
.__div
__(Decimal(2), context
=context
)
1391 context
._set
_rounding
_decision
(rounding
)
1392 context
._regard
_flags
(*flags
)
1394 s1
, s2
= r
._sign
, comparison
._sign
1395 r
._sign
, comparison
._sign
= 0, 0
1398 r
._sign
, comparison
._sign
= s1
, s2
1400 self
.__divmod
__(other
, context
=context
)
1401 return r
._fix
(context
)
1402 r
._sign
, comparison
._sign
= s1
, s2
1404 rounding
= context
._set
_rounding
_decision
(NEVER_ROUND
)
1406 (side
, r
) = self
.__divmod
__(other
, context
=context
)
1407 context
._set
_rounding
_decision
(rounding
)
1411 decrease
= not side
._iseven
()
1412 rounding
= context
._set
_rounding
_decision
(NEVER_ROUND
)
1413 side
= side
.__abs
__(context
=context
)
1414 context
._set
_rounding
_decision
(rounding
)
1416 s1
, s2
= r
._sign
, comparison
._sign
1417 r
._sign
, comparison
._sign
= 0, 0
1418 if r
> comparison
or decrease
and r
== comparison
:
1419 r
._sign
, comparison
._sign
= s1
, s2
1421 if len(side
.__add
__(Decimal(1), context
=context
)._int
) >= context
.prec
:
1423 return context
._raise
_error
(DivisionImpossible
)[1]
1425 if self
._sign
== other
._sign
:
1426 r
= r
.__sub
__(other
, context
=context
)
1428 r
= r
.__add
__(other
, context
=context
)
1430 r
._sign
, comparison
._sign
= s1
, s2
1432 return r
._fix
(context
)
1434 def __floordiv__(self
, other
, context
=None):
1436 return self
._divide
(other
, 2, context
)[0]
1438 def __rfloordiv__(self
, other
, context
=None):
1439 """Swaps self/other and returns __floordiv__."""
1440 other
= _convert_other(other
)
1441 if other
is NotImplemented:
1443 return other
.__floordiv
__(self
, context
=context
)
1445 def __float__(self
):
1446 """Float representation."""
1447 return float(str(self
))
1450 """Converts self to an int, truncating if necessary."""
1451 if self
._is
_special
:
1453 context
= getcontext()
1454 return context
._raise
_error
(InvalidContext
)
1455 elif self
._isinfinity
():
1456 raise OverflowError, "Cannot convert infinity to long"
1458 s
= ''.join(map(str, self
._int
)) + '0'*self
._exp
1460 s
= ''.join(map(str, self
._int
))[:self
._exp
]
1463 sign
= '-'*self
._sign
1464 return int(sign
+ s
)
1467 """Converts to a long.
1469 Equivalent to long(int(self))
1471 return long(self
.__int
__())
1473 def _fix(self
, context
):
1474 """Round if it is necessary to keep self within prec precision.
1476 Rounds and fixes the exponent. Does not raise on a sNaN.
1479 self - Decimal instance
1480 context - context used.
1482 if self
._is
_special
:
1485 context
= getcontext()
1487 ans
= self
._fixexponents
(context
)
1488 if len(ans
._int
) > prec
:
1489 ans
= ans
._round
(prec
, context
=context
)
1490 ans
= ans
._fixexponents
(context
)
1493 def _fixexponents(self
, context
):
1494 """Fix the exponents and return a copy with the exponent in bounds.
1495 Only call if known to not be a special value.
1497 folddown
= context
._clamp
1500 ans_adjusted
= ans
.adjusted()
1501 if ans_adjusted
< Emin
:
1502 Etiny
= context
.Etiny()
1503 if ans
._exp
< Etiny
:
1507 context
._raise
_error
(Clamped
)
1509 ans
= ans
._rescale
(Etiny
, context
=context
)
1510 #It isn't zero, and exp < Emin => subnormal
1511 context
._raise
_error
(Subnormal
)
1512 if context
.flags
[Inexact
]:
1513 context
._raise
_error
(Underflow
)
1516 #Only raise subnormal if non-zero.
1517 context
._raise
_error
(Subnormal
)
1519 Etop
= context
.Etop()
1520 if folddown
and ans
._exp
> Etop
:
1521 context
._raise
_error
(Clamped
)
1522 ans
= ans
._rescale
(Etop
, context
=context
)
1525 if ans_adjusted
> Emax
:
1529 context
._raise
_error
(Clamped
)
1531 context
._raise
_error
(Inexact
)
1532 context
._raise
_error
(Rounded
)
1533 return context
._raise
_error
(Overflow
, 'above Emax', ans
._sign
)
1536 def _round(self
, prec
=None, rounding
=None, context
=None):
1537 """Returns a rounded version of self.
1539 You can specify the precision or rounding method. Otherwise, the
1540 context determines it.
1543 if self
._is
_special
:
1544 ans
= self
._check
_nans
(context
=context
)
1548 if self
._isinfinity
():
1549 return Decimal(self
)
1552 context
= getcontext()
1554 if rounding
is None:
1555 rounding
= context
.rounding
1562 exp
= len(self
._int
) - prec
+ self
._exp
1565 exp
= len(self
._int
) + self
._exp
- prec
1566 ans
= Decimal((self
._sign
, dig
, exp
))
1567 context
._raise
_error
(Rounded
)
1571 temp
= Decimal(self
)
1572 temp
._int
= (0,)+temp
._int
1575 exp
= self
._exp
+ len(self
._int
) - prec
- 1
1576 temp
= Decimal( (self
._sign
, (0, 1), exp
))
1579 temp
= Decimal(self
)
1581 numdigits
= len(temp
._int
)
1582 if prec
== numdigits
:
1585 # See if we need to extend precision
1586 expdiff
= prec
- numdigits
1588 tmp
= list(temp
._int
)
1589 tmp
.extend([0] * expdiff
)
1590 ans
= Decimal( (temp
._sign
, tmp
, temp
._exp
- expdiff
))
1593 #OK, but maybe all the lost digits are 0.
1594 lostdigits
= self
._int
[expdiff
:]
1595 if lostdigits
== (0,) * len(lostdigits
):
1596 ans
= Decimal( (temp
._sign
, temp
._int
[:prec
], temp
._exp
- expdiff
))
1597 #Rounded, but not Inexact
1598 context
._raise
_error
(Rounded
)
1601 # Okay, let's round and lose data
1603 this_function
= getattr(temp
, self
._pick
_rounding
_function
[rounding
])
1604 #Now we've got the rounding function
1606 if prec
!= context
.prec
:
1607 context
= context
._shallow
_copy
()
1609 ans
= this_function(prec
, expdiff
, context
)
1610 context
._raise
_error
(Rounded
)
1611 context
._raise
_error
(Inexact
, 'Changed in rounding')
1615 _pick_rounding_function
= {}
1617 def _round_down(self
, prec
, expdiff
, context
):
1618 """Also known as round-towards-0, truncate."""
1619 return Decimal( (self
._sign
, self
._int
[:prec
], self
._exp
- expdiff
) )
1621 def _round_half_up(self
, prec
, expdiff
, context
, tmp
= None):
1622 """Rounds 5 up (away from 0)"""
1625 tmp
= Decimal( (self
._sign
,self
._int
[:prec
], self
._exp
- expdiff
))
1626 if self
._int
[prec
] >= 5:
1627 tmp
= tmp
._increment
(round=0, context
=context
)
1628 if len(tmp
._int
) > prec
:
1629 return Decimal( (tmp
._sign
, tmp
._int
[:-1], tmp
._exp
+ 1))
1632 def _round_half_even(self
, prec
, expdiff
, context
):
1633 """Round 5 to even, rest to nearest."""
1635 tmp
= Decimal( (self
._sign
, self
._int
[:prec
], self
._exp
- expdiff
))
1636 half
= (self
._int
[prec
] == 5)
1638 for digit
in self
._int
[prec
+1:]:
1643 if self
._int
[prec
-1] & 1 == 0:
1645 return self
._round
_half
_up
(prec
, expdiff
, context
, tmp
)
1647 def _round_half_down(self
, prec
, expdiff
, context
):
1650 tmp
= Decimal( (self
._sign
, self
._int
[:prec
], self
._exp
- expdiff
))
1651 half
= (self
._int
[prec
] == 5)
1653 for digit
in self
._int
[prec
+1:]:
1659 return self
._round
_half
_up
(prec
, expdiff
, context
, tmp
)
1661 def _round_up(self
, prec
, expdiff
, context
):
1662 """Rounds away from 0."""
1663 tmp
= Decimal( (self
._sign
, self
._int
[:prec
], self
._exp
- expdiff
) )
1664 for digit
in self
._int
[prec
:]:
1666 tmp
= tmp
._increment
(round=1, context
=context
)
1667 if len(tmp
._int
) > prec
:
1668 return Decimal( (tmp
._sign
, tmp
._int
[:-1], tmp
._exp
+ 1))
1673 def _round_ceiling(self
, prec
, expdiff
, context
):
1674 """Rounds up (not away from 0 if negative.)"""
1676 return self
._round
_down
(prec
, expdiff
, context
)
1678 return self
._round
_up
(prec
, expdiff
, context
)
1680 def _round_floor(self
, prec
, expdiff
, context
):
1681 """Rounds down (not towards 0 if negative)"""
1683 return self
._round
_down
(prec
, expdiff
, context
)
1685 return self
._round
_up
(prec
, expdiff
, context
)
1687 def __pow__(self
, n
, modulo
= None, context
=None):
1688 """Return self ** n (mod modulo)
1690 If modulo is None (default), don't take it mod modulo.
1692 n
= _convert_other(n
)
1693 if n
is NotImplemented:
1697 context
= getcontext()
1699 if self
._is
_special
or n
._is
_special
or n
.adjusted() > 8:
1700 #Because the spot << doesn't work with really big exponents
1701 if n
._isinfinity
() or n
.adjusted() > 8:
1702 return context
._raise
_error
(InvalidOperation
, 'x ** INF')
1704 ans
= self
._check
_nans
(n
, context
)
1708 if not n
._isinteger
():
1709 return context
._raise
_error
(InvalidOperation
, 'x ** (non-integer)')
1711 if not self
and not n
:
1712 return context
._raise
_error
(InvalidOperation
, '0 ** 0')
1717 if self
== Decimal(1):
1720 sign
= self
._sign
and not n
._iseven
()
1723 if self
._isinfinity
():
1725 return context
._raise
_error
(InvalidOperation
, 'INF % x')
1727 return Infsign
[sign
]
1728 return Decimal( (sign
, (0,), 0) )
1730 #with ludicrously large exponent, just raise an overflow and return inf.
1731 if not modulo
and n
> 0 and (self
._exp
+ len(self
._int
) - 1) * n
> context
.Emax \
1734 tmp
= Decimal('inf')
1736 context
._raise
_error
(Rounded
)
1737 context
._raise
_error
(Inexact
)
1738 context
._raise
_error
(Overflow
, 'Big power', sign
)
1741 elength
= len(str(abs(n
)))
1742 firstprec
= context
.prec
1744 if not modulo
and firstprec
+ elength
+ 1 > DefaultContext
.Emax
:
1745 return context
._raise
_error
(Overflow
, 'Too much precision.', sign
)
1749 context
= context
._shallow
_copy
()
1750 context
.prec
= firstprec
+ elength
+ 1
1752 #n is a long now, not Decimal instance
1754 mul
= Decimal(1).__div
__(mul
, context
=context
)
1761 #Spot is the highest power of 2 less than n
1763 val
= val
.__mul
__(val
, context
=context
)
1764 if val
._isinfinity
():
1768 val
= val
.__mul
__(mul
, context
=context
)
1769 if modulo
is not None:
1770 val
= val
.__mod
__(modulo
, context
=context
)
1772 context
.prec
= firstprec
1774 if context
._rounding
_decision
== ALWAYS_ROUND
:
1775 return val
._fix
(context
)
1778 def __rpow__(self
, other
, context
=None):
1779 """Swaps self/other and returns __pow__."""
1780 other
= _convert_other(other
)
1781 if other
is NotImplemented:
1783 return other
.__pow
__(self
, context
=context
)
1785 def normalize(self
, context
=None):
1786 """Normalize- strip trailing 0s, change anything equal to 0 to 0e0"""
1788 if self
._is
_special
:
1789 ans
= self
._check
_nans
(context
=context
)
1793 dup
= self
._fix
(context
)
1794 if dup
._isinfinity
():
1798 return Decimal( (dup
._sign
, (0,), 0) )
1801 while dup
._int
[end
-1] == 0:
1804 return Decimal( (dup
._sign
, dup
._int
[:end
], exp
) )
1807 def quantize(self
, exp
, rounding
=None, context
=None, watchexp
=1):
1808 """Quantize self so its exponent is the same as that of exp.
1810 Similar to self._rescale(exp._exp) but with error checking.
1812 if self
._is
_special
or exp
._is
_special
:
1813 ans
= self
._check
_nans
(exp
, context
)
1817 if exp
._isinfinity
() or self
._isinfinity
():
1818 if exp
._isinfinity
() and self
._isinfinity
():
1819 return self
#if both are inf, it is OK
1821 context
= getcontext()
1822 return context
._raise
_error
(InvalidOperation
,
1823 'quantize with one INF')
1824 return self
._rescale
(exp
._exp
, rounding
, context
, watchexp
)
1826 def same_quantum(self
, other
):
1827 """Test whether self and other have the same exponent.
1829 same as self._exp == other._exp, except NaN == sNaN
1831 if self
._is
_special
or other
._is
_special
:
1832 if self
._isnan
() or other
._isnan
():
1833 return self
._isnan
() and other
._isnan
() and True
1834 if self
._isinfinity
() or other
._isinfinity
():
1835 return self
._isinfinity
() and other
._isinfinity
() and True
1836 return self
._exp
== other
._exp
1838 def _rescale(self
, exp
, rounding
=None, context
=None, watchexp
=1):
1839 """Rescales so that the exponent is exp.
1841 exp = exp to scale to (an integer)
1842 rounding = rounding version
1843 watchexp: if set (default) an error is returned if exp is greater
1844 than Emax or less than Etiny.
1847 context
= getcontext()
1849 if self
._is
_special
:
1850 if self
._isinfinity
():
1851 return context
._raise
_error
(InvalidOperation
, 'rescale with an INF')
1853 ans
= self
._check
_nans
(context
=context
)
1857 if watchexp
and (context
.Emax
< exp
or context
.Etiny() > exp
):
1858 return context
._raise
_error
(InvalidOperation
, 'rescale(a, INF)')
1866 diff
= self
._exp
- exp
1867 digits
= len(self
._int
) + diff
1869 if watchexp
and digits
> context
.prec
:
1870 return context
._raise
_error
(InvalidOperation
, 'Rescale > prec')
1873 tmp
._int
= (0,) + tmp
._int
1877 tmp
._exp
= -digits
+ tmp
._exp
1880 tmp
= tmp
._round
(digits
, rounding
, context
=context
)
1882 if tmp
._int
[0] == 0 and len(tmp
._int
) > 1:
1883 tmp
._int
= tmp
._int
[1:]
1886 tmp_adjusted
= tmp
.adjusted()
1887 if tmp
and tmp_adjusted
< context
.Emin
:
1888 context
._raise
_error
(Subnormal
)
1889 elif tmp
and tmp_adjusted
> context
.Emax
:
1890 return context
._raise
_error
(InvalidOperation
, 'rescale(a, INF)')
1893 def to_integral(self
, rounding
=None, context
=None):
1894 """Rounds to the nearest integer, without raising inexact, rounded."""
1895 if self
._is
_special
:
1896 ans
= self
._check
_nans
(context
=context
)
1902 context
= getcontext()
1903 flags
= context
._ignore
_flags
(Rounded
, Inexact
)
1904 ans
= self
._rescale
(0, rounding
, context
=context
)
1905 context
._regard
_flags
(flags
)
1908 def sqrt(self
, context
=None):
1909 """Return the square root of self.
1911 Uses a converging algorithm (Xn+1 = 0.5*(Xn + self / Xn))
1912 Should quadratically approach the right answer.
1914 if self
._is
_special
:
1915 ans
= self
._check
_nans
(context
=context
)
1919 if self
._isinfinity
() and self
._sign
== 0:
1920 return Decimal(self
)
1923 #exponent = self._exp / 2, using round_down.
1925 # exp = (self._exp+1) // 2
1927 exp
= (self
._exp
) // 2
1930 return Decimal( (1, (0,), exp
))
1932 return Decimal( (0, (0,), exp
))
1935 context
= getcontext()
1938 return context
._raise
_error
(InvalidOperation
, 'sqrt(-x), x > 0')
1942 expadd
= tmp
._exp
// 2
1949 context
= context
._shallow
_copy
()
1950 flags
= context
._ignore
_all
_flags
()
1951 firstprec
= context
.prec
1953 if tmp
.adjusted() & 1 == 0:
1954 ans
= Decimal( (0, (8,1,9), tmp
.adjusted() - 2) )
1955 ans
= ans
.__add
__(tmp
.__mul
__(Decimal((0, (2,5,9), -2)),
1956 context
=context
), context
=context
)
1957 ans
._exp
-= 1 + tmp
.adjusted() // 2
1959 ans
= Decimal( (0, (2,5,9), tmp
._exp
+ len(tmp
._int
)- 3) )
1960 ans
= ans
.__add
__(tmp
.__mul
__(Decimal((0, (8,1,9), -3)),
1961 context
=context
), context
=context
)
1962 ans
._exp
-= 1 + tmp
.adjusted() // 2
1964 #ans is now a linear approximation.
1966 Emax
, Emin
= context
.Emax
, context
.Emin
1967 context
.Emax
, context
.Emin
= DefaultContext
.Emax
, DefaultContext
.Emin
1969 half
= Decimal('0.5')
1971 maxp
= firstprec
+ 2
1972 rounding
= context
._set
_rounding
(ROUND_HALF_EVEN
)
1974 context
.prec
= min(2*context
.prec
- 2, maxp
)
1975 ans
= half
.__mul
__(ans
.__add
__(tmp
.__div
__(ans
, context
=context
),
1976 context
=context
), context
=context
)
1977 if context
.prec
== maxp
:
1980 #round to the answer's precision-- the only error can be 1 ulp.
1981 context
.prec
= firstprec
1982 prevexp
= ans
.adjusted()
1983 ans
= ans
._round
(context
=context
)
1985 #Now, check if the other last digits are better.
1986 context
.prec
= firstprec
+ 1
1987 # In case we rounded up another digit and we should actually go lower.
1988 if prevexp
!= ans
.adjusted():
1993 lower
= ans
.__sub
__(Decimal((0, (5,), ans
._exp
-1)), context
=context
)
1994 context
._set
_rounding
(ROUND_UP
)
1995 if lower
.__mul
__(lower
, context
=context
) > (tmp
):
1996 ans
= ans
.__sub
__(Decimal((0, (1,), ans
._exp
)), context
=context
)
1999 upper
= ans
.__add
__(Decimal((0, (5,), ans
._exp
-1)),context
=context
)
2000 context
._set
_rounding
(ROUND_DOWN
)
2001 if upper
.__mul
__(upper
, context
=context
) < tmp
:
2002 ans
= ans
.__add
__(Decimal((0, (1,), ans
._exp
)),context
=context
)
2006 context
.prec
= firstprec
2007 context
.rounding
= rounding
2008 ans
= ans
._fix
(context
)
2010 rounding
= context
._set
_rounding
_decision
(NEVER_ROUND
)
2011 if not ans
.__mul
__(ans
, context
=context
) == self
:
2012 # Only rounded/inexact if here.
2013 context
._regard
_flags
(flags
)
2014 context
._raise
_error
(Rounded
)
2015 context
._raise
_error
(Inexact
)
2017 #Exact answer, so let's set the exponent right.
2019 # exp = (self._exp +1)// 2
2021 exp
= self
._exp
// 2
2022 context
.prec
+= ans
._exp
- exp
2023 ans
= ans
._rescale
(exp
, context
=context
)
2024 context
.prec
= firstprec
2025 context
._regard
_flags
(flags
)
2026 context
.Emax
, context
.Emin
= Emax
, Emin
2028 return ans
._fix
(context
)
2030 def max(self
, other
, context
=None):
2031 """Returns the larger value.
2033 like max(self, other) except if one is not a number, returns
2034 NaN (and signals if one is sNaN). Also rounds.
2036 other
= _convert_other(other
)
2037 if other
is NotImplemented:
2040 if self
._is
_special
or other
._is
_special
:
2041 # if one operand is a quiet NaN and the other is number, then the
2042 # number is always returned
2046 if on
== 1 and sn
!= 2:
2048 if sn
== 1 and on
!= 2:
2050 return self
._check
_nans
(other
, context
)
2053 c
= self
.__cmp
__(other
)
2055 # if both operands are finite and equal in numerical value
2056 # then an ordering is applied:
2058 # if the signs differ then max returns the operand with the
2059 # positive sign and min returns the operand with the negative sign
2061 # if the signs are the same then the exponent is used to select
2063 if self
._sign
!= other
._sign
:
2066 elif self
._exp
< other
._exp
and not self
._sign
:
2068 elif self
._exp
> other
._exp
and self
._sign
:
2074 context
= getcontext()
2075 if context
._rounding
_decision
== ALWAYS_ROUND
:
2076 return ans
._fix
(context
)
2079 def min(self
, other
, context
=None):
2080 """Returns the smaller value.
2082 like min(self, other) except if one is not a number, returns
2083 NaN (and signals if one is sNaN). Also rounds.
2085 other
= _convert_other(other
)
2086 if other
is NotImplemented:
2089 if self
._is
_special
or other
._is
_special
:
2090 # if one operand is a quiet NaN and the other is number, then the
2091 # number is always returned
2095 if on
== 1 and sn
!= 2:
2097 if sn
== 1 and on
!= 2:
2099 return self
._check
_nans
(other
, context
)
2102 c
= self
.__cmp
__(other
)
2104 # if both operands are finite and equal in numerical value
2105 # then an ordering is applied:
2107 # if the signs differ then max returns the operand with the
2108 # positive sign and min returns the operand with the negative sign
2110 # if the signs are the same then the exponent is used to select
2112 if self
._sign
!= other
._sign
:
2115 elif self
._exp
> other
._exp
and not self
._sign
:
2117 elif self
._exp
< other
._exp
and self
._sign
:
2123 context
= getcontext()
2124 if context
._rounding
_decision
== ALWAYS_ROUND
:
2125 return ans
._fix
(context
)
2128 def _isinteger(self
):
2129 """Returns whether self is an integer"""
2132 rest
= self
._int
[self
._exp
:]
2133 return rest
== (0,)*len(rest
)
2136 """Returns 1 if self is even. Assumes self is an integer."""
2139 return self
._int
[-1+self
._exp
] & 1 == 0
2142 """Return the adjusted exponent of self"""
2144 return self
._exp
+ len(self
._int
) - 1
2145 #If NaN or Infinity, self._exp is string
2149 # support for pickling, copy, and deepcopy
2150 def __reduce__(self
):
2151 return (self
.__class
__, (str(self
),))
2154 if type(self
) == Decimal
:
2155 return self
# I'm immutable; therefore I am my own clone
2156 return self
.__class
__(str(self
))
2158 def __deepcopy__(self
, memo
):
2159 if type(self
) == Decimal
:
2160 return self
# My components are also immutable
2161 return self
.__class
__(str(self
))
2163 ##### Context class ###########################################
2166 # get rounding method function:
2167 rounding_functions
= [name
for name
in Decimal
.__dict
__.keys() if name
.startswith('_round_')]
2168 for name
in rounding_functions
:
2169 #name is like _round_half_even, goes to the global ROUND_HALF_EVEN value.
2170 globalname
= name
[1:].upper()
2171 val
= globals()[globalname
]
2172 Decimal
._pick
_rounding
_function
[val
] = name
2174 del name
, val
, globalname
, rounding_functions
2176 class Context(object):
2177 """Contains the context for a Decimal instance.
2180 prec - precision (for use in rounding, division, square roots..)
2181 rounding - rounding type. (how you round)
2182 _rounding_decision - ALWAYS_ROUND, NEVER_ROUND -- do you round?
2183 traps - If traps[exception] = 1, then the exception is
2184 raised when it is caused. Otherwise, a value is
2186 flags - When an exception is caused, flags[exception] is incremented.
2187 (Whether or not the trap_enabler is set)
2188 Should be reset by user of Decimal instance.
2189 Emin - Minimum exponent
2190 Emax - Maximum exponent
2191 capitals - If 1, 1*10^1 is printed as 1E+1.
2192 If 0, printed as 1e1
2193 _clamp - If 1, change exponents if too high (Default 0)
2196 def __init__(self
, prec
=None, rounding
=None,
2197 traps
=None, flags
=None,
2198 _rounding_decision
=None,
2199 Emin
=None, Emax
=None,
2200 capitals
=None, _clamp
=0,
2201 _ignored_flags
=None):
2204 if _ignored_flags
is None:
2206 if not isinstance(flags
, dict):
2207 flags
= dict([(s
,s
in flags
) for s
in _signals
])
2209 if traps
is not None and not isinstance(traps
, dict):
2210 traps
= dict([(s
,s
in traps
) for s
in _signals
])
2212 for name
, val
in locals().items():
2214 setattr(self
, name
, _copy
.copy(getattr(DefaultContext
, name
)))
2216 setattr(self
, name
, val
)
2220 """Show the current context."""
2222 s
.append('Context(prec=%(prec)d, rounding=%(rounding)s, Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d' % vars(self
))
2223 s
.append('flags=[' + ', '.join([f
.__name
__ for f
, v
in self
.flags
.items() if v
]) + ']')
2224 s
.append('traps=[' + ', '.join([t
.__name
__ for t
, v
in self
.traps
.items() if v
]) + ']')
2225 return ', '.join(s
) + ')'
2227 def clear_flags(self
):
2228 """Reset all flags to zero"""
2229 for flag
in self
.flags
:
2230 self
.flags
[flag
] = 0
2232 def _shallow_copy(self
):
2233 """Returns a shallow copy from self."""
2234 nc
= Context(self
.prec
, self
.rounding
, self
.traps
, self
.flags
,
2235 self
._rounding
_decision
, self
.Emin
, self
.Emax
,
2236 self
.capitals
, self
._clamp
, self
._ignored
_flags
)
2240 """Returns a deep copy from self."""
2241 nc
= Context(self
.prec
, self
.rounding
, self
.traps
.copy(), self
.flags
.copy(),
2242 self
._rounding
_decision
, self
.Emin
, self
.Emax
,
2243 self
.capitals
, self
._clamp
, self
._ignored
_flags
)
2247 def _raise_error(self
, condition
, explanation
= None, *args
):
2250 If the flag is in _ignored_flags, returns the default response.
2251 Otherwise, it increments the flag, then, if the corresponding
2252 trap_enabler is set, it reaises the exception. Otherwise, it returns
2253 the default value after incrementing the flag.
2255 error
= _condition_map
.get(condition
, condition
)
2256 if error
in self
._ignored
_flags
:
2257 #Don't touch the flag
2258 return error().handle(self
, *args
)
2260 self
.flags
[error
] += 1
2261 if not self
.traps
[error
]:
2262 #The errors define how to handle themselves.
2263 return condition().handle(self
, *args
)
2265 # Errors should only be risked on copies of the context
2266 #self._ignored_flags = []
2267 raise error
, explanation
2269 def _ignore_all_flags(self
):
2270 """Ignore all flags, if they are raised"""
2271 return self
._ignore
_flags
(*_signals
)
2273 def _ignore_flags(self
, *flags
):
2274 """Ignore the flags, if they are raised"""
2275 # Do not mutate-- This way, copies of a context leave the original
2277 self
._ignored
_flags
= (self
._ignored
_flags
+ list(flags
))
2280 def _regard_flags(self
, *flags
):
2281 """Stop ignoring the flags, if they are raised"""
2282 if flags
and isinstance(flags
[0], (tuple,list)):
2285 self
._ignored
_flags
.remove(flag
)
2288 """A Context cannot be hashed."""
2289 # We inherit object.__hash__, so we must deny this explicitly
2290 raise TypeError, "Cannot hash a Context."
2293 """Returns Etiny (= Emin - prec + 1)"""
2294 return int(self
.Emin
- self
.prec
+ 1)
2297 """Returns maximum exponent (= Emax - prec + 1)"""
2298 return int(self
.Emax
- self
.prec
+ 1)
2300 def _set_rounding_decision(self
, type):
2301 """Sets the rounding decision.
2303 Sets the rounding decision, and returns the current (previous)
2304 rounding decision. Often used like:
2306 context = context._shallow_copy()
2307 # That so you don't change the calling context
2308 # if an error occurs in the middle (say DivisionImpossible is raised).
2310 rounding = context._set_rounding_decision(NEVER_ROUND)
2311 instance = instance / Decimal(2)
2312 context._set_rounding_decision(rounding)
2314 This will make it not round for that operation.
2317 rounding
= self
._rounding
_decision
2318 self
._rounding
_decision
= type
2321 def _set_rounding(self
, type):
2322 """Sets the rounding type.
2324 Sets the rounding type, and returns the current (previous)
2325 rounding type. Often used like:
2327 context = context.copy()
2328 # so you don't change the calling context
2329 # if an error occurs in the middle.
2330 rounding = context._set_rounding(ROUND_UP)
2331 val = self.__sub__(other, context=context)
2332 context._set_rounding(rounding)
2334 This will make it round up for that operation.
2336 rounding
= self
.rounding
2340 def create_decimal(self
, num
='0'):
2341 """Creates a new Decimal instance but using self as context."""
2342 d
= Decimal(num
, context
=self
)
2347 """Returns the absolute value of the operand.
2349 If the operand is negative, the result is the same as using the minus
2350 operation on the operand. Otherwise, the result is the same as using
2351 the plus operation on the operand.
2353 >>> ExtendedContext.abs(Decimal('2.1'))
2355 >>> ExtendedContext.abs(Decimal('-100'))
2357 >>> ExtendedContext.abs(Decimal('101.5'))
2359 >>> ExtendedContext.abs(Decimal('-101.5'))
2362 return a
.__abs
__(context
=self
)
2364 def add(self
, a
, b
):
2365 """Return the sum of the two operands.
2367 >>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))
2369 >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
2372 return a
.__add
__(b
, context
=self
)
2374 def _apply(self
, a
):
2375 return str(a
._fix
(self
))
2377 def compare(self
, a
, b
):
2378 """Compares values numerically.
2380 If the signs of the operands differ, a value representing each operand
2381 ('-1' if the operand is less than zero, '0' if the operand is zero or
2382 negative zero, or '1' if the operand is greater than zero) is used in
2383 place of that operand for the comparison instead of the actual
2386 The comparison is then effected by subtracting the second operand from
2387 the first and then returning a value according to the result of the
2388 subtraction: '-1' if the result is less than zero, '0' if the result is
2389 zero or negative zero, or '1' if the result is greater than zero.
2391 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3'))
2393 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1'))
2395 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10'))
2397 >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1'))
2399 >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3'))
2401 >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1'))
2404 return a
.compare(b
, context
=self
)
2406 def divide(self
, a
, b
):
2407 """Decimal division in a specified context.
2409 >>> ExtendedContext.divide(Decimal('1'), Decimal('3'))
2410 Decimal("0.333333333")
2411 >>> ExtendedContext.divide(Decimal('2'), Decimal('3'))
2412 Decimal("0.666666667")
2413 >>> ExtendedContext.divide(Decimal('5'), Decimal('2'))
2415 >>> ExtendedContext.divide(Decimal('1'), Decimal('10'))
2417 >>> ExtendedContext.divide(Decimal('12'), Decimal('12'))
2419 >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2'))
2421 >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0'))
2423 >>> ExtendedContext.divide(Decimal('1000'), Decimal('100'))
2425 >>> ExtendedContext.divide(Decimal('1000'), Decimal('1'))
2427 >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
2430 return a
.__div
__(b
, context
=self
)
2432 def divide_int(self
, a
, b
):
2433 """Divides two numbers and returns the integer part of the result.
2435 >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3'))
2437 >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3'))
2439 >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3'))
2442 return a
.__floordiv
__(b
, context
=self
)
2444 def divmod(self
, a
, b
):
2445 return a
.__divmod
__(b
, context
=self
)
2448 """max compares two values numerically and returns the maximum.
2450 If either operand is a NaN then the general rules apply.
2451 Otherwise, the operands are compared as as though by the compare
2452 operation. If they are numerically equal then the left-hand operand
2453 is chosen as the result. Otherwise the maximum (closer to positive
2454 infinity) of the two operands is chosen as the result.
2456 >>> ExtendedContext.max(Decimal('3'), Decimal('2'))
2458 >>> ExtendedContext.max(Decimal('-10'), Decimal('3'))
2460 >>> ExtendedContext.max(Decimal('1.0'), Decimal('1'))
2462 >>> ExtendedContext.max(Decimal('7'), Decimal('NaN'))
2465 return a
.max(b
, context
=self
)
2468 """min compares two values numerically and returns the minimum.
2470 If either operand is a NaN then the general rules apply.
2471 Otherwise, the operands are compared as as though by the compare
2472 operation. If they are numerically equal then the left-hand operand
2473 is chosen as the result. Otherwise the minimum (closer to negative
2474 infinity) of the two operands is chosen as the result.
2476 >>> ExtendedContext.min(Decimal('3'), Decimal('2'))
2478 >>> ExtendedContext.min(Decimal('-10'), Decimal('3'))
2480 >>> ExtendedContext.min(Decimal('1.0'), Decimal('1'))
2482 >>> ExtendedContext.min(Decimal('7'), Decimal('NaN'))
2485 return a
.min(b
, context
=self
)
2488 """Minus corresponds to unary prefix minus in Python.
2490 The operation is evaluated using the same rules as subtract; the
2491 operation minus(a) is calculated as subtract('0', a) where the '0'
2492 has the same exponent as the operand.
2494 >>> ExtendedContext.minus(Decimal('1.3'))
2496 >>> ExtendedContext.minus(Decimal('-1.3'))
2499 return a
.__neg
__(context
=self
)
2501 def multiply(self
, a
, b
):
2502 """multiply multiplies two operands.
2504 If either operand is a special value then the general rules apply.
2505 Otherwise, the operands are multiplied together ('long multiplication'),
2506 resulting in a number which may be as long as the sum of the lengths
2507 of the two operands.
2509 >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3'))
2511 >>> ExtendedContext.multiply(Decimal('7'), Decimal('3'))
2513 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8'))
2515 >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0'))
2517 >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321'))
2518 Decimal("4.28135971E+11")
2520 return a
.__mul
__(b
, context
=self
)
2522 def normalize(self
, a
):
2523 """normalize reduces an operand to its simplest form.
2525 Essentially a plus operation with all trailing zeros removed from the
2528 >>> ExtendedContext.normalize(Decimal('2.1'))
2530 >>> ExtendedContext.normalize(Decimal('-2.0'))
2532 >>> ExtendedContext.normalize(Decimal('1.200'))
2534 >>> ExtendedContext.normalize(Decimal('-120'))
2536 >>> ExtendedContext.normalize(Decimal('120.00'))
2538 >>> ExtendedContext.normalize(Decimal('0.00'))
2541 return a
.normalize(context
=self
)
2544 """Plus corresponds to unary prefix plus in Python.
2546 The operation is evaluated using the same rules as add; the
2547 operation plus(a) is calculated as add('0', a) where the '0'
2548 has the same exponent as the operand.
2550 >>> ExtendedContext.plus(Decimal('1.3'))
2552 >>> ExtendedContext.plus(Decimal('-1.3'))
2555 return a
.__pos
__(context
=self
)
2557 def power(self
, a
, b
, modulo
=None):
2558 """Raises a to the power of b, to modulo if given.
2560 The right-hand operand must be a whole number whose integer part (after
2561 any exponent has been applied) has no more than 9 digits and whose
2562 fractional part (if any) is all zeros before any rounding. The operand
2563 may be positive, negative, or zero; if negative, the absolute value of
2564 the power is used, and the left-hand operand is inverted (divided into
2567 If the increased precision needed for the intermediate calculations
2568 exceeds the capabilities of the implementation then an Invalid operation
2569 condition is raised.
2571 If, when raising to a negative power, an underflow occurs during the
2572 division into 1, the operation is not halted at that point but
2575 >>> ExtendedContext.power(Decimal('2'), Decimal('3'))
2577 >>> ExtendedContext.power(Decimal('2'), Decimal('-3'))
2579 >>> ExtendedContext.power(Decimal('1.7'), Decimal('8'))
2580 Decimal("69.7575744")
2581 >>> ExtendedContext.power(Decimal('Infinity'), Decimal('-2'))
2583 >>> ExtendedContext.power(Decimal('Infinity'), Decimal('-1'))
2585 >>> ExtendedContext.power(Decimal('Infinity'), Decimal('0'))
2587 >>> ExtendedContext.power(Decimal('Infinity'), Decimal('1'))
2589 >>> ExtendedContext.power(Decimal('Infinity'), Decimal('2'))
2591 >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('-2'))
2593 >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('-1'))
2595 >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('0'))
2597 >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('1'))
2598 Decimal("-Infinity")
2599 >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('2'))
2601 >>> ExtendedContext.power(Decimal('0'), Decimal('0'))
2604 return a
.__pow
__(b
, modulo
, context
=self
)
2606 def quantize(self
, a
, b
):
2607 """Returns a value equal to 'a' (rounded) and having the exponent of 'b'.
2609 The coefficient of the result is derived from that of the left-hand
2610 operand. It may be rounded using the current rounding setting (if the
2611 exponent is being increased), multiplied by a positive power of ten (if
2612 the exponent is being decreased), or is unchanged (if the exponent is
2613 already equal to that of the right-hand operand).
2615 Unlike other operations, if the length of the coefficient after the
2616 quantize operation would be greater than precision then an Invalid
2617 operation condition is raised. This guarantees that, unless there is an
2618 error condition, the exponent of the result of a quantize is always
2619 equal to that of the right-hand operand.
2621 Also unlike other operations, quantize will never raise Underflow, even
2622 if the result is subnormal and inexact.
2624 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
2626 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
2628 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
2630 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
2632 >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
2634 >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
2635 Decimal("-Infinity")
2636 >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
2638 >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
2640 >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
2642 >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
2644 >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
2646 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
2648 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
2650 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
2652 >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
2655 return a
.quantize(b
, context
=self
)
2657 def remainder(self
, a
, b
):
2658 """Returns the remainder from integer division.
2660 The result is the residue of the dividend after the operation of
2661 calculating integer division as described for divide-integer, rounded to
2662 precision digits if necessary. The sign of the result, if non-zero, is
2663 the same as that of the original dividend.
2665 This operation will fail under the same conditions as integer division
2666 (that is, if integer division on the same two operands would fail, the
2667 remainder cannot be calculated).
2669 >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3'))
2671 >>> ExtendedContext.remainder(Decimal('10'), Decimal('3'))
2673 >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3'))
2675 >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1'))
2677 >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3'))
2679 >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3'))
2682 return a
.__mod
__(b
, context
=self
)
2684 def remainder_near(self
, a
, b
):
2685 """Returns to be "a - b * n", where n is the integer nearest the exact
2686 value of "x / b" (if two integers are equally near then the even one
2687 is chosen). If the result is equal to 0 then its sign will be the
2690 This operation will fail under the same conditions as integer division
2691 (that is, if integer division on the same two operands would fail, the
2692 remainder cannot be calculated).
2694 >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3'))
2696 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6'))
2698 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3'))
2700 >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3'))
2702 >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1'))
2704 >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3'))
2706 >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
2709 return a
.remainder_near(b
, context
=self
)
2711 def same_quantum(self
, a
, b
):
2712 """Returns True if the two operands have the same exponent.
2714 The result is never affected by either the sign or the coefficient of
2717 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001'))
2719 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01'))
2721 >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1'))
2723 >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf'))
2726 return a
.same_quantum(b
)
2729 """Returns the square root of a non-negative number to context precision.
2731 If the result must be inexact, it is rounded using the round-half-even
2734 >>> ExtendedContext.sqrt(Decimal('0'))
2736 >>> ExtendedContext.sqrt(Decimal('-0'))
2738 >>> ExtendedContext.sqrt(Decimal('0.39'))
2739 Decimal("0.624499800")
2740 >>> ExtendedContext.sqrt(Decimal('100'))
2742 >>> ExtendedContext.sqrt(Decimal('1'))
2744 >>> ExtendedContext.sqrt(Decimal('1.0'))
2746 >>> ExtendedContext.sqrt(Decimal('1.00'))
2748 >>> ExtendedContext.sqrt(Decimal('7'))
2749 Decimal("2.64575131")
2750 >>> ExtendedContext.sqrt(Decimal('10'))
2751 Decimal("3.16227766")
2752 >>> ExtendedContext.prec
2755 return a
.sqrt(context
=self
)
2757 def subtract(self
, a
, b
):
2758 """Return the difference between the two operands.
2760 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07'))
2762 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30'))
2764 >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07'))
2767 return a
.__sub
__(b
, context
=self
)
2769 def to_eng_string(self
, a
):
2770 """Converts a number to a string, using scientific notation.
2772 The operation is not affected by the context.
2774 return a
.to_eng_string(context
=self
)
2776 def to_sci_string(self
, a
):
2777 """Converts a number to a string, using scientific notation.
2779 The operation is not affected by the context.
2781 return a
.__str
__(context
=self
)
2783 def to_integral(self
, a
):
2784 """Rounds to an integer.
2786 When the operand has a negative exponent, the result is the same
2787 as using the quantize() operation using the given operand as the
2788 left-hand-operand, 1E+0 as the right-hand-operand, and the precision
2789 of the operand as the precision setting, except that no flags will
2790 be set. The rounding mode is taken from the context.
2792 >>> ExtendedContext.to_integral(Decimal('2.1'))
2794 >>> ExtendedContext.to_integral(Decimal('100'))
2796 >>> ExtendedContext.to_integral(Decimal('100.0'))
2798 >>> ExtendedContext.to_integral(Decimal('101.5'))
2800 >>> ExtendedContext.to_integral(Decimal('-101.5'))
2802 >>> ExtendedContext.to_integral(Decimal('10E+5'))
2804 >>> ExtendedContext.to_integral(Decimal('7.89E+77'))
2806 >>> ExtendedContext.to_integral(Decimal('-Inf'))
2807 Decimal("-Infinity")
2809 return a
.to_integral(context
=self
)
2811 class _WorkRep(object):
2812 __slots__
= ('sign','int','exp')
2815 # exp: None, int, or string
2817 def __init__(self
, value
=None):
2822 elif isinstance(value
, Decimal
):
2823 self
.sign
= value
._sign
2825 for digit
in value
._int
:
2826 cum
= cum
* 10 + digit
2828 self
.exp
= value
._exp
2830 # assert isinstance(value, tuple)
2831 self
.sign
= value
[0]
2836 return "(%r, %r, %r)" % (self
.sign
, self
.int, self
.exp
)
2842 def _normalize(op1
, op2
, shouldround
= 0, prec
= 0):
2843 """Normalizes op1, op2 to have the same exp and length of coefficient.
2845 Done during addition.
2847 # Yes, the exponent is a long, but the difference between exponents
2848 # must be an int-- otherwise you'd get a big memory problem.
2849 numdigits
= int(op1
.exp
- op2
.exp
)
2851 numdigits
= -numdigits
2859 if shouldround
and numdigits
> prec
+ 1:
2860 # Big difference in exponents - check the adjusted exponents
2861 tmp_len
= len(str(tmp
.int))
2862 other_len
= len(str(other
.int))
2863 if numdigits
> (other_len
+ prec
+ 1 - tmp_len
):
2864 # If the difference in adjusted exps is > prec+1, we know
2865 # other is insignificant, so might as well put a 1 after the precision.
2866 # (since this is only for addition.) Also stops use of massive longs.
2868 extend
= prec
+ 2 - tmp_len
2871 tmp
.int *= 10 ** extend
2877 tmp
.int *= 10 ** numdigits
2878 tmp
.exp
-= numdigits
2881 def _adjust_coefficients(op1
, op2
):
2882 """Adjust op1, op2 so that op2.int * 10 > op1.int >= op2.int.
2884 Returns the adjusted op1, op2 as well as the change in op1.exp-op2.exp.
2886 Used on _WorkRep instances during division.
2889 #If op1 is smaller, make it larger
2890 while op2
.int > op1
.int:
2895 #If op2 is too small, make it larger
2896 while op1
.int >= (10 * op2
.int):
2901 return op1
, op2
, adjust
2903 ##### Helper Functions ########################################
2905 def _convert_other(other
):
2906 """Convert other to Decimal.
2908 Verifies that it's ok to use in an implicit construction.
2910 if isinstance(other
, Decimal
):
2912 if isinstance(other
, (int, long)):
2913 return Decimal(other
)
2914 return NotImplemented
2925 def _isinfinity(num
):
2926 """Determines whether a string or float is infinity.
2928 +1 for negative infinity; 0 for finite ; +1 for positive infinity
2930 num
= str(num
).lower()
2931 return _infinity_map
.get(num
, 0)
2934 """Determines whether a string or float is NaN
2936 (1, sign, diagnostic info as string) => NaN
2937 (2, sign, diagnostic info as string) => sNaN
2940 num
= str(num
).lower()
2944 #get the sign, get rid of trailing [+-]
2948 elif num
[0] == '-': #elif avoids '+-nan'
2952 if num
.startswith('nan'):
2953 if len(num
) > 3 and not num
[3:].isdigit(): #diagnostic info
2955 return (1, sign
, num
[3:].lstrip('0'))
2956 if num
.startswith('snan'):
2957 if len(num
) > 4 and not num
[4:].isdigit():
2959 return (2, sign
, num
[4:].lstrip('0'))
2963 ##### Setup Specific Contexts ################################
2965 # The default context prototype used by Context()
2966 # Is mutable, so that new contexts can have different default values
2968 DefaultContext
= Context(
2969 prec
=28, rounding
=ROUND_HALF_EVEN
,
2970 traps
=[DivisionByZero
, Overflow
, InvalidOperation
],
2972 _rounding_decision
=ALWAYS_ROUND
,
2978 # Pre-made alternate contexts offered by the specification
2979 # Don't change these; the user should be able to select these
2980 # contexts and be able to reproduce results from other implementations
2983 BasicContext
= Context(
2984 prec
=9, rounding
=ROUND_HALF_UP
,
2985 traps
=[DivisionByZero
, Overflow
, InvalidOperation
, Clamped
, Underflow
],
2989 ExtendedContext
= Context(
2990 prec
=9, rounding
=ROUND_HALF_EVEN
,
2996 ##### Useful Constants (internal use only) ####################
2999 Inf
= Decimal('Inf')
3000 negInf
= Decimal('-Inf')
3002 #Infsign[sign] is infinity w/ that sign
3003 Infsign
= (Inf
, negInf
)
3005 NaN
= Decimal('NaN')
3008 ##### crud for parsing strings #################################
3011 # There's an optional sign at the start, and an optional exponent
3012 # at the end. The exponent has an optional sign and at least one
3013 # digit. In between, must have either at least one digit followed
3014 # by an optional fraction, or a decimal point followed by at least
3017 _parser
= re
.compile(r
"""
3021 (?P<int>\d+) (\. (?P<frac>\d*))?
3023 \. (?P<onlyfrac>\d+)
3025 ([eE](?P<exp>[-+]? \d+))?
3028 """, re
.VERBOSE
).match
#Uncomment the \s* to allow leading or trailing spaces.
3032 # return sign, n, p s.t. float string value == -1**sign * n * 10**p exactly
3034 def _string2exact(s
):
3037 raise ValueError("invalid literal for Decimal: %r" % s
)
3039 if m
.group('sign') == "-":
3044 exp
= m
.group('exp')
3050 intpart
= m
.group('int')
3053 fracpart
= m
.group('onlyfrac')
3055 fracpart
= m
.group('frac')
3056 if fracpart
is None:
3059 exp
-= len(fracpart
)
3061 mantissa
= intpart
+ fracpart
3062 tmp
= map(int, mantissa
)
3064 while tmp
and tmp
[0] == 0:
3070 return (sign
, tuple(backup
), exp
)
3071 return (sign
, (0,), exp
)
3072 mantissa
= tuple(tmp
)
3074 return (sign
, mantissa
, exp
)
3077 if __name__
== '__main__':
3079 doctest
.testmod(sys
.modules
[__name__
])