mņ
­fIc           @   sQ  d  Z  d d d d d d d d d	 d
 d d d d d d d d d d d d d g Z d k Z d Z d Z d Z d Z d Z d Z	 d Z
 d Z d Z d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d	 e e f d      YZ d! e f d"     YZ d# e e f d$     YZ d
 e f d%     YZ d& e f d'     YZ d e f d(     YZ d e f d)     YZ d e e f d*     YZ d e e e f d+     YZ e e e e e e e e g Z h  e e <e e <e e <e e <Z y d k Z Wn> e j
 o2 d k  Z  d, f  d-     YZ! e!   Z [  [! n Xy e i" WnK e# j
 o? e$ e i%   d.  o e i%   `& n d/   Z' d0   Z( nF Xe i"   Z" e$ e" d.  o
 e" `& n e" d1  Z( e" d2  Z' [ [" d e) f d3     YZ* g  Z+ e* i, i-   D]! Z. e. i/ d4  o e+ e. n q[+ Z0 x8 e0 D]0 Z. e. d5 i1   Z2 e3   e2 Z4 e. e* i5 e4 <qKW[. [4 [2 [0 d e) f d6     YZ6 d7 e) f d8     YZ7 d9 d9 d:  Z8 d;   Z9 d<   Z: h  d= d5 <d> d5 <d? d5 <d@ d5 <dA dB <dC dB <Z; dD   Z< dE   Z= e6 dF dG dH e dI e e e g dJ g  dK e dL dM dN dO dP d5  Z> e6 dF dQ dH e dI e e e e e g dJ g   Z? e6 dF dQ dH e dI g  dJ g   Z@ e* dR  ZA e* dS  ZB eA eB f ZC e* dT  ZD d kE ZE eE iF dU eE iG  iH ZI [E dV   ZJ eK dW j o* d kL ZL d k  Z  eL iM e  iN eK  n d S(X   sČ	  
This is a Py2.3 implementation of decimal floating point arithmetic based on
the General Decimal Arithmetic Specification:

    www2.hursley.ibm.com/decimal/decarith.html

and IEEE standard 854-1987:

    www.cs.berkeley.edu/~ejr/projects/754/private/drafts/854-1987/dir.html

Decimal floating point has finite precision with arbitrarily large bounds.

The purpose of the module is to support arithmetic using familiar
"schoolhouse" rules and to avoid the some of tricky representation
issues associated with binary floating point.  The package is especially
useful for financial applications or for contexts where users have
expectations that are at odds with binary floating point (for instance,
in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead
of the expected Decimal("0.00") returned by decimal floating point).

Here are some examples of using the decimal module:

>>> from decimal import *
>>> setcontext(ExtendedContext)
>>> Decimal(0)
Decimal("0")
>>> Decimal("1")
Decimal("1")
>>> Decimal("-.0123")
Decimal("-0.0123")
>>> Decimal(123456)
Decimal("123456")
>>> Decimal("123.45e12345678901234567890")
Decimal("1.2345E+12345678901234567892")
>>> Decimal("1.33") + Decimal("1.27")
Decimal("2.60")
>>> Decimal("12.34") + Decimal("3.87") - Decimal("18.41")
Decimal("-2.20")
>>> dig = Decimal(1)
>>> print dig / Decimal(3)
0.333333333
>>> getcontext().prec = 18
>>> print dig / Decimal(3)
0.333333333333333333
>>> print dig.sqrt()
1
>>> print Decimal(3).sqrt()
1.73205080756887729
>>> print Decimal(3) ** 123
4.85192780976896427E+58
>>> inf = Decimal(1) / Decimal(0)
>>> print inf
Infinity
>>> neginf = Decimal(-1) / Decimal(0)
>>> print neginf
-Infinity
>>> print neginf + inf
NaN
>>> print neginf * inf
-Infinity
>>> print dig / 0
Infinity
>>> getcontext().traps[DivisionByZero] = 1
>>> print dig / 0
Traceback (most recent call last):
  ...
  ...
  ...
DivisionByZero: x / 0
>>> c = Context()
>>> c.traps[InvalidOperation] = 0
>>> print c.flags[InvalidOperation]
0
>>> c.divide(Decimal(0), Decimal(0))
Decimal("NaN")
>>> c.traps[InvalidOperation] = 1
>>> print c.flags[InvalidOperation]
1
>>> c.flags[InvalidOperation] = 0
>>> print c.flags[InvalidOperation]
0
>>> print c.divide(Decimal(0), Decimal(0))
Traceback (most recent call last):
  ...
  ...
  ...
InvalidOperation: 0 / 0
>>> print c.flags[InvalidOperation]
1
>>> c.flags[InvalidOperation] = 0
>>> c.traps[InvalidOperation] = 0
>>> print c.divide(Decimal(0), Decimal(0))
NaN
>>> print c.flags[InvalidOperation]
1
>>>
t   Decimalt   Contextt   DefaultContextt   BasicContextt   ExtendedContextt   DecimalExceptiont   Clampedt   InvalidOperationt   DivisionByZerot   Inexactt   Roundedt	   Subnormalt   Overflowt	   Underflowt
   ROUND_DOWNt   ROUND_HALF_UPt   ROUND_HALF_EVENt   ROUND_CEILINGt   ROUND_FLOORt   ROUND_UPt   ROUND_HALF_DOWNt
   setcontextt
   getcontextNt   NEVER_ROUNDt   ALWAYS_ROUNDc           B   s   t  Z d  Z d   Z RS(   s-  Base exception class.

    Used exceptions derive from this.
    If an exception derives from another exception besides this (such as
    Underflow (Inexact, Rounded, Subnormal) that indicates that it is only
    called if the others are present.  This isn't actually used for
    anything, though.

    handle  -- Called when context._raise_error is called and the
               trap_enabler is set.  First argument is self, second is the
               context.  More arguments can be given, those being after
               the explanation in _raise_error (For example,
               context._raise_error(NewError, '(-x)!', self._sign) would
               call NewError().handle(context, self._sign).)

    To define a new exception, it should be sufficient to have it derive
    from DecimalException.
    c         G   s   d  S(   N(    (   t   selft   contextt   args(    (    t$   /mit/python/lib/python2.4/decimal.pyt   handle­   s    (   t   __name__t
   __module__t   __doc__R   (    (    (    R   R      s    c           B   s   t  Z d  Z RS(   s(  Exponent of a 0 changed to fit bounds.

    This occurs and signals clamped if the exponent of a result has been
    altered in order to fit the constraints of a specific concrete
    representation. This may occur when the exponent of a zero result would
    be outside the bounds of a representation, or  when a large normal
    number would have an encoded exponent that cannot be represented. In
    this latter case, the exponent is reduced to fit and the corresponding
    number of zero digits are appended to the coefficient ("fold-down").
    (   R   R   R    (    (    (    R   R   ±   s   
c           B   s   t  Z d  Z d   Z RS(   sD  An invalid operation was performed.

    Various bad things cause this:

    Something creates a signaling NaN
    -INF + INF
     0 * (+-)INF
     (+-)INF / (+-)INF
    x % 0
    (+-)INF % x
    x._rescale( non-integer )
    sqrt(-x) , x > 0
    0 ** 0
    x ** (non-integer)
    x ** (+-)INF
    An operand is invalid
    c         G   sE   | o: | d d j o% t | d i | d i d f  SqA n t S(   Ni    i   t   n(   R   R    t   _signt   _intt   NaN(   R   R   R   (    (    R   R   Š   s    )(   R   R   R    R   (    (    (    R   R   ¾   s    t   ConversionSyntaxc           B   s   t  Z d  Z d   Z RS(   sÜ   Trying to convert badly formed string.

    This occurs and signals invalid-operation if an string is being
    converted to a number and it does not conform to the numeric string
    syntax. The result is [0,qNaN].
    c         G   s   d d d f S(   Ni    R!   (   i    (    (   R   R   R   (    (    R   R   Ž   s    (   R   R   R    R   (    (    (    R   R%   Ö   s    c           B   s   t  Z d  Z e d  Z RS(   s²  Division by 0.

    This occurs and signals division-by-zero if division of a finite number
    by zero was attempted (during a divide-integer or divide operation, or a
    power operation with negative right-hand operand), and the dividend was
    not zero.

    The result of the operation is [sign,inf], where sign is the exclusive
    or of the signs of the operands for divide, or is 1 for an odd power of
    -0, for power.
    c         G   s(   | d  j	 o t | f d Sn t | S(   Ni   (   t   doublet   Nonet   Infsignt   sign(   R   R   R)   R&   R   (    (    R   R   ī   s    (   R   R   R    R'   R   (    (    (    R   R   į   s    t   DivisionImpossiblec           B   s   t  Z d  Z d   Z RS(   sņ   Cannot perform the division adequately.

    This occurs and signals invalid-operation if the integer result of a
    divide-integer or remainder operation had too many digits (would be
    longer than precision). The result is [0,qNaN].
    c         G   s
   t  t  f S(   N(   R$   (   R   R   R   (    (    R   R   ū   s    (   R   R   R    R   (    (    (    R   R*   ó   s    t   DivisionUndefinedc           B   s   t  Z d  Z e d  Z RS(   sķ   Undefined result of division.

    This occurs and signals invalid-operation if division by zero was
    attempted (during a divide-integer, divide, or remainder operation), and
    the dividend is also zero. The result is [0,qNaN].
    c         G   s   | d  j	 o t t f Sn t S(   N(   t   tupR'   R$   (   R   R   R,   R   (    (    R   R     s    (   R   R   R    R'   R   (    (    (    R   R+   ž   s    c           B   s   t  Z d  Z RS(   s¬  Had to round, losing information.

    This occurs and signals inexact whenever the result of an operation is
    not exact (that is, it needed to be rounded and any discarded digits
    were non-zero), or if an overflow or underflow condition occurs. The
    result in all cases is unchanged.

    The inexact signal may be tested (or trapped) to determine if a given
    operation (or sequence of operations) was inexact.
    (   R   R   R    (    (    (    R   R	     s   
t   InvalidContextc           B   s   t  Z d  Z d   Z RS(   sé  Invalid context.  Unknown rounding, for example.

    This occurs and signals invalid-operation if an invalid context was
    detected during an operation. This can occur if contexts are not checked
    on creation and either the precision exceeds the capability of the
    underlying concrete representation or an unknown or unsupported rounding
    was specified. These aspects of the context need only be checked when
    the values are required to be used. The result is [0,qNaN].
    c         G   s   t  S(   N(   R$   (   R   R   R   (    (    R   R   #  s    (   R   R   R    R   (    (    (    R   R-     s   	 c           B   s   t  Z d  Z RS(   sŲ  Number got rounded (not  necessarily changed during rounding).

    This occurs and signals rounded whenever the result of an operation is
    rounded (that is, some zero or non-zero digits were discarded from the
    coefficient), or if an overflow or underflow condition occurs. The
    result in all cases is unchanged.

    The rounded signal may be tested (or trapped) to determine if a given
    operation (or sequence of operations) caused a loss of precision.
    (   R   R   R    (    (    (    R   R
   &  s   
c           B   s   t  Z d  Z RS(   s  Exponent < Emin before rounding.

    This occurs and signals subnormal whenever the result of a conversion or
    operation is subnormal (that is, its adjusted exponent is less than
    Emin, before any rounding). The result in all cases is unchanged.

    The subnormal signal may be tested (or trapped) to determine if a given
    or operation (or sequence of operations) yielded a subnormal result.
    (   R   R   R    (    (    (    R   R   3  s   	c           B   s   t  Z d  Z d   Z RS(   s  Numerical overflow.

    This occurs and signals overflow if the adjusted exponent of a result
    (from a conversion or from an operation that is not an attempt to divide
    by zero), after rounding, would be greater than the largest value that
    can be handled by the implementation (the value Emax).

    The result depends on the rounding mode:

    For round-half-up and round-half-even (and for round-half-down and
    round-up, if implemented), the result of the operation is [sign,inf],
    where sign is the sign of the intermediate result. For round-down, the
    result is the largest finite number that can be represented in the
    current precision, with the sign of the intermediate result. For
    round-ceiling, the result is the same as for round-down if the sign of
    the intermediate result is 1, or is [0,inf] otherwise. For round-floor,
    the result is the same as for round-down if the sign of the intermediate
    result is 0, or is [1,inf] otherwise. In all cases, Inexact and Rounded
    will also be raised.
   c         G   sÖ   | i t t t t f j o t | Sn | d j oH | i t j o t | Sn t	 | d | i
 | i | i
 d f  Sn | d j oH | i t j o t | Sn t	 | d | i
 | i | i
 d f  Sn d  S(   Ni    i	   i   (   i	   (   i	   (   R   t   roundingR   R   R   R   R(   R)   R   R    t   prect   EmaxR   (   R   R   R)   R   (    (    R   R   U  s    ,(   R   R   R    R   (    (    (    R   R   ?  s    c           B   s   t  Z d  Z RS(   sv  Numerical underflow with result rounded to 0.

    This occurs and signals underflow if a result is inexact and the
    adjusted exponent of the result would be smaller (more negative) than
    the smallest value that can be handled by the implementation (the value
    Emin). That is, the result is both inexact and subnormal.

    The result after an underflow will be a subnormal number rounded, if
    necessary, so that its exponent is not less than Etiny. This may result
    in 0 with the sign of the intermediate result and an exponent of Etiny.

    In all cases, Inexact, Rounded, and Subnormal will also be raised.
    (   R   R   R    (    (    (    R   R   e  s   t   MockThreadingc           B   s   t  Z e d  Z RS(   Nc         C   s   | i t S(   N(   t   syst   modulesR   (   R   R2   (    (    R   t   local  s    (   R   R   R2   R4   (    (    (    R   R1     s   t   __decimal_context__c         C   sC   |  t t t f j o |  i   }  |  i   n |  t i   _ d S(   s%   Set this thread's context to context.N(	   R   R   R   R   t   copyt   clear_flagst	   threadingt   currentThreadR5   (   R   (    (    R   R     s
     c          C   sG   y t  i   i SWn/ t j
 o# t   }  |  t  i   _ |  Sn Xd S(   sĶ   Returns this thread's context.

        If this thread does not yet have a context, returns
        a new context and sets this thread's context.
        New contexts are copies of DefaultContext.
        N(   R8   R9   R5   t   AttributeErrorR   R   (   R   (    (    R   R   ¢  s     	c         C   s;   y |  i SWn) t j
 o t   } | |  _ | Sn Xd S(   sĶ   Returns this thread's context.

        If this thread does not yet have a context, returns
        a new context and sets this thread's context.
        New contexts are copies of DefaultContext.
        N(   t   _localR5   R:   R   R   (   R;   R   (    (    R   R   ¶  s     		c         C   s=   |  t t t f j o |  i   }  |  i   n |  | _ d S(   s%   Set this thread's context to context.N(   R   R   R   R   R6   R7   R;   R5   (   R   R;   (    (    R   R   Ä  s
     c           B   sź  t  Z d  Z dE Z d e d  Z d   Z d   Z e e d	  Z d
   Z	 e d  Z
 d   Z d   Z e d  Z d   Z d   Z d   Z d e d  Z e d  Z e d  Z e d  Z d e d  Z e d  Z e Z e d  Z e d  Z d e d  Z e d  Z e Z e d  Z e Z d e d  Z e d   Z  e  Z! e d!  Z" e d"  Z# e d#  Z$ e d$  Z% e d%  Z& e d&  Z' e d'  Z( d(   Z) d)   Z* d*   Z+ d+   Z, d,   Z- e e e d-  Z. h  Z/ d.   Z0 e d/  Z1 d0   Z2 d1   Z3 d2   Z4 d3   Z5 d4   Z6 e e d5  Z7 e d6  Z8 e d7  Z9 e e d d8  Z: d9   Z; e e d d:  Z< e e d;  Z= e d<  Z> e d=  Z? e d>  Z@ d?   ZA d@   ZB dA   ZC dB   ZD dC   ZE dD   ZF RS(F   s,   Floating point class for decimal arithmetic.t   _expR#   R"   t   _is_specialt   0c         C   s   t  i |   } t | _ t | t  oG | i	 | _
 t t t t | i    | _ t | i  | _ | Sn t | t  o8 | i | _ | i
 | _
 | i | _ | i | _ | Sn t | t t f  oX | d j o d | _
 n
 d | _
 d | _ t t t t t |     | _ | Sn t | t t f  oź t |  d j o t d  n | d d j o t d  n xC | d D]7 } t | t t f  p | d j  o t d  qqW| d | _
 t | d  | _ | d d j o | d | _ t | _ n t | d  | _ | Sn t | t  o t d d   n | d j o t   } n t | t  oyt  |  oL d | _ d | _ t | _ t  |  d j o d | _
 n
 d | _
 | Sn t! |  o© t! |  \ } } } t | _ t |  | i$ j o) | i% t&  \ | _
 | _ | _ | Sn | d j o d	 | _ n
 d
 | _ | | _
 t t t |   | _ | Sn y" t' |  \ | _
 | _ | _ Wn= t j
 o1 t | _ | i% t&  \ | _
 | _ | _ n X| Sn t d |   d S(   s  Create a decimal point instance.

        >>> Decimal('3.14')              # string input
        Decimal("3.14")
        >>> Decimal((0, (3, 1, 4), -2))  # tuple input (sign, digit_tuple, exponent)
        Decimal("3.14")
        >>> Decimal(314)                 # int or long
        Decimal("314")
        >>> Decimal(Decimal(314))        # another decimal instance
        Decimal("314")
        i    i   i   s   Invalid argumentss   Invalid signsP   The second value in the tuple must be composed of non negative integer elements.i   t   FR!   t   Ns"   Cannot convert float to Decimal.  s#   First convert the float to a strings   Cannot convert %r to DecimalN(   i    i   (   R?   R!   R@   (   i    ((   t   objectt   __new__t   clsR   t   FalseR=   t
   isinstancet   valuet   _WorkRepR)   R"   t   tuplet   mapt   intt   strR#   t   expR<   R    t   longt   abst   listt   lent
   ValueErrort   digitt   Truet   floatt	   TypeErrorR   R'   R   t
   basestringt   _isinfinityt   _isnant   sigt   diagR/   t   _raise_errorR%   t   _string2exact(   RC   RF   R   R)   RZ   R   RY   RR   (    (    R   RB   Ł  s     	!		$ $					!		"	&c         C   sE   |  i o7 |  i } | d j o d SqA | d j o d SqA n d S(   sr   Returns whether the number is not actually one.

        0 if a number
        1 if NaN
        2 if sNaN
        R!   i   R@   i   i    N(   R   R=   R<   RL   (   R   RL   (    (    R   RX   @  s     
	c         C   s.   |  i d j o |  i o d Sn d Sn d S(   sy   Returns whether the number is infinite

        0 if finite or not a number
        1 if +INF
        -1 if -INF
        R?   i’’’’i   i    N(   R   R<   R"   (   R   (    (    R   RW   O  s     
c         C   sĄ   |  i   } | d j o
 t } n | i   } | p | o | d j o t   } n | d j o | i	 t
 d d |   Sn | d j o | i	 t
 d d |  Sn | o |  Sn | Sn d S(   s½   Returns whether the number is not actually one.

        if self, other are sNaN, signal
        if self, other are NaN return nan
        return 0

        Done before operations.
        i   t   sNaNi   i    N(   R   RX   t   self_is_nant   otherR'   RD   t   other_is_nanR   R   R[   R   (   R   R_   R   R`   R^   (    (    R   t   _check_nans\  s$     
c         C   s%   |  i o d Sn t |  i  d j S(   sO   Is the number non-zero?

        0 if self == 0
        1 if self != 0
        i   i    N(   R   R=   t   sumR#   (   R   (    (    R   t   __nonzero__|  s     
c   	      C   s  t  |  } | t j o | Sn |  i p
 | i o> |  i | |  } | o d Sn t |  i	   | i	    Sn |  o | o d Sn | i
 |  i
 j  o d Sn |  i
 | i
 j  o d Sn |  i   } | i   } | | j o? |  i d |  i | i | i d | i |  i j o d Snb | | j o# |  i d d j o d |  i
 Sn2 | | j  o$ | i d d j o d |  i
 Sn | d  j o t   } n | i   } | i t  } | i   } |  i | d | } | i |   | | _ | p d Sn | i
 o d Sn d S(   Ni   i    i’’’’R   (   i    (   i    (   t   _convert_otherR_   t   NotImplementedR   R=   Ra   R   t   anst   cmpRW   R"   t   adjustedt   self_adjustedt   other_adjustedR#   R<   R'   R   t   _shallow_copyt   _set_roundingR   R.   t   _ignore_all_flagst   flagst   __sub__t   rest   _regard_flags(	   R   R_   R   Ri   R.   Rp   Rj   Rn   Rf   (    (    R   t   __cmp__  sF    D!!	
c         C   s4   t  | t t t f  p t Sn |  i |  d j S(   Ni    (   RE   R_   R    RJ   RM   Re   R   Rr   (   R   R_   (    (    R   t   __eq__»  s    c         C   s4   t  | t t t f  p t Sn |  i |  d j S(   Ni    (   RE   R_   R    RJ   RM   Re   R   Rr   (   R   R_   (    (    R   t   __ne__Ą  s    c         C   sw   t  |  } | t j o | Sn |  i p | o/ | i o% |  i | |  } | o | Sqa n t |  i	 | |   S(   s­   Compares one to another.

        -1 => a < b
        0  => a = b
        1  => a > b
        NaN => one is NaN
        Like __cmp__, but returns Decimal instances.
        N(
   Rd   R_   Re   R   R=   Ra   R   Rf   R    Rr   (   R   R_   R   Rf   (    (    R   t   compareÅ  s     c         C   s   |  i o1 |  i   o t d   n t t |    Sn t |   } |  t |  j o t |  Sn |  i	   p t
  t t |  i     S(   s   x.__hash__() <==> hash(x)s   Cannot hash a NaN value.N(   R   R=   RX   RU   t   hashRK   RJ   t   iR    Rc   t   AssertionErrort	   normalize(   R   Rw   (    (    R   t   __hash__Ś  s     
c         C   s   |  i |  i |  i f S(   se   Represents the number as a triple tuple.

        To show the internals exactly as they are.
        N(   R   R"   R#   R<   (   R   (    (    R   t   as_tupleé  s     c         C   s   d t  |   S(   s0   Represents the number as an instance of Decimal.s   Decimal("%s")N(   RK   R   (   R   (    (    R   t   __repr__š  s     i    c         C   sI  |  i o¬ |  i   ou d |  i }
 |  i d j o
 d } n d i t t	 |  i   } |  i   d j o |
 d | Sn |
 d | Sn |  i
   o d |  i }
 |
 d Sq¶ n | d j o t   } n t t	 |  i  }	 t |  i  } |  i | } | o|  o	|  i d j  o: |  i d j o* d |  i d	 d
 t |  i  } | Sn |  i d d d d } | |  i j o d	 d
 | |  i } n d
 } | d j oQ | i o | d 7} n | d 7} | d j o | d 7} n | t	 |  7} n d |  i | } | Sn | o, | d d d } | d | d d } n | d } d } |  i d j o n,|  i d j  o! | d j o |	 i | d  nū |  i d j  o< | d j o/ d
 g t |  |	 d d +|	 i d d	  nÆ | | j o |	 i | d  n* | | j  o |	 i d
 g | |  n | o] | i p |	 i d  n, |	 i d  | d j o |	 i d  n |	 i t	 |   n | o x) |	 d d !d
 g j o g  |	 d d +q¢Wt |	  d j p( |	 d d j p |	 d i   d j o d
 g |	 d d +qn |  i o |	 i d d  n d i |	  S(   s   Return string representation of the number in scientific notation.

        Captures all of the information in the underlying representation.
        t   -i    t    i   R]   R$   t   Infinityiś’’’s   0.R>   i   i   t   Et   et   +t   .N(   i    (   R   R=   RX   R"   t   minusR#   t   infot   joinRI   RK   RW   R   R'   R   t   tmpRP   t	   numdigitsR<   t
   leftdigitst   engRN   t   sRL   t   capitalst   dotplacet   adjexpt   insertRJ   t   extendt   appendt   lower(   R   R   R   R   R   R   R   RL   R   R   R   R   (    (    R   t   __str__õ  s     

 "



 ;
c         C   s   |  i d d d |  S(   s  Convert to engineering-type string.

        Engineering notation has an exponent which is a multiple of 3, so there
        are up to 3 digits left of the decimal place.

        Same rules for when in exponential and when as a value as in __str__.
        R   i   R   N(   R   R   R   (   R   R   (    (    R   t   to_eng_stringG  s     c         C   sĆ   |  i o% |  i d |  } | o | Sq/ n |  p
 d } n |  i o
 d } n d } | d j o t   } n | i	 t
 j o& t | |  i |  i f  i |  Sn t | |  i |  i f  S(   sR   Returns a copy with the sign switched.

        Rounds, if it has reason.
        R   i    i   N(   R   R=   Ra   R   Rf   R)   R"   R'   R   t   _rounding_decisionR   R    R#   R<   t   _fix(   R   R   Rf   R)   (    (    R   t   __neg__Q  s     



&c         C   s   |  i o% |  i d |  } | o | Sq/ n |  i } |  p
 d } n | d j o t   } n | i	 t
 j o |  i |  } n t |   } | | _ | S(   sh   Returns a copy, unless it is a sNaN.

        Rounds the number (if more then precision digits)
        R   i    N(   R   R=   Ra   R   Rf   R"   R)   R'   R   R   R   R   R    (   R   R   Rf   R)   (    (    R   t   __pos__i  s     
	
	i   c         C   s£   |  i o% |  i d |  } | o | Sq/ n | p7 | d j o t   } n | i   } | i	 t
  n |  i o |  i d |  } n |  i d |  } | S(   s`   Returns the absolute value of self.

        If the second argument is 0, do not round.
        R   N(   R   R=   Ra   R   Rf   t   roundR'   R   Rk   t   _set_rounding_decisionR   R"   R   R   (   R   R   R   Rf   (    (    R   t   __abs__  s     

c         C   s’  t  |  } | t j o | Sn | d j o t   } n |  i p
 | i o |  i | |  } | o | Sn |  i
   oB |  i | i j o! | i
   o | i t d  Sn t |   Sn | i
   o t |  SqŽ n | i t j }	 t |  i | i  }
 d } | i t j o |  i | i j o
 d } n |  oE | o= t |  i | i  } | o
 d } n t | d |
 f  Sn |  pZ t |
 | i | i d  }
 | i |
 d d d | } |	 o | i |  } n | Sn | pZ t |
 |  i | i d  }
 |  i |
 d d d | } |	 o | i |  } n | Sn t |   } t |  } t  | | |	 | i  \ } } t   } | i | i j oĮ | i" | i" j oG |
 | i#   j  o | i#   }
 | i t$  n t | d |
 f  Sn | i" | i" j  o | | } } n | i d j o& d | _ | i | i | _ | _ qd | _ n9 | i d j o d | _ d	 \ | _ | _ n
 d | _ | i d j o | i" | i" | _" n | i" | i" | _" | i | _ t |  } |	 o | i |  } n | S(
   sb   Returns self + other.

        -INF + INF (or the reverse) cause InvalidOperation errors.
        s
   -INF + INFi    i   t   watchexpR   N(   i    (   i    (   i    i    (%   Rd   R_   Re   R   R'   R   R   R=   Ra   Rf   RW   R"   R[   R   R    R   R   t   shouldroundt   minR<   RL   t   negativezeroR.   R   R)   t   maxR/   t   _rescaleR   RG   t   op1t   op2t
   _normalizet   resultRJ   t   EtinyR   (   R   R_   R   R¢   R£   Rf   R)   R„   R   R   RL   (    (    R   t   __add__  s      #

				c         C   s   t  |  } | t j o | Sn |  i p
 | i o( |  i | d | } | o | Sq] n t |  } d | i
 | _
 |  i | d | S(   s   Return self + (-other)R   i   N(   Rd   R_   Re   R   R=   Ra   R   Rf   R    R   R"   R§   (   R   R_   R   R   Rf   (    (    R   Ro   õ  s     c         C   sP   t  |  } | t j o | Sn t |   } d | i | _ | i | d | S(   s   Return other + (-self)i   R   N(	   Rd   R_   Re   R    R   R   R"   R§   R   (   R   R_   R   R   (    (    R   t   __rsub__  s     c         C   s.  |  i o/ |  i d |  } | o | Sn t |   Sn t |  i  } | d c d 7<t	 |  d } x` | | d j oN d | | <| d j o d g | d d +Pn | | d c d 7<| d 8} qk Wt |  i | |  i f  } | d j o t   } n | o# | i t j o | i |  } n | S(   s  Special case of add, adding 1eExponent

        Since it is common, (rounding, for example) this adds
        (sign)*one E self._exp to the number more efficiently than add.

        For example:
        Decimal('5.624e10')._increment() == Decimal('5.625e10')
        R   i’’’’i   i
   i    N(   R   R=   Ra   R   Rf   R    RO   R#   t   LRP   t   spotR"   R<   R'   R   R   R   R   R   (   R   R   R   RŖ   R©   Rf   (    (    R   t
   _increment  s.     
 
c   	      C   sJ  t  |  } | t j o | Sn | d j o t   } n |  i | i A} |  i	 p
 | i	 o |  i
 | |  } | o | Sn |  i   o' | p | i t d  Sn t | Sn | i   o' |  p | i t d  Sn t | Sqģ n |  i | i } | i t j } |  p | o7 t | d | f  } | o | i |  } n | Sn |  i d j o: t | | i | f  } | o | i |  } n | Sn | i d j o: t | |  i | f  } | o | i |  } n | Sn t |   } t |  } t | t t t | i | i   | f  } | o | i |  } n | S(	   s\   Return self * other.

        (+-) INF * 0 (or its reverse) raise InvalidOperation.
        s   (+-)INF * 0s   0 * (+-)INFi    i   N(   i    (   i   (   i   (   Rd   R_   Re   R   R'   R   R   R"   t
   resultsignR=   Ra   Rf   RW   R[   R   R(   R<   t	   resultexpR   R   R   R    R   R#   RG   R¢   R£   RI   RJ   RK   (	   R   R_   R   R£   R¬   R¢   R   Rf   R­   (    (    R   t   __mul__4  sT     .c         C   s   |  i | d | S(   s   Return self / other.R   N(   R   t   _divideR_   R   (   R   R_   R   (    (    R   t   __div__r  s     c         C   s£  t  |  } | t j o# | d j o t Sn t t f Sn | d j o t   } n |  i | i A} |  i
 p
 | i
 o|  i | |  } | o | o | | f Sn | Sn |  i   oN | i   oA | o& | i t d  | i t d  f Sn | i t d  Sn |  i   o | d j o t | | i t d  f SnK | d j o t | t f Sn, | d j o t | | i t d  f Sn t | Sn | i   oW | o# t | d d f  t |   f Sn | i t d	  t | d | i   f  Sqn |  o: | o2 | o | i t d
 d  Sn | i t d
  Sn |  pĻ | oA t |   } t |  i | i  | _ t | d d f  | f Sn |  i | i } | | i   j  o  | i   } | i t d  n | | i j o | i } | i t d  n t | d | f  Sn | p8 | o | i t d | d  Sn | i t d |  Sn | i t j } | oŚ |  i d |  | i d |  j  oµ | d j p | d j og t |  i | i  } |  i  | d | d d } | o | i" |  } n t | d d f  | f SqH| d j o# t | d d f  t |   f SqHn t# |   } t# |  } t& | |  \ } } } t# | d | i | i f  }
 | o( |
 i | i) d j o | i t*  Sn d | i) }	 x£x9 | i, | i, j o% |
 i, d 7_, | i, | i, 8_, qŁW|
 i d j o² | o« |
 i, |	 j o | o | i t*  Sn t |  } | i-   } t |  i | i  } | i  | d | d d } | i/ |   | o | i" |  } n t |
  | f Sn | i, d j o | d j o | o Pn |
 i, |	 j ok | od | o | i t*  Sn d } | i, d j o1 |
 i, d 9_, |
 i, d 7_, |
 i d 8_ n Pn |
 i, d 9_, |
 i d 8_ | d 7} | i, d 9_, | i d 8_ |
 i d j o„ | o | i, | i, j o |
 i, |	 j o | o | i t*  Sn t |  } | i-   } t |  i | i  } | i  | d | } | i/ |   t |
  | f SqÖqÖWt |
  } | o | i" |  } n | S(   s  Return a / b, to context.prec precision.

        divmod:
        0 => true division
        1 => (a //b, a%b)
        2 => a //b
        3 => a%b

        Actually, if divmod is 2 or 3 a tuple is returned, but errors for
        computing the other value are not raised.
        i    i   s   (+-)INF // (+-)INFs   (+-)INF % (+-)INFs   (+-)INF/(+-)INFs   INF % xi   i   s   Division by infinitys   0 / 0s   0e-x / ys   0e+x / ys   divmod(x,0)s   x / 0R   R   i
   N(   i    i   (   i    (   i    (   i    (   i    (   i    (   i    (0   Rd   R_   Re   t   divmodR   R'   R   R   R"   R)   R=   Ra   Rf   RW   R[   R   R(   R$   R    R   R¦   R+   t	   othersideR   R<   RL   R0   R   R   R   R   R   R”   t   ans2R   RG   R¢   R£   t   _adjust_coefficientst   adjustRp   R/   R*   t
   prec_limitRJ   Rm   t   frozenRq   (   R   R_   R±   R   R¢   R£   R³   R)   Rf   R¶   Rp   RL   R   R²   R·   Rµ   (    (    R   RÆ   w  sī     	#!	,'   %
*c         C   s4   t  |  } | t j o | Sn | i |  d | S(   s%   Swaps self/other and returns __div__.R   N(   Rd   R_   Re   R°   R   R   (   R   R_   R   (    (    R   t   __rdiv__  s
     c         C   s   |  i | d |  S(   s/   
        (self // other, self % other)
        i   N(   R   RÆ   R_   R   (   R   R_   R   (    (    R   t
   __divmod__%  s     c         C   s4   t  |  } | t j o | Sn | i |  d | S(   s(   Swaps self/other and returns __divmod__.R   N(   Rd   R_   Re   R¹   R   R   (   R   R_   R   (    (    R   t   __rdivmod__+  s
     c         C   s   t  |  } | t j o | Sn |  i p
 | i o% |  i | |  } | o | SqZ n |  o | o | i t	 d  Sn |  i
 | d |  d S(   s   
        self % other
        s   x % 0i   i   N(   Rd   R_   Re   R   R=   Ra   R   Rf   R[   R   RÆ   (   R   R_   R   Rf   (    (    R   t   __mod__2  s     c         C   s4   t  |  } | t j o | Sn | i |  d | S(   s%   Swaps self/other and returns __mod__.R   N(   Rd   R_   Re   R»   R   R   (   R   R_   R   (    (    R   t   __rmod__D  s
     c         C   s  t  |  } | t j o | Sn |  i p
 | i o% |  i | |  } | o | SqZ n |  o | o | i t	 d  Sn | d j o t   } n | i   } | i t t  }
 |  i | d | \ } }	 |	 i   o | i |
   |	 Sn | i   } | i t  } | i o | i t d  d | } n | i t d  d | } | i |  | i |
   |	 i | i } } d \ |	 _ | _ |	 | j  o7 | | |	 _ | _ |  i | d | |	 i |  Sn | | |	 _ | _ | i t  } |  i | d | \ } }	 | i |  |	 i   o |	 Sn | i    } | i t  } | i" d |  } | i |  |	 i | i } } d	 \ |	 _ | _ |	 | j p | oÕ |	 | j oČ | | |	 _ | _ | i# d 7_# t$ | i% t d  d | i&  | i# j o$ | i# d 8_# | i t'  d Sn | i# d 8_# |  i | i j o |	 i( | d | }	 q|	 i% | d | }	 n | | |	 _ | _ |	 i |  S(
   sI   
        Remainder nearest to 0-  abs(remainder-near) <= other/2
        s   x % 0R   iž’’’i   i    i   N(   i    i    (   i    i    ()   Rd   R_   Re   R   R=   Ra   R   Rf   R[   R   R'   R   Rk   t   _ignore_flagsR
   R	   Rn   R¹   t   sidet   rRX   Rq   R   R   R.   R"   R°   R    t
   comparisont   s1t   s2R   t   _isevent   decreaseR   R/   RP   R§   R#   R*   Ro   (   R   R_   R   RÄ   Rf   R.   RĀ   RĮ   RĄ   Ræ   Rn   R¾   (    (    R   t   remainder_nearK  sn     
!.c         C   s   |  i | d |  d S(   s   self // otheri   i    N(   R   RÆ   R_   R   (   R   R_   R   (    (    R   t   __floordiv__  s     c         C   s4   t  |  } | t j o | Sn | i |  d | S(   s*   Swaps self/other and returns __floordiv__.R   N(   Rd   R_   Re   RĘ   R   R   (   R   R_   R   (    (    R   t   __rfloordiv__  s
     c         C   s   t  t |    S(   s   Float representation.N(   RT   RK   R   (   R   (    (    R   t	   __float__„  s     c         C   sŻ   |  i oE |  i   o t   } | i t  SqO |  i   o t d  qO n |  i	 d j o* d i
 t t |  i   d |  i	 } n# d i
 t t |  i   |  i	  } | d j o
 d } n d |  i } t | |  S(   s1   Converts self to an int, truncating if necessary.s   Cannot convert infinity to longi    R~   R>   R}   N(   R   R=   RX   R   R   R[   R-   RW   t   OverflowErrorR<   R   RI   RK   R#   R   R"   R)   RJ   (   R   R   R   R)   (    (    R   t   __int__©  s     
	*"
c         C   s   t  |  i    S(   sC   Converts to a long.

        Equivalent to long(int(self))
        N(   RM   R   RŹ   (   R   (    (    R   t   __long__ŗ  s     c         C   s   |  i o |  Sn | d j o t   } n | i } |  i |  } t | i	  | j o( | i
 | d | } | i |  } n | S(   sÜ   Round if it is necessary to keep self within prec precision.

        Rounds and fixes the exponent.  Does not raise on a sNaN.

        Arguments:
        self - Decimal instance
        context - context used.
        R   N(   R   R=   R   R'   R   R/   t   _fixexponentsRf   RP   R#   t   _round(   R   R   R/   Rf   (    (    R   R   Į  s     
	c   	      C   s§  | i } | i } |  } | i   } | | j  o® | i   } | i	 | j  ov | p* t
 |   } | | _	 | i t  | Sn | i | d | } | i t  | i t o | i t  qŪ q£| o | i t  q£nÅ | i   } | o6 | i	 | j o& | i t  | i | d | } n| | i } | | j oe | p* t
 |   } | | _	 | i t  | Sn | i t  | i t  | i t d | i  Sn | S(   s   Fix the exponents and return a copy with the exponent in bounds.
        Only call if known to not be a special value.
        R   s
   above EmaxN(   R   t   _clampt   folddownt   EminR   Rf   Rh   t   ans_adjustedR¦   R<   R    R[   R   R”   R   Rn   R	   R   t   EtopR0   R
   R   R"   (	   R   R   RĻ   RŃ   RŠ   R0   R¦   RŅ   Rf   (    (    R   RĢ   Õ  sD     					c         C   s  |  i o@ |  i d |  }
 |
 o |
 Sn |  i   o t |   SqJ n | d j o t   } n | d j o | i	 } n | d j o | i
 } n |  p | d j o$ d } t |  i  | |  i } n% d | } t |  i  |  i | } t |  i | | f  }
 | i t  |
 Sn | d j o& t |   } d | i | _ d } nZ | d j  o@ |  i t |  i  | d } t |  i d	 | f  } d } n t |   } t | i  } | | j o | Sn | | } | d j oJ t | i  } | i d g |  t | i | | i | f  }
 |
 Sn |  i | }	 |	 d
 t |	  j o; t | i | i |  | i | f  }
 | i t  |
 Sn t | |  i |  } | | i
 j o | i   } | | _
 n | | | |  }
 | i t  | i t d  |
 S(   s   Returns a rounded version of self.

        You can specify the precision or rounding method.  Otherwise, the
        context determines it.
        R   i    i   s   Changed in roundingN(   i    (   i    (   i    (   i    i   (   i    (   R   R=   Ra   R   Rf   RW   R    R'   R   R.   R/   t   digRP   R#   R<   RL   R"   R[   R
   t   tempR   t   expdiffRO   R   R   t
   lostdigitst   getattrt   _pick_rounding_functiont   this_functionRk   R	   (   R   R/   R.   R   R   RÕ   RL   RŌ   RÓ   RÖ   Rf   RŁ   R   (    (    R   RĶ      sj     




&c         C   s$   t  |  i |  i |  |  i | f  S(   s(   Also known as round-towards-0, truncate.N(   R    R   R"   R#   R/   R<   RÕ   (   R   R/   RÕ   R   (    (    R   t   _round_downQ  s     c         C   s©   | d j o* t |  i |  i |  |  i | f  } n |  i | d j oZ | i	 d d d |  } t | i  | j o( t | i | i d  | i d f  Sq„ n | S(   s   Rounds 5 up (away from 0)i   R   i    R   i’’’’i   N(   R   R'   R    R   R"   R#   R/   R<   RÕ   R«   R   RP   (   R   R/   RÕ   R   R   (    (    R   t   _round_half_upU  s     *,c         C   s½   t  |  i |  i |  |  i | f  } |  i | d j } | o8 x5 |  i | d D] } | d j o d } PqR qR Wn | o( |  i | d d @d j o | Sq§ n |  i
 | | | |  S(   s!   Round 5 to even, rest to nearest.i   i   i    N(   R    R   R"   R#   R/   R<   RÕ   R   t   halfRR   RŪ   R   (   R   R/   RÕ   R   R   RR   RÜ   (    (    R   t   _round_half_even`  s     & c         C   s   t  |  i |  i |  |  i | f  } |  i | d j } | o8 x5 |  i | d D] } | d j o d } PqR qR Wn | o | Sn |  i
 | | | |  S(   s   Round 5 downi   i   i    N(   R    R   R"   R#   R/   R<   RÕ   R   RÜ   RR   RŪ   R   (   R   R/   RÕ   R   R   RR   RÜ   (    (    R   t   _round_half_downo  s     & c         C   s­   t  |  i |  i |  |  i | f  } x |  i | D]q } | d j o^ | i	 d d d |  } t | i  | j o( t  | i | i d  | i d f  Sq„ | Sq4 q4 W| S(   s   Rounds away from 0.i    R   i   R   i’’’’N(   R    R   R"   R#   R/   R<   RÕ   R   RR   R«   R   RP   (   R   R/   RÕ   R   R   RR   (    (    R   t	   _round_up}  s     & (c         C   s8   |  i o |  i | | |  Sn |  i | | |  Sd S(   s(   Rounds up (not away from 0 if negative.)N(   R   R"   RŚ   R/   RÕ   R   Rß   (   R   R/   RÕ   R   (    (    R   t   _round_ceiling  s     
c         C   s8   |  i p |  i | | |  Sn |  i | | |  Sd S(   s'   Rounds down (not towards 0 if negative)N(   R   R"   RŚ   R/   RÕ   R   Rß   (   R   R/   RÕ   R   (    (    R   t   _round_floor  s     
c         C   sš  t  |  } | t j o | Sn | d j o t   } n |  i p | i p | i   d j oY | i	   p | i   d j o | i
 t d  Sn |  i | |  } | o | Sq» n | i   p | i
 t d  Sn |  o | o | i
 t d  Sn | p t d  Sn |  t d  j o t d  Sn |  i o | i   } t |  } |  i	   oK | o | i
 t d  Sn | d j o t | Sn t | d d f  Sn | o | d j oy |  i t |  i  d | | i j oQ |  oJ t d  } | | _ | i
 t  | i
 t  | i
 t d	 |  | Sn t t t |    } | i! } | o/ | | d t# i j o | i
 t d
 |  Sn t |   }	 t d  } | i&   } | | d | _! | d j  o& | } t d  i' |	 d | }	 n d }
 x |
 | j o |
 d K}
 qW|
 d L}
 x |
 o | i) | d | } | i	   o t | } Pn |
 | @o | i) |	 d | } n | d j	 o | i* | d | } n |
 d L}
 q1W| | _! | i+ t, j o | i- |  Sn | S(   sf   Return self ** n (mod modulo)

        If modulo is None (default), don't take it mod modulo.
        i   s   x ** INFs   x ** (non-integer)s   0 ** 0i   s   INF % xi    t   infs	   Big powers   Too much precision.R   N(   i    (.   Rd   R!   Re   R   R'   R   R   R=   Rh   RW   R[   R   Ra   Rf   t
   _isintegerR    R"   RĆ   R)   RJ   t   moduloR(   R<   RP   R#   R0   R   R
   R	   R   RK   RN   t   elengthR/   t	   firstprecR   t   mult   valRk   R°   RŖ   R®   R»   R   R   R   (   R   R!   Rä   R   R)   Rå   Rf   R   Rč   Rē   RŖ   Rę   (    (    R   t   __pow__  s     ' D		  
 
	c         C   s4   t  |  } | t j o | Sn | i |  d | S(   s%   Swaps self/other and returns __pow__.R   N(   Rd   R_   Re   Ré   R   R   (   R   R_   R   (    (    R   t   __rpow__ņ  s
     c         C   sŻ   |  i o% |  i d |  } | o | Sq/ n |  i |  } | i   o | Sn | p t | i	 d d f  Sn t
 | i  } | i } x1 | i | d d j o | d 7} | d 8} q Wt | i	 | i |  | f  S(   s?   Normalize- strip trailing 0s, change anything equal to 0 to 0e0R   i    i   N(   i    (   R   R=   Ra   R   Rf   R   t   dupRW   R    R"   RP   R#   t   endR<   RL   (   R   R   Rģ   RL   Rf   Rė   (    (    R   Ry   ł  s"     
	 
c         C   s¼   |  i p
 | i o |  i | |  } | o | Sn | i   p |  i   oP | i   o |  i   o |  Sn | d j o t   } n | i	 t
 d  Sq£ n |  i | i | | |  S(   s   Quantize self so its exponent is the same as that of exp.

        Similar to self._rescale(exp._exp) but with error checking.
        s   quantize with one INFN(   R   R=   RL   Ra   R   Rf   RW   R'   R   R[   R   R”   R<   R.   R   (   R   RL   R.   R   R   Rf   (    (    R   t   quantize  s     	c         C   s    |  i p
 | i o| |  i   p | i   o" |  i   o | i   o t Sn |  i   p | i   o" |  i   o | i   o t Sq n |  i | i j S(   sy   Test whether self and other have the same exponent.

        same as self._exp == other._exp, except NaN == sNaN
        N(   R   R=   R_   RX   RS   RW   R<   (   R   R_   (    (    R   t   same_quantum"  s     "&c   
      C   s9  | d j o t   } n |  i oF |  i   o | i t d  Sn |  i d |  } | o | Sqj n | o7 | i | j  p | i   | j o | i t d  Sn |  p& t |   } d | _ | | _ | Sn |  i | }	 t |  i  |	 } | o$ | | i j o | i t d  Sn t |   } d	 | i | _ | d 7} | d j  o$ | | i | _ d
 | _ d } n | i | | d | } | i d d j o* t | i  d j o | i d | _ n | | _ | i   } | o! | | i j  o | i t  n, | o$ | | i j o | i t d  Sn | S(   sī   Rescales so that the exponent is exp.

        exp = exp to scale to (an integer)
        rounding = rounding version
        watchexp: if set (default) an error is returned if exp is greater
        than Emax or less than Etiny.
        s   rescale with an INFR   s   rescale(a, INF)i    s   Rescale > preci   N(   i    (   i    (   i    i   (   R   R'   R   R   R=   RW   R[   R   Ra   Rf   R   R0   RL   R¦   R    R#   R<   t   diffRP   t   digitsR/   R   RĶ   R.   Rh   t   tmp_adjustedRŠ   R   (
   R   RL   R.   R   R   Rš   R   Rń   Rf   Rļ   (    (    R   R”   .  sJ     
*		
	
*	c         C   s   |  i o% |  i d |  } | o | Sq/ n |  i d j o |  Sn | d j o t   } n | i t	 t
  } |  i d | d | } | i |  | S(   s@   Rounds to the nearest integer, without raising inexact, rounded.R   i    N(   R   R=   Ra   R   Rf   R<   R'   R   R½   R
   R	   Rn   R”   R.   Rq   (   R   R.   R   Rn   Rf   (    (    R   t   to_integrale  s     
c         C   s|  |  i oP |  i d |  } | o | Sn |  i   o |  i d j o t |   SqZ n |  pK |  i d } |  i d j o t d d | f  Sq¬ t d d | f  Sn | d j o t   } n |  i d j o | i t d  Sn t |   } | i d } | i d @o | i d 7_ d | _ n
 d | _ | i   } | i   } | i } d | _ | i   d @d j os t d d | i   d f  } | i | i t d d d
 f  d | d | } | i d | i   d 8_ nz t d d | i t | i  d f  } | i | i t d d d f  d | d | } | i d | i   d 8_ | i | i } }	 t i t i | _ | _ t d  }
 | d } | i t   } xj t" d | i d |  | _ |
 i | i | i# | d | d | d | } | i | j o Pq·q·W| | _ | i   } | i% d |  } | d | _ | | i   j o" | i d 7_ | i d 8_ n | i& t d d | i d f  d | } | i t(  | i | d | | j o+ | i& t d d | i f  d | } n | i t d d | i d f  d | } | i t*  | i | d | | j  o+ | i t d d | i f  d | } n | i | 7_ | | _ | | _! | i+ |  } | i, t-  } | i | d | |  j p+ | i. |  | i t/  | i t0  nO |  i d } | i | i | 7_ | i1 | d | } | | _ | i. |  | |	 | _ | _ | i+ |  S(   s£   Return the square root of self.

        Uses a converging algorithm (Xn+1 = 0.5*(Xn + self / Xn))
        Should quadratically approach the right answer.
        R   i    i   i   s   sqrt(-x), x > 0i   i   i	   i   iž’’’iż’’’s   0.5N(   i    (   i    (   i    (   i   i   i	   (   i   i   i	   (   i   i   i	   (   i   i   i	   (   i    (   i   (   i   (   i   (   i   (2   R   R=   Ra   R   Rf   RW   R"   R    R<   RL   R'   R   R[   R   R   t   expaddR#   Rk   Rm   Rn   R/   Rę   Rh   R§   R®   RP   R0   RŠ   R   RÜ   t   maxpRl   R   R.   R   R°   t   prevexpRĶ   Ro   R   R   t   upperR   R   R   R   Rq   R
   R	   R”   (   R   R   Rö   Rf   R   R.   Rō   Ró   RL   RŠ   RÜ   R   Rõ   R0   Rn   Rę   (    (    R   t   sqrtt  s¢     
				 !)	 
  	 
	++++			c         C   s­  t  |  } | t j o | Sn |  i p
 | i o |  i   } | i   } | p | oX | d j o | d j o |  Sn | d j o | d j o | Sn |  i | |  Sq· n |  } |  i |  } | d j o~ |  i | i j o |  i o
 | } qSqn|  i | i j  o |  i o
 | } qn|  i | i j o |  i o
 | } qnn | d j o
 | } n | d j o t   } n | i t j o | i |  Sn | S(   s    Returns the larger value.

        like max(self, other) except if one is not a number, returns
        NaN (and signals if one is sNaN).  Also rounds.
        i   i   i    i’’’’N(   Rd   R_   Re   R   R=   RX   t   snt   onRa   R   Rf   Rr   t   cR"   R<   R'   R   R   R   R   (   R   R_   R   Rś   Rł   Rų   Rf   (    (    R   R    ī  s<     	


c         C   s­  t  |  } | t j o | Sn |  i p
 | i o |  i   } | i   } | p | oX | d j o | d j o |  Sn | d j o | d j o | Sn |  i | |  Sq· n |  } |  i |  } | d j o~ |  i | i j o | i o
 | } qSqn|  i | i j o |  i o
 | } qn|  i | i j  o |  i o
 | } qnn | d j o
 | } n | d j o t   } n | i t j o | i |  Sn | S(   s”   Returns the smaller value.

        like min(self, other) except if one is not a number, returns
        NaN (and signals if one is sNaN).  Also rounds.
        i   i   i    N(   Rd   R_   Re   R   R=   RX   Rų   Rł   Ra   R   Rf   Rr   Rś   R"   R<   R'   R   R   R   R   (   R   R_   R   Rś   Rł   Rų   Rf   (    (    R   R     s<     	


c         C   s<   |  i d j o t Sn |  i |  i } | d t |  j S(   s"   Returns whether self is an integeri    N(   i    (   R   R<   RS   R#   t   restRP   (   R   Rū   (    (    R   Rć   P  s
     c         C   s4   |  i d j o d Sn |  i d |  i d @d j S(   s7   Returns 1 if self is even.  Assumes self is an integer.i    i   i’’’’N(   R   R<   R#   (   R   (    (    R   RĆ   W  s     c         C   s:   y |  i t |  i  d SWn t j
 o d Sn Xd S(   s$   Return the adjusted exponent of selfi   i    N(   R   R<   RP   R#   RU   (   R   (    (    R   Rh   ]  s
     c         C   s   |  i t |   f f S(   N(   R   t	   __class__RK   (   R   (    (    R   t
   __reduce__f  s    c         C   s.   t  |   t j o |  Sn |  i t |    S(   N(   t   typeR   R    Rü   RK   (   R   (    (    R   t   __copy__i  s    c         C   s.   t  |   t j o |  Sn |  i t |    S(   N(   Rž   R   R    Rü   RK   (   R   t   memo(    (    R   t   __deepcopy__n  s    (   s   _exps   _ints   _signs   _is_special(G   R   R   R    t	   __slots__R'   RB   RX   RW   Ra   Rc   Rr   Rs   Rt   Ru   Rz   R{   R|   R   R   R   R   R   R§   t   __radd__Ro   RØ   R«   R®   t   __rmul__R°   t   __truediv__RÆ   Rø   t   __rtruediv__R¹   Rŗ   R»   R¼   RÅ   RĘ   RĒ   RČ   RŹ   RĖ   R   RĢ   RĶ   RŲ   RŚ   RŪ   RŻ   RŽ   Rß   Rą   Rį   Ré   Rź   Ry   Rķ   Rī   R”   Rņ   R÷   R    R   Rć   RĆ   Rh   Rż   R’   R  (    (    (    R   R    Š  s    g		 	
5					R
Z
"<¦O					+O						[	7z11						t   _round_i   c           B   s  t  Z d  Z e e e e e e e e d e d 
 Z d   Z d   Z d   Z d   Z e Z	 e d  Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z  e d  Z! d    Z" d!   Z# d"   Z$ d#   Z% d$   Z& d%   Z' d&   Z( d'   Z) d(   Z* RS()   s,  Contains the context for a Decimal instance.

    Contains:
    prec - precision (for use in rounding, division, square roots..)
    rounding - rounding type. (how you round)
    _rounding_decision - ALWAYS_ROUND, NEVER_ROUND -- do you round?
    traps - If traps[exception] = 1, then the exception is
                    raised when it is caused.  Otherwise, a value is
                    substituted in.
    flags  - When an exception is caused, flags[exception] is incremented.
             (Whether or not the trap_enabler is set)
             Should be reset by user of Decimal instance.
    Emin -   Minimum exponent
    Emax -   Maximum exponent
    capitals -      If 1, 1*10^1 is printed as 1E+1.
                    If 0, printed as 1e1
    _clamp - If 1, change exponents if too high (Default 0)
    i    c         C   s=  | d  j o
 g  } n |
 d  j o
 g  }
 n t | t  p: t g  } t D] } | | | | j f qL ~  } ~ n | d  j	 oK t | t  o: t g  } t D] } | | | | j f q¤ ~  } ~ n x` t	   i
   D]O \ } } | d  j o& t |  | t i t t |    qą t |  | |  qą W|  ` d  S(   N(   Rn   R'   t   _ignored_flagsRE   t   dictt   _[1]t   _signalsR   t   trapst   localst   itemst   nameRč   t   setattrR   t   _copyR6   R×   R   (   R   R/   R.   R  Rn   R   RŠ   R0   R   RĪ   R  Rč   R  R
  R   (    (    R   t   __init__  s     

33 &c         C   sŚ   g  } | i d t |    | i d d i g  } |  i i   D]! \ } } | o | | i
 q@ q@ ~  d  | i d d i g  } |  i i   D]! \ } } | o | | i
 q q ~  d  d i |  d S(   s   Show the current context.sa   Context(prec=%(prec)d, rounding=%(rounding)s, Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)ds   flags=[s   , t   ]s   traps=[t   )N(   R   R   t   varsR   R   R
  Rn   R  t   ft   vR   R  t   t(   R   R  R
  R   R  R  (    (    R   R|   «  s     VVc         C   s%   x |  i D] } d |  i | <q
 Wd S(   s   Reset all flags to zeroi    N(   R   Rn   t   flag(   R   R  (    (    R   R7   ³  s     
 c         C   sI   t  |  i |  i |  i |  i |  i |  i |  i |  i	 |  i
 |  i 
 } | S(   s!   Returns a shallow copy from self.N(   R   R   R/   R.   R  Rn   R   RŠ   R0   R   RĪ   R  t   nc(   R   R  (    (    R   Rk   ø  s
     c         C   sU   t  |  i |  i |  i i   |  i i   |  i |  i |  i	 |  i
 |  i |  i 
 } | S(   s   Returns a deep copy from self.N(   R   R   R/   R.   R  R6   Rn   R   RŠ   R0   R   RĪ   R  R  (   R   R  (    (    R   R6   æ  s
     'c         G   s~   t  i | |  } | |  i j o |   i |  |  Sn |  i | c d 7<|  i	 | p |   i |  |  Sn | |  d S(   s-  Handles an error

        If the flag is in _ignored_flags, returns the default response.
        Otherwise, it increments the flag, then, if the corresponding
        trap_enabler is set, it reaises the exception.  Otherwise, it returns
        the default value after incrementing the flag.
        i   N(   t   _condition_mapt   gett	   conditiont   errorR   R  R   R   Rn   R  t   explanation(   R   R  R  R   R  (    (    R   R[   Ē  s     c         C   s   |  i t   S(   s$   Ignore all flags, if they are raisedN(   R   R½   R  (   R   (    (    R   Rm   Ż  s     c         G   s    |  i t |  |  _ t |  S(   s$   Ignore the flags, if they are raisedN(   R   R  RO   Rn   (   R   Rn   (    (    R   R½   į  s     c         G   sT   | o( t | d t t f  o | d } n x | D] } |  i i |  q6 Wd S(   s+   Stop ignoring the flags, if they are raisedi    N(   Rn   RE   RH   RO   R  R   R  t   remove(   R   Rn   R  (    (    R   Rq   č  s     ! c         C   s   t  d  d S(   s   A Context cannot be hashed.s   Cannot hash a Context.N(   RU   (   R   (    (    R   Rz   ļ  s     c         C   s   t  |  i |  i d  S(   s!   Returns Etiny (= Emin - prec + 1)i   N(   RJ   R   RŠ   R/   (   R   (    (    R   R¦   ō  s     c         C   s   t  |  i |  i d  S(   s,   Returns maximum exponent (= Emax - prec + 1)i   N(   RJ   R   R0   R/   (   R   (    (    R   RŅ   ų  s     c         C   s   |  i } | |  _ | S(   s  Sets the rounding decision.

        Sets the rounding decision, and returns the current (previous)
        rounding decision.  Often used like:

        context = context._shallow_copy()
        # That so you don't change the calling context
        # if an error occurs in the middle (say DivisionImpossible is raised).

        rounding = context._set_rounding_decision(NEVER_ROUND)
        instance = instance / Decimal(2)
        context._set_rounding_decision(rounding)

        This will make it not round for that operation.
        N(   R   R   R.   Rž   (   R   Rž   R.   (    (    R   R   ü  s     		c         C   s   |  i } | |  _ | S(   sÓ  Sets the rounding type.

        Sets the rounding type, and returns the current (previous)
        rounding type.  Often used like:

        context = context.copy()
        # so you don't change the calling context
        # if an error occurs in the middle.
        rounding = context._set_rounding(ROUND_UP)
        val = self.__sub__(other, context=context)
        context._set_rounding(rounding)

        This will make it round up for that operation.
        N(   R   R.   Rž   (   R   Rž   R.   (    (    R   Rl   	  s     		R>   c         C   s   t  | d |  } | i |   S(   s9   Creates a new Decimal instance but using self as context.R   N(   R    t   numR   t   dR   (   R   R!  R"  (    (    R   t   create_decimal$	  s     c         C   s   | i d |   S(   s!  Returns the absolute value of the operand.

        If the operand is negative, the result is the same as using the minus
        operation on the operand. Otherwise, the result is the same as using
        the plus operation on the operand.

        >>> ExtendedContext.abs(Decimal('2.1'))
        Decimal("2.1")
        >>> ExtendedContext.abs(Decimal('-100'))
        Decimal("100")
        >>> ExtendedContext.abs(Decimal('101.5'))
        Decimal("101.5")
        >>> ExtendedContext.abs(Decimal('-101.5'))
        Decimal("101.5")
        R   N(   t   aR   R   (   R   R$  (    (    R   RN   *	  s     c         C   s   | i | d |  S(   sę   Return the sum of the two operands.

        >>> ExtendedContext.add(Decimal('12'), Decimal('7.00'))
        Decimal("19.00")
        >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4'))
        Decimal("1.02E+4")
        R   N(   R$  R§   t   bR   (   R   R$  R%  (    (    R   t   add<	  s     c         C   s   t  | i |    S(   N(   RK   R$  R   R   (   R   R$  (    (    R   t   _applyF	  s    c         C   s   | i | d |  S(   s³  Compares values numerically.

        If the signs of the operands differ, a value representing each operand
        ('-1' if the operand is less than zero, '0' if the operand is zero or
        negative zero, or '1' if the operand is greater than zero) is used in
        place of that operand for the comparison instead of the actual
        operand.

        The comparison is then effected by subtracting the second operand from
        the first and then returning a value according to the result of the
        subtraction: '-1' if the result is less than zero, '0' if the result is
        zero or negative zero, or '1' if the result is greater than zero.

        >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3'))
        Decimal("-1")
        >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1'))
        Decimal("0")
        >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10'))
        Decimal("0")
        >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1'))
        Decimal("1")
        >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3'))
        Decimal("1")
        >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1'))
        Decimal("-1")
        R   N(   R$  Ru   R%  R   (   R   R$  R%  (    (    R   Ru   I	  s     c         C   s   | i | d |  S(   s¼  Decimal division in a specified context.

        >>> ExtendedContext.divide(Decimal('1'), Decimal('3'))
        Decimal("0.333333333")
        >>> ExtendedContext.divide(Decimal('2'), Decimal('3'))
        Decimal("0.666666667")
        >>> ExtendedContext.divide(Decimal('5'), Decimal('2'))
        Decimal("2.5")
        >>> ExtendedContext.divide(Decimal('1'), Decimal('10'))
        Decimal("0.1")
        >>> ExtendedContext.divide(Decimal('12'), Decimal('12'))
        Decimal("1")
        >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2'))
        Decimal("4.00")
        >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0'))
        Decimal("1.20")
        >>> ExtendedContext.divide(Decimal('1000'), Decimal('100'))
        Decimal("10")
        >>> ExtendedContext.divide(Decimal('1000'), Decimal('1'))
        Decimal("1000")
        >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
        Decimal("1.20E+6")
        R   N(   R$  R°   R%  R   (   R   R$  R%  (    (    R   t   dividef	  s     c         C   s   | i | d |  S(   sT  Divides two numbers and returns the integer part of the result.

        >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3'))
        Decimal("0")
        >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3'))
        Decimal("3")
        >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3'))
        Decimal("3")
        R   N(   R$  RĘ   R%  R   (   R   R$  R%  (    (    R   t
   divide_int	  s    	 c         C   s   | i | d |  S(   NR   (   R$  R¹   R%  R   (   R   R$  R%  (    (    R   R±   	  s    c         C   s   | i | d |  S(   sń  max compares two values numerically and returns the maximum.

        If either operand is a NaN then the general rules apply.
        Otherwise, the operands are compared as as though by the compare
        operation. If they are numerically equal then the left-hand operand
        is chosen as the result. Otherwise the maximum (closer to positive
        infinity) of the two operands is chosen as the result.

        >>> ExtendedContext.max(Decimal('3'), Decimal('2'))
        Decimal("3")
        >>> ExtendedContext.max(Decimal('-10'), Decimal('3'))
        Decimal("3")
        >>> ExtendedContext.max(Decimal('1.0'), Decimal('1'))
        Decimal("1")
        >>> ExtendedContext.max(Decimal('7'), Decimal('NaN'))
        Decimal("7")
        R   N(   R$  R    R%  R   (   R   R$  R%  (    (    R   R    	  s     c         C   s   | i | d |  S(   sõ  min compares two values numerically and returns the minimum.

        If either operand is a NaN then the general rules apply.
        Otherwise, the operands are compared as as though by the compare
        operation. If they are numerically equal then the left-hand operand
        is chosen as the result. Otherwise the minimum (closer to negative
        infinity) of the two operands is chosen as the result.

        >>> ExtendedContext.min(Decimal('3'), Decimal('2'))
        Decimal("2")
        >>> ExtendedContext.min(Decimal('-10'), Decimal('3'))
        Decimal("-10")
        >>> ExtendedContext.min(Decimal('1.0'), Decimal('1'))
        Decimal("1.0")
        >>> ExtendedContext.min(Decimal('7'), Decimal('NaN'))
        Decimal("7")
        R   N(   R$  R   R%  R   (   R   R$  R%  (    (    R   R   £	  s     c         C   s   | i d |   S(   s  Minus corresponds to unary prefix minus in Python.

        The operation is evaluated using the same rules as subtract; the
        operation minus(a) is calculated as subtract('0', a) where the '0'
        has the same exponent as the operand.

        >>> ExtendedContext.minus(Decimal('1.3'))
        Decimal("-1.3")
        >>> ExtendedContext.minus(Decimal('-1.3'))
        Decimal("1.3")
        R   N(   R$  R   R   (   R   R$  (    (    R   R   ·	  s     c         C   s   | i | d |  S(   s  multiply multiplies two operands.

        If either operand is a special value then the general rules apply.
        Otherwise, the operands are multiplied together ('long multiplication'),
        resulting in a number which may be as long as the sum of the lengths
        of the two operands.

        >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3'))
        Decimal("3.60")
        >>> ExtendedContext.multiply(Decimal('7'), Decimal('3'))
        Decimal("21")
        >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8'))
        Decimal("0.72")
        >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0'))
        Decimal("-0.0")
        >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321'))
        Decimal("4.28135971E+11")
        R   N(   R$  R®   R%  R   (   R   R$  R%  (    (    R   t   multiplyÅ	  s     c         C   s   | i d |   S(   su  normalize reduces an operand to its simplest form.

        Essentially a plus operation with all trailing zeros removed from the
        result.

        >>> ExtendedContext.normalize(Decimal('2.1'))
        Decimal("2.1")
        >>> ExtendedContext.normalize(Decimal('-2.0'))
        Decimal("-2")
        >>> ExtendedContext.normalize(Decimal('1.200'))
        Decimal("1.2")
        >>> ExtendedContext.normalize(Decimal('-120'))
        Decimal("-1.2E+2")
        >>> ExtendedContext.normalize(Decimal('120.00'))
        Decimal("1.2E+2")
        >>> ExtendedContext.normalize(Decimal('0.00'))
        Decimal("0")
        R   N(   R$  Ry   R   (   R   R$  (    (    R   Ry   Ś	  s     c         C   s   | i d |   S(   s  Plus corresponds to unary prefix plus in Python.

        The operation is evaluated using the same rules as add; the
        operation plus(a) is calculated as add('0', a) where the '0'
        has the same exponent as the operand.

        >>> ExtendedContext.plus(Decimal('1.3'))
        Decimal("1.3")
        >>> ExtendedContext.plus(Decimal('-1.3'))
        Decimal("-1.3")
        R   N(   R$  R   R   (   R   R$  (    (    R   t   plusļ	  s     c         C   s   | i | | d |  S(   sE  Raises a to the power of b, to modulo if given.

        The right-hand operand must be a whole number whose integer part (after
        any exponent has been applied) has no more than 9 digits and whose
        fractional part (if any) is all zeros before any rounding. The operand
        may be positive, negative, or zero; if negative, the absolute value of
        the power is used, and the left-hand operand is inverted (divided into
        1) before use.

        If the increased precision needed for the intermediate calculations
        exceeds the capabilities of the implementation then an Invalid operation
        condition is raised.

        If, when raising to a negative power, an underflow occurs during the
        division into 1, the operation is not halted at that point but
        continues.

        >>> ExtendedContext.power(Decimal('2'), Decimal('3'))
        Decimal("8")
        >>> ExtendedContext.power(Decimal('2'), Decimal('-3'))
        Decimal("0.125")
        >>> ExtendedContext.power(Decimal('1.7'), Decimal('8'))
        Decimal("69.7575744")
        >>> ExtendedContext.power(Decimal('Infinity'), Decimal('-2'))
        Decimal("0")
        >>> ExtendedContext.power(Decimal('Infinity'), Decimal('-1'))
        Decimal("0")
        >>> ExtendedContext.power(Decimal('Infinity'), Decimal('0'))
        Decimal("1")
        >>> ExtendedContext.power(Decimal('Infinity'), Decimal('1'))
        Decimal("Infinity")
        >>> ExtendedContext.power(Decimal('Infinity'), Decimal('2'))
        Decimal("Infinity")
        >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('-2'))
        Decimal("0")
        >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('-1'))
        Decimal("-0")
        >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('0'))
        Decimal("1")
        >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('1'))
        Decimal("-Infinity")
        >>> ExtendedContext.power(Decimal('-Infinity'), Decimal('2'))
        Decimal("Infinity")
        >>> ExtendedContext.power(Decimal('0'), Decimal('0'))
        Decimal("NaN")
        R   N(   R$  Ré   R%  Rä   R   (   R   R$  R%  Rä   (    (    R   t   powerż	  s    . c         C   s   | i | d |  S(   sD	  Returns a value equal to 'a' (rounded) and having the exponent of 'b'.

        The coefficient of the result is derived from that of the left-hand
        operand. It may be rounded using the current rounding setting (if the
        exponent is being increased), multiplied by a positive power of ten (if
        the exponent is being decreased), or is unchanged (if the exponent is
        already equal to that of the right-hand operand).

        Unlike other operations, if the length of the coefficient after the
        quantize operation would be greater than precision then an Invalid
        operation condition is raised. This guarantees that, unless there is an
        error condition, the exponent of the result of a quantize is always
        equal to that of the right-hand operand.

        Also unlike other operations, quantize will never raise Underflow, even
        if the result is subnormal and inexact.

        >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001'))
        Decimal("2.170")
        >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01'))
        Decimal("2.17")
        >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1'))
        Decimal("2.2")
        >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0'))
        Decimal("2")
        >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1'))
        Decimal("0E+1")
        >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity'))
        Decimal("-Infinity")
        >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity'))
        Decimal("NaN")
        >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1'))
        Decimal("-0")
        >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5'))
        Decimal("-0E+5")
        >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2'))
        Decimal("NaN")
        >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2'))
        Decimal("NaN")
        >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1'))
        Decimal("217.0")
        >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0'))
        Decimal("217")
        >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1'))
        Decimal("2.2E+2")
        >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2'))
        Decimal("2E+2")
        R   N(   R$  Rķ   R%  R   (   R   R$  R%  (    (    R   Rķ   .
  s    0 c         C   s   | i | d |  S(   s=  Returns the remainder from integer division.

        The result is the residue of the dividend after the operation of
        calculating integer division as described for divide-integer, rounded to
        precision digits if necessary. The sign of the result, if non-zero, is
        the same as that of the original dividend.

        This operation will fail under the same conditions as integer division
        (that is, if integer division on the same two operands would fail, the
        remainder cannot be calculated).

        >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3'))
        Decimal("2.1")
        >>> ExtendedContext.remainder(Decimal('10'), Decimal('3'))
        Decimal("1")
        >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3'))
        Decimal("-1")
        >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1'))
        Decimal("0.2")
        >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3'))
        Decimal("0.1")
        >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3'))
        Decimal("1.0")
        R   N(   R$  R»   R%  R   (   R   R$  R%  (    (    R   t	   remaindera
  s     c         C   s   | i | d |  S(   s_  Returns to be "a - b * n", where n is the integer nearest the exact
        value of "x / b" (if two integers are equally near then the even one
        is chosen). If the result is equal to 0 then its sign will be the
        sign of a.

        This operation will fail under the same conditions as integer division
        (that is, if integer division on the same two operands would fail, the
        remainder cannot be calculated).

        >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3'))
        Decimal("-0.9")
        >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6'))
        Decimal("-2")
        >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3'))
        Decimal("1")
        >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3'))
        Decimal("-1")
        >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1'))
        Decimal("0.2")
        >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3'))
        Decimal("0.1")
        >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3'))
        Decimal("-0.3")
        R   N(   R$  RÅ   R%  R   (   R   R$  R%  (    (    R   RÅ   |
  s     c         C   s   | i |  S(   s  Returns True if the two operands have the same exponent.

        The result is never affected by either the sign or the coefficient of
        either operand.

        >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001'))
        False
        >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01'))
        True
        >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1'))
        False
        >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf'))
        True
        N(   R$  Rī   R%  (   R   R$  R%  (    (    R   Rī   
  s     c         C   s   | i d |   S(   sp  Returns the square root of a non-negative number to context precision.

        If the result must be inexact, it is rounded using the round-half-even
        algorithm.

        >>> ExtendedContext.sqrt(Decimal('0'))
        Decimal("0")
        >>> ExtendedContext.sqrt(Decimal('-0'))
        Decimal("-0")
        >>> ExtendedContext.sqrt(Decimal('0.39'))
        Decimal("0.624499800")
        >>> ExtendedContext.sqrt(Decimal('100'))
        Decimal("10")
        >>> ExtendedContext.sqrt(Decimal('1'))
        Decimal("1")
        >>> ExtendedContext.sqrt(Decimal('1.0'))
        Decimal("1.0")
        >>> ExtendedContext.sqrt(Decimal('1.00'))
        Decimal("1.0")
        >>> ExtendedContext.sqrt(Decimal('7'))
        Decimal("2.64575131")
        >>> ExtendedContext.sqrt(Decimal('10'))
        Decimal("3.16227766")
        >>> ExtendedContext.prec
        9
        R   N(   R$  R÷   R   (   R   R$  (    (    R   R÷   Ø
  s     c         C   s   | i | d |  S(   sT  Return the difference between the two operands.

        >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07'))
        Decimal("0.23")
        >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30'))
        Decimal("0.00")
        >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07'))
        Decimal("-0.77")
        R   N(   R$  Ro   R%  R   (   R   R$  R%  (    (    R   t   subtractÅ
  s    	 c         C   s   | i d |   S(   sy   Converts a number to a string, using scientific notation.

        The operation is not affected by the context.
        R   N(   R$  R   R   (   R   R$  (    (    R   R   Ń
  s     c         C   s   | i d |   S(   sy   Converts a number to a string, using scientific notation.

        The operation is not affected by the context.
        R   N(   R$  R   R   (   R   R$  (    (    R   t   to_sci_stringŲ
  s     c         C   s   | i d |   S(   s  Rounds to an integer.

        When the operand has a negative exponent, the result is the same
        as using the quantize() operation using the given operand as the
        left-hand-operand, 1E+0 as the right-hand-operand, and the precision
        of the operand as the precision setting, except that no flags will
        be set. The rounding mode is taken from the context.

        >>> ExtendedContext.to_integral(Decimal('2.1'))
        Decimal("2")
        >>> ExtendedContext.to_integral(Decimal('100'))
        Decimal("100")
        >>> ExtendedContext.to_integral(Decimal('100.0'))
        Decimal("100")
        >>> ExtendedContext.to_integral(Decimal('101.5'))
        Decimal("102")
        >>> ExtendedContext.to_integral(Decimal('-101.5'))
        Decimal("-102")
        >>> ExtendedContext.to_integral(Decimal('10E+5'))
        Decimal("1.0E+6")
        >>> ExtendedContext.to_integral(Decimal('7.89E+77'))
        Decimal("7.89E+77")
        >>> ExtendedContext.to_integral(Decimal('-Inf'))
        Decimal("-Infinity")
        R   N(   R$  Rņ   R   (   R   R$  (    (    R   Rņ   ß
  s     (+   R   R   R    R'   R  R|   R7   Rk   R6   R’   R[   Rm   R½   Rq   Rz   R¦   RŅ   R   Rl   R#  RN   R&  R'  Ru   R(  R)  R±   R    R   R   R*  Ry   R+  R,  Rķ   R-  RÅ   Rī   R÷   R.  R   R/  Rņ   (    (    (    R   R     sP    '														
											1	3							RG   c           B   s)   t  Z d Z d  d  Z d   Z e Z RS(   NR)   RJ   RL   c         C   s“   | d  j o d  |  _ d |  _ d  |  _ n t | t  oM | i |  _ d } x | i
 D] } | d | } qX W| |  _ | i |  _ n( | d |  _ | d |  _ | d |  _ d  S(   Ni    i
   i   i   (   RF   R'   R   R)   RJ   RL   RE   R    R"   t   cumR#   RR   R<   (   R   RF   RR   R0  (    (    R   R    s    		
 	c         C   s   d |  i |  i |  i f S(   Ns   (%r, %r, %r)(   R   R)   RJ   RL   (   R   (    (    R   R|     s    (   s   signs   ints   exp(   R   R   R  R'   R  R|   R   (    (    (    R   RG   ū
  s   	i    c   
      C   s;  t  |  i | i  }	 |	 d j  o |	 }	 | } |  } n |  } | } | oĀ |	 | d j o± t	 t
 | i    } t	 t
 | i    } |	 | | d | j oj | d | } | d j o
 d } n | i  d | 9_  | i | 8_ d | _  | i | _ |  | f Sqn | i  d |	 9_  | i |	 8_ |  | f S(   sc   Normalizes op1, op2 to have the same exp and length of coefficient.

    Done during addition.
    i    i   i   i
   N(   RJ   R¢   RL   R£   R   R   R_   R   R/   RP   RK   t   tmp_lent	   other_lenR   (
   R¢   R£   R   R/   R   R   R1  R_   R2  R   (    (    R   R¤     s.     

	c         C   s   d } x@ | i |  i j o, |  i d 9_ |  i d 8_ | d 7} q	 WxD |  i d | i j o, | i d 9_ | i d 8_ | d 8} qL W|  | | f S(   s½   Adjust op1, op2 so that op2.int * 10 > op1.int >= op2.int.

    Returns the adjusted op1, op2 as well as the change in op1.exp-op2.exp.

    Used on _WorkRep instances during division.
    i    i
   i   N(   Rµ   R£   RJ   R¢   RL   (   R¢   R£   Rµ   (    (    R   R“   A  s       c         C   s@   t  |  t  o |  Sn t  |  t t f  o t |   Sn t S(   s]   Convert other to Decimal.

    Verifies that it's ok to use in an implicit construction.
    N(   RE   R_   R    RJ   RM   Re   (   R_   (    (    R   Rd   Y  s     Rā   t   infinitys   +infs	   +infinitys   -infi’’’’s	   -infinityc         C   s"   t  |   i   }  t i |  d  S(   s}   Determines whether a string or float is infinity.

    +1 for negative infinity; 0 for finite ; +1 for positive infinity
    i    N(   RK   R!  R   t   _infinity_mapR  (   R!  (    (    R   RW   m  s     c         C   s%  t  |   i   }  |  p d Sn d } |  d d j o |  d }  n& |  d d j o |  d }  d } n |  i d  oK t |   d j o |  d i   o d Sn d | |  d i d  f Sn |  i d  oK t |   d	 j o |  d	 i   o d Sn d
 | |  d	 i d  f Sn d S(   s„   Determines whether a string or float is NaN

    (1, sign, diagnostic info as string) => NaN
    (2, sign, diagnostic info as string) => sNaN
    0 => not a NaN
    i    R   i   R}   t   nani   R>   t   snani   i   N(   RK   R!  R   R)   t
   startswithRP   t   isdigitt   lstrip(   R!  R)   (    (    R   RX   u  s&     

%%R/   i   R.   R  Rn   R   R0   i’É;RŠ   i6eÄR   i	   t   Infs   -InfR$   s£   
#    \s*
    (?P<sign>[-+])?
    (
        (?P<int>\d+) (\. (?P<frac>\d*))?
    |
        \. (?P<onlyfrac>\d+)
    )
    ([eE](?P<exp>[-+]? \d+))?
#    \s*
    $
c   	      C   s  t  |   } | d  j o t d |    n | i d  d j o
 d } n d } | i d  } | d  j o
 d } n t |  } | i d  } | d  j o d } | i d	  } n' | i d
  } | d  j o
 d } n | t |  8} | | } t t |  } | } x$ | o | d d j o | d =qW| p/ | o | t |  | f Sn | d | f Sn t |  } | | | f S(   Ns   invalid literal for Decimal: %rR)   R}   i   i    RL   RJ   R~   t   onlyfract   frac(   i    (   t   _parserR   t   mR'   RQ   t   groupR)   RL   RJ   t   intpartt   fracpartRP   t   mantissaRI   R   t   backupRH   (	   R   R   R>  R)   RB  RL   RA  RC  R@  (    (    R   R\   Ś  s<    



 t   __main__(O   R    t   __all__R6   R  R   R   R   R   R   R   R   R   R   t   ArithmeticErrorR   R   R   R%   t   ZeroDivisionErrorR   R*   R+   R	   R-   R
   R   R   R   R  R  R8   t   ImportErrorR2   R1   R4   R:   t   hasattrR9   R5   R   R   RA   R    R
  t   __dict__t   keysR  R7  t   rounding_functionsRö   t
   globalnamet   globalsRč   RŲ   R   RG   R¤   R“   Rd   R4  RW   RX   R   R   R   R:  t   negInfR(   R$   t   ret   compilet   VERBOSEt   matchR=  R\   R   t   doctestt   testmodR3   (9   Rd   R“   R(   R\   R   R-   RG   R   R   R   RW   R   R   R   R   R=  RE  R  R   R  R$   R	   R:  R   R%   R   R8   R*   R   R4   R   R    R2   R  R   RM  RO  R   R1   RL  R   R
   R  R   R   R   R
  Rč   R   RT  R¤   R4  RX   R+   R   R   RP  (    (    R   t   ?t   s¼   K	&*			
’ ’ ’ ’ ’ ’ ­> ’ ’ }'		<			#		+