B
    u9a|             $   @   s  d ddddddddd	d
dd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 eZd$Zd%Zd&Zd'd(lZd'd(lZd'd(l	Z	yd'd)l
mZ edd*ZW n ek
r   d+d, ZY nX dZdZdZdZdZdZdZdZd-Ze	jd.krd/Zd/Zd0Znd1Zd1Zd2Zeed3  ZG d4d deZG d5d deZG d6d deZ G d7d de Z!G d8d	 d	ee"Z#G d9d de Z$G d:d de e"Z%G d;d
 d
eZ&G d<d de Z'G d=d deZ(G d>d deZ)G d?d de&e(Z*G d@d de&e(e)Z+G dAd dee,Z-ee#e&e*e(e+e e)e-g	Z.e!e e$e e%e e'e iZ/eeeeeeeefZ0d'd(l1Z1e12dBZ3dCd Z4dDd Z5[1ddEdZ6G dFd  d e7Z8ddHdIZ9ej:;e8 G dJdK dKe7Z<G dLd de7Z=G dMdN dNe7Z>ddOdPZ?e@jAZBdQdR ZCdSdT ZDdUdV ZEdWdX ZFddZd[ZGd\d] ZHd^d_ ZIG d`da dae7ZJeJ jKZLddbdcZMddde ZNdfdg ZOdhdidjdkdldmdndodpdq	fdrdsZPddtduZQddvdwZRe=dxee#e*e gg dydzd3d'd{ZSe=d|ee#e*e ee+gg d}ZTe=d|eg g d}ZUd'd(lVZVeVWd~eVjXeVjYB jZZ[eVWdjZZ\eVWdjZZ]eVWdeVjXeVj^B Z_[Vyd'd(l`ZaW n ek
r   Y nX dddZbdd Zcdd ZddddZedd Zfdd Zge8dZhe8dZie8dZje8d'Zke8d3Zle8dZmeheifZne	jojpZqe	jojrZse	jojtZuevdoeqd eqZw[	d(S )DecimalContextDecimalTupleDefaultContextBasicContextExtendedContextDecimalExceptionClampedInvalidOperationDivisionByZeroInexactRounded	SubnormalOverflow	UnderflowFloatOperationDivisionImpossibleInvalidContextConversionSyntaxDivisionUndefined
ROUND_DOWNROUND_HALF_UPROUND_HALF_EVENROUND_CEILINGROUND_FLOORROUND_UPROUND_HALF_DOWN
ROUND_05UP
setcontext
getcontextlocalcontextMAX_PRECMAX_EMAXMIN_EMIN	MIN_ETINYHAVE_THREADSZdecimalz1.70z2.4.2    N)
namedtuplezsign digits exponentc              G   s   | S )N )argsr'   r'    /usr/lib/python3.7/_pydecimal.py<lambda>       r*   Tl    l   NZolNZoi@Ti   c               @   s   e Zd Zdd ZdS )r   c             G   s   d S )Nr'   )selfcontextr(   r'   r'   r)   handle   s    zDecimalException.handleN)__name__
__module____qualname__r/   r'   r'   r'   r)   r      s   c               @   s   e Zd ZdS )r   N)r0   r1   r2   r'   r'   r'   r)   r      s   
c               @   s   e Zd Zdd ZdS )r	   c             G   s,   |r(t |d j|d jdd}||S tS )Nr%   nT)_dec_from_triple_sign_int_fix_nan_NaN)r-   r.   r(   ansr'   r'   r)   r/      s    
zInvalidOperation.handleN)r0   r1   r2   r/   r'   r'   r'   r)   r	      s   c               @   s   e Zd Zdd ZdS )r   c             G   s   t S )N)r8   )r-   r.   r(   r'   r'   r)   r/     s    zConversionSyntax.handleN)r0   r1   r2   r/   r'   r'   r'   r)   r      s   c               @   s   e Zd Zdd ZdS )r
   c             G   s   t | S )N)_SignedInfinity)r-   r.   signr(   r'   r'   r)   r/     s    zDivisionByZero.handleN)r0   r1   r2   r/   r'   r'   r'   r)   r
     s   c               @   s   e Zd Zdd ZdS )r   c             G   s   t S )N)r8   )r-   r.   r(   r'   r'   r)   r/     s    zDivisionImpossible.handleN)r0   r1   r2   r/   r'   r'   r'   r)   r     s   c               @   s   e Zd Zdd ZdS )r   c             G   s   t S )N)r8   )r-   r.   r(   r'   r'   r)   r/   )  s    zDivisionUndefined.handleN)r0   r1   r2   r/   r'   r'   r'   r)   r   !  s   c               @   s   e Zd ZdS )r   N)r0   r1   r2   r'   r'   r'   r)   r   ,  s   
c               @   s   e Zd Zdd ZdS )r   c             G   s   t S )N)r8   )r-   r.   r(   r'   r'   r)   r/   C  s    zInvalidContext.handleN)r0   r1   r2   r/   r'   r'   r'   r)   r   8  s   c               @   s   e Zd ZdS )r   N)r0   r1   r2   r'   r'   r'   r)   r   F  s   
c               @   s   e Zd ZdS )r   N)r0   r1   r2   r'   r'   r'   r)   r   R  s   	c               @   s   e Zd Zdd ZdS )r   c             G   s   |j ttttfkrt| S |dkrR|j tkr4t| S t|d|j |j	|j d S |dkr|j t
krlt| S t|d|j |j	|j d S d S )Nr%   9r,   )roundingr   r   r   r   r:   r   r4   precEmaxr   )r-   r.   r;   r(   r'   r'   r)   r/   s  s    


zOverflow.handleN)r0   r1   r2   r/   r'   r'   r'   r)   r   ]  s   c               @   s   e Zd ZdS )r   N)r0   r1   r2   r'   r'   r'   r)   r     s   c               @   s   e Zd ZdS )r   N)r0   r1   r2   r'   r'   r'   r)   r     s   Zdecimal_contextc              C   s2   yt  S  tk
r,   t } t |  | S X d S )N)_current_context_vargetLookupErrorr   set)r.   r'   r'   r)   r     s    
c             C   s,   | t ttfkr|  } |   t|  d S )N)r   r   r   copyclear_flagsr@   rC   )r.   r'   r'   r)   r     s    c             C   s   | d krt  } t| S )N)r   _ContextManager)Zctxr'   r'   r)   r     s    $ c            
   @   s  e Zd ZdZdddZedd Zdd	 Zd
d ZdddZ	dd Z
dd Zdd ZdddZdddZdddZdddZdddZdddZd d! Zd"d# Zd$d% Zd&d' Zdd)d*Zdd+d,Zdd-d.Zdd/d0Zdd2d3Zdd4d5ZeZdd6d7Zd d8d9Zdd:d;Z e Z!dd<d=Z"d>d? Z#dd@dAZ$ddBdCZ%ddDdEZ&ddFdGZ'ddHdIZ(ddJdKZ)d	dLdMZ*d
dNdOZ+dPdQ Z,dRdS Z-e-Z.e/dTdU Z0e/dVdW Z1dXdY Z2dZd[ Z3d\d] Z4d^d_ Z5d`da Z6dbdc Z7ddde Z8dfdg Z9dhdi Z:djdk Z;dldm Z<dndo Z=e>e6e7e8e9e:e;e<e=dpZ?ddqdrZ@dsdt ZAdudv ZBddwdxZCddydzZDd{d| ZEdd}d~ZFdddZGdddZHdddZIdddZJdd ZKdd ZLdddZMdddZNeNZOdddZPdddZQdddZRdd ZSdd ZTdd ZUdd ZVdddZWdddZXdddZYdd ZZdd Z[dddZ\dddZ]dd Z^dd Z_dd Z`dd ZadddZbdd Zcdd Zddd ZedddZfdd Zgdd ZhdddZiddĄ Zjd ddƄZkd!ddȄZlddʄ Zmdd̄ Znd"dd΄Zod#ddЄZpd$dd҄Zqd%ddԄZrd&ddքZsd'dd؄Ztd(ddڄZud)dd܄Zvd*ddބZwd+ddZxdd Zyd,ddZzd-ddZ{d.ddZ|dd Z}dd Z~dd Zd/ddZdS (0  r   )_expr6   r5   _is_special0Nc             C   s  t | }t|tr$t| dd}|d krP|d kr@t }|t	d| S |
ddkrfd|_nd|_|
d}|d k	r|
d	pd}t|
d
pd}tt|| |_|t| |_d|_nZ|
d}|d k	rtt|pdd|_|
drd|_nd|_nd|_d|_d|_|S t|trf|dkrBd|_nd|_d|_tt||_d|_|S t|tr|j|_|j|_|j|_|j|_|S t|tr|j|_t|j|_t|j|_d|_|S t|ttfr&t|dkrtdt|d tr|d dkstd|d |_|d dkrHd|_|d |_d|_ng }	x^|d D ]R}
t|
trd|
  kr~dkrn n|	s|
dkr|	|
 ntdqVW |d dkrdtt|	|_|d |_d|_nDt|d trdtt|	p dg|_|d |_d|_ntd|S t|tr||d krBt }|td t|}|j|_|j|_|j|_|j|_|S t d| d S )N_ zInvalid literal for Decimal: %rr;   -r,   r%   intZfracexprI   FdiagsignalNr3   FT   ztInvalid tuple size in creation of Decimal from list or tuple.  The list or tuple should have exactly three elements.)r%   r,   z|Invalid sign.  The first value in the tuple should be an integer; either 0 for a positive number or 1 for a negative number.   	   zTThe second value in the tuple must be composed of integers in the range 0 through 9.)r3   rQ   zUThe third value in the tuple must be an integer, or one of the strings 'F', 'n', 'N'.z;strict semantics for mixing floats and Decimals are enabledzCannot convert %r to Decimal)!object__new__
isinstancestr_parserstripreplacer   _raise_errorr   groupr5   rM   r6   lenrG   rH   lstripabsr   _WorkRepr;   rN   listtuple
ValueErrorappendjoinmapfloatr   
from_float	TypeError)clsvaluer.   r-   mintpartfracpartrN   rO   digitsZdigitr'   r'   r)   rW     s    







(



zDecimal.__new__c             C   s   t |tr,|dkrdnd}d}tt|}nzt |trt|sJt|rV| t|S t	d|dkrld}nd}t|
 \}}| d }t|d|  }ntdt||| }| tkr|S | |S d S )Nr%   r,   g      ?   zargument must be int or float.)rX   rM   rY   ra   ri   _mathZisinfZisnanreprZcopysignas_integer_ratio
bit_lengthrk   r4   r   )rl   fr;   kcoeffr3   dresultr'   r'   r)   rj     s$    

zDecimal.from_floatc             C   s(   | j r$| j}|dkrdS |dkr$dS dS )Nr3   r,   rQ   rT   r%   )rH   rG   )r-   rN   r'   r'   r)   _isnan  s    zDecimal._isnanc             C   s   | j dkr| jrdS dS dS )NrR   r,   r%   )rG   r5   )r-   r'   r'   r)   _isinfinity  s
    
zDecimal._isinfinityc             C   s|   |   }|d krd}n|  }|s&|rx|d kr4t }|dkrJ|td| S |dkr`|td|S |rn| |S ||S dS )NFrT   sNaNr%   )r|   r   r]   r	   r7   )r-   otherr.   self_is_nanother_is_nanr'   r'   r)   _check_nans  s"    


zDecimal._check_nansc             C   sv   |d krt  }| js|jrr|  r0|td| S | rF|td|S |  r\|td| S | rr|td|S dS )Nzcomparison involving sNaNzcomparison involving NaNr%   )r   rH   is_snanr]   r	   is_qnan)r-   r   r.   r'   r'   r)   _compare_check_nans  s(    zDecimal._compare_check_nansc             C   s   | j p| jdkS )NrI   )rH   r6   )r-   r'   r'   r)   __bool__&  s    zDecimal.__bool__c             C   s  | j s|j r8|  }| }||kr(dS ||k r4dS dS | sP|sDdS d|j  S |s^d| j S |j| jk rndS | j|jk r~dS |  }| }||kr| jd| j|j   }|jd|j| j   }||krdS ||k rd| j  S d| j S n ||krd| j S d| j  S d S )Nr%   r}   r,   rI   )rH   r~   r5   adjustedr6   rG   )r-   r   Zself_infZ	other_infself_adjustedZother_adjustedself_paddedZother_paddedr'   r'   r)   _cmp-  s>    


zDecimal._cmpc             C   s<   t | |dd\} }|tkr|S | ||r.dS | |dkS )NT)equality_opFr%   )_convert_for_comparisonNotImplementedr   r   )r-   r   r.   r'   r'   r)   __eq__m  s    zDecimal.__eq__c             C   s<   t | |\} }|tkr|S | ||}|r.dS | |dk S )NFr%   )r   r   r   r   )r-   r   r.   r9   r'   r'   r)   __lt__u  s    zDecimal.__lt__c             C   s<   t | |\} }|tkr|S | ||}|r.dS | |dkS )NFr%   )r   r   r   r   )r-   r   r.   r9   r'   r'   r)   __le__~  s    zDecimal.__le__c             C   s<   t | |\} }|tkr|S | ||}|r.dS | |dkS )NFr%   )r   r   r   r   )r-   r   r.   r9   r'   r'   r)   __gt__  s    zDecimal.__gt__c             C   s<   t | |\} }|tkr|S | ||}|r.dS | |dkS )NFr%   )r   r   r   r   )r-   r   r.   r9   r'   r'   r)   __ge__  s    zDecimal.__ge__c             C   s>   t |dd}| js|r0|jr0| ||}|r0|S t| |S )NT)raiseit)_convert_otherrH   r   r   r   )r-   r   r.   r9   r'   r'   r)   compare  s    zDecimal.comparec             C   s   | j r4|  rtdn|  r$tS | jr0t S tS | jdkrNtd| jt	}ntt
| j t	}t| j| t	 }| dkr||n| }|dkrdS |S )Nz"Cannot hash a signaling NaN value.r%   
   r}   )rH   r   rk   is_nan_PyHASH_NANr5   _PyHASH_INFrG   pow_PyHASH_MODULUS_PyHASH_10INVrM   r6   )r-   Zexp_hashZhash_r9   r'   r'   r)   __hash__  s    

zDecimal.__hash__c             C   s   t | jttt| j| jS )N)r   r5   rd   rh   rM   r6   rG   )r-   r'   r'   r)   as_tuple  s    zDecimal.as_tuplec             C   s   | j r |  rtdntd| s(dS t| j}| jdkrR|d| j  d }}nr| j }x(|dkr|d dkr|d }|d8 }q\W | j }t|| @  d |}|r||L }||8 }d| |> }| j	r| }||fS )Nz#cannot convert NaN to integer ratioz(cannot convert Infinity to integer ratio)r%   r,   r%   r   r,   rr   )
rH   r   re   OverflowErrorrM   r6   rG   minrv   r5   )r-   r3   rz   Zd5Zd2Zshift2r'   r'   r)   ru     s,    


zDecimal.as_integer_ratioc             C   s   dt |  S )NzDecimal('%s'))rY   )r-   r'   r'   r)   __repr__  s    zDecimal.__repr__Fc       	      C   sb  ddg| j  }| jrL| jdkr&|d S | jdkr>|d | j S |d | j S | jt| j }| jdkrt|d	krt|}n6|s~d
}n,| jdkr|d
 d d
 }n|d
 d d
 }|dkrd}dd|   | j }nL|t| jkr| jd|t| j   }d}n | jd | }d| j|d   }||kr(d}n*|d kr8t }ddg|j d||   }|| | | S )NrK   rL   rR   ZInfinityr3   NaNr   r%   ir,   rI   rS   .eEz%+d)r5   rH   rG   r6   r_   r   capitals)	r-   engr.   r;   
leftdigitsdotplacero   rp   rN   r'   r'   r)   __str__  s:    




zDecimal.__str__c             C   s   | j d|dS )NT)r   r.   )r   )r-   r.   r'   r'   r)   to_eng_string7  s    zDecimal.to_eng_stringc             C   sR   | j r| j|d}|r|S |d kr(t }| s@|jtkr@|  }n|  }||S )N)r.   )rH   r   r   r=   r   copy_abscopy_negate_fix)r-   r.   r9   r'   r'   r)   __neg__@  s    
zDecimal.__neg__c             C   sR   | j r| j|d}|r|S |d kr(t }| s@|jtkr@|  }nt| }||S )N)r.   )rH   r   r   r=   r   r   r   r   )r-   r.   r9   r'   r'   r)   __pos__V  s    
zDecimal.__pos__Tc             C   sJ   |s|   S | jr&| j|d}|r&|S | jr:| j|d}n| j|d}|S )N)r.   )r   rH   r   r5   r   r   )r-   roundr.   r9   r'   r'   r)   __abs__k  s    zDecimal.__abs__c       
      C   sh  t |}|tkr|S |d kr"t }| js.|jr| ||}|rB|S |  rr| j|jkrj| rj|tdS t	| S | rt	|S t
| j|j}d}|jtkr| j|jkrd}| s|st
| j|j}|rd}t|d|}||}|S | st||j|j d }|||j}||}|S |sVt|| j|j d }| ||j}||}|S t| }t|}t|||j\}}t }	|j|jkr|j|jkrt|d|}||}|S |j|jk r|| }}|jdkrd|	_|j|j |_|_nd|	_n&|jdkrd|	_d\|_|_nd|	_|jdkr<|j|j |	_n|j|j |	_|j|	_t	|	}||}|S )Nz
-INF + INFr%   r,   rI   )r%   r%   )r   r   r   rH   r   r~   r5   r]   r	   r   r   rG   r=   r   r4   r   maxr>   _rescalerb   
_normalizer;   rM   rN   )
r-   r   r.   r9   rN   Znegativezeror;   op1op2r{   r'   r'   r)   __add__  s|    





zDecimal.__add__c             C   sH   t |}|tkr|S | js |jr6| j||d}|r6|S | j| |dS )N)r.   )r   r   rH   r   r   r   )r-   r   r.   r9   r'   r'   r)   __sub__  s    zDecimal.__sub__c             C   s"   t |}|tkr|S |j| |dS )N)r.   )r   r   r   )r-   r   r.   r'   r'   r)   __rsub__  s    zDecimal.__rsub__c             C   s@  t |}|tkr|S |d kr"t }| j|jA }| js:|jr| ||}|rN|S |  rn|sf|tdS t	| S | r| s|tdS t	| S | j
|j
 }| r|st|d|}||}|S | jdkrt||j|}||}|S |jdkr
t|| j|}||}|S t| }t|}t|t|j|j |}||}|S )Nz(+-)INF * 0z0 * (+-)INFrI   1)r   r   r   r5   rH   r   r~   r]   r	   r:   rG   r4   r   r6   rb   rY   rM   )r-   r   r.   Z
resultsignr9   Z	resultexpr   r   r'   r'   r)   __mul__  sH    




zDecimal.__mul__c             C   s  t |}|tkrtS |d kr"t }| j|jA }| js:|jr| ||}|rN|S |  rj| rj|tdS |  rzt	| S | r|t
d t|d| S |s| s|tdS |td|S | s| j|j }d}nt|jt| j |j d }| j|j | }t| }t|}	|dkr:t|jd|  |	j\}}
nt|j|	jd|   \}}
|
rt|d	 dkr|d7 }n<| j|j }x.||k r|d dkr|d }|d7 }qW t|t||}||S )
Nz(+-)INF/(+-)INFzDivision by infinityrI   z0 / 0zx / 0r%   r,   r   rr   )r   r   r   r5   rH   r   r~   r]   r	   r:   r   r4   Etinyr   r
   rG   r_   r6   r>   rb   divmodrM   rY   r   )r-   r   r.   r;   r9   rN   ry   shiftr   r   	remainder	ideal_expr'   r'   r)   __truediv__(  sP    

zDecimal.__truediv__c             C   s  | j |j A }| r| j}nt| j|j}|  |  }| rN| sN|dkrht|dd| ||jfS ||jkr
t	| }t	|}|j
|j
kr| jd|j
|j
  9  _n| jd|j
|j
  9  _t|j|j\}}	|d|j k r
t|t|dt| j t|	|fS |td}
|
|
fS )Nr   rI   r%   r   z%quotient too large in //, % or divmod)r5   r~   rG   r   r   r4   r   r=   r>   rb   rN   rM   r   rY   r]   r   )r-   r   r.   r;   r   expdiffr   r   qrr9   r'   r'   r)   _dividec  s*    
zDecimal._dividec             C   s"   t |}|tkr|S |j| |dS )N)r.   )r   r   r   )r-   r   r.   r'   r'   r)   __rtruediv__  s    zDecimal.__rtruediv__c             C   s   t |}|tkr|S |d kr"t }| ||}|r:||fS | j|jA }|  r~| rj|td}||fS t| |tdfS |s| s|t	d}||fS |t
d||tdfS | ||\}}||}||fS )Nzdivmod(INF, INF)zINF % xzdivmod(0, 0)zx // 0zx % 0)r   r   r   r   r5   r~   r]   r	   r:   r   r
   r   r   )r-   r   r.   r9   r;   Zquotientr   r'   r'   r)   
__divmod__  s0    
zDecimal.__divmod__c             C   s"   t |}|tkr|S |j| |dS )N)r.   )r   r   r   )r-   r   r.   r'   r'   r)   __rdivmod__  s    zDecimal.__rdivmod__c             C   s   t |}|tkr|S |d kr"t }| ||}|r6|S |  rJ|tdS |sj| r^|tdS |tdS | ||d }|	|}|S )NzINF % xzx % 0z0 % 0r,   )
r   r   r   r   r~   r]   r	   r   r   r   )r-   r   r.   r9   r   r'   r'   r)   __mod__  s"    
zDecimal.__mod__c             C   s"   t |}|tkr|S |j| |dS )N)r.   )r   r   r   )r-   r   r.   r'   r'   r)   __rmod__  s    zDecimal.__rmod__c             C   s  |d krt  }t|dd}| ||}|r.|S |  rB|tdS |sb| rV|tdS |tdS | r|t| }||S t	| j
|j
}| st| jd|}||S |  |  }||jd kr|tS |dkr| ||j}||S t| }t|}|j|jkr(| jd	|j|j  9  _n| jd	|j|j  9  _t|j|j\}}	d
|	 |d@  |jkr~|	|j8 }	|d7 }|d	|j kr|tS | j}
|	dk rd|
 }
|	 }	t|
t|	|}||S )NT)r   zremainder_near(infinity, x)zremainder_near(x, 0)zremainder_near(0, 0)rI   r,   r   r   rT   r%   )r   r   r   r~   r]   r	   r   r   r   r   rG   r4   r5   r   r>   r   r   r=   rb   rN   rM   r   rY   )r-   r   r.   r9   ideal_exponentr   r   r   r   r   r;   r'   r'   r)   remainder_near  sZ    






zDecimal.remainder_nearc             C   s   t |}|tkr|S |d kr"t }| ||}|r6|S |  rb| rR|tdS t| j|jA  S |s| r|t	d| j|jA S |t
dS | ||d S )Nz
INF // INFzx // 0z0 // 0r%   )r   r   r   r   r~   r]   r	   r:   r5   r
   r   r   )r-   r   r.   r9   r'   r'   r)   __floordiv__#  s$    zDecimal.__floordiv__c             C   s"   t |}|tkr|S |j| |dS )N)r.   )r   r   r   )r-   r   r.   r'   r'   r)   __rfloordiv__?  s    zDecimal.__rfloordiv__c             C   s8   |   r(|  rtd| jr"dnd}nt| }t|S )Nz%Cannot convert signaling NaN to floatz-nannan)r|   r   re   r5   rY   ri   )r-   sr'   r'   r)   	__float__F  s    zDecimal.__float__c             C   st   | j r(|  rtdn|  r(tdd| j }| jdkrT|t| j d| j  S |t| jd | j pjd S d S )NzCannot convert NaN to integerz"Cannot convert infinity to integerr}   r%   r   rI   )	rH   r|   re   r~   r   r5   rG   rM   r6   )r-   r   r'   r'   r)   __int__P  s    


zDecimal.__int__c             C   s   | S )Nr'   )r-   r'   r'   r)   real_  s    zDecimal.realc             C   s   t dS )Nr%   )r   )r-   r'   r'   r)   imagc  s    zDecimal.imagc             C   s   | S )Nr'   )r-   r'   r'   r)   	conjugateg  s    zDecimal.conjugatec             C   s   t t| S )N)complexri   )r-   r'   r'   r)   __complex__j  s    zDecimal.__complex__c             C   sR   | j }|j|j }t||krJ|t|| d  d}t| j|| jdS t| S )NrI   T)	r6   r>   clampr_   r`   r4   r5   rG   r   )r-   r.   ZpayloadZmax_payload_lenr'   r'   r)   r7   m  s    zDecimal._fix_nanc             C   sX  | j r |  r| |S t| S | }| }| s|j|g|j }tt	| j
||}|| j
krx|t t| jd|S t| S t| j| j
 |j }||kr|td| j}|t |t |S ||k }|r|}| j
|k rt| j| j
 | }	|	dk rt| jd|d } d}	| j|j }
|
| |	}| jd |	 p>d}|dkr~tt|d }t||jkr~|d d }|d7 }||kr|td| j}nt| j||}|r|r|t |r|t |r|t |t |s|t |S |r|t |jdkrP| j
|krP|t | jd| j
|   }t| j||S t| S )NrI   z
above Emaxr%   r   r,   r}   )rH   r|   r7   r   r   Etopr?   r   r   r   rG   r]   r   r4   r5   r_   r6   r>   r   r   r   _pick_rounding_functionr=   rY   rM   r   r   )r-   r.   r   r   exp_maxZnew_expZexp_minr9   Zself_is_subnormalrq   Zrounding_methodchangedry   r   r'   r'   r)   r   y  sn    
















zDecimal._fixc             C   s   t | j|rdS dS d S )Nr%   r}   )
_all_zerosr6   )r-   r>   r'   r'   r)   _round_down  s    zDecimal._round_downc             C   s   |  | S )N)r   )r-   r>   r'   r'   r)   	_round_up  s    zDecimal._round_upc             C   s*   | j | dkrdS t| j |r"dS dS d S )NZ56789r,   r%   r}   )r6   r   )r-   r>   r'   r'   r)   _round_half_up  s
    zDecimal._round_half_upc             C   s   t | j|rdS | |S d S )Nr}   )_exact_halfr6   r   )r-   r>   r'   r'   r)   _round_half_down  s    zDecimal._round_half_downc             C   s8   t | j|r*|dks&| j|d  dkr*dS | |S d S )Nr%   r,   02468r}   )r   r6   r   )r-   r>   r'   r'   r)   _round_half_even  s    zDecimal._round_half_evenc             C   s    | j r| |S | | S d S )N)r5   r   )r-   r>   r'   r'   r)   _round_ceiling  s    
zDecimal._round_ceilingc             C   s    | j s| |S | | S d S )N)r5   r   )r-   r>   r'   r'   r)   _round_floor	  s    
zDecimal._round_floorc             C   s0   |r | j |d  dkr | |S | | S d S )Nr,   Z05)r6   r   )r-   r>   r'   r'   r)   _round_05up  s    
zDecimal._round_05up)r   r   r   r   r   r   r   r   c             C   sb   |d k	r2t |tstdtdd| }| |S | jrR|  rJtdntdt| 	dt
S )Nz+Second argument to round should be integralr%   r   zcannot round a NaNzcannot round an infinity)rX   rM   rk   r4   quantizerH   r   re   r   r   r   )r-   r3   rN   r'   r'   r)   	__round__"  s    /


zDecimal.__round__c             C   s0   | j r |  rtdntdt| dtS )Nzcannot round a NaNzcannot round an infinityr%   )rH   r   re   r   rM   r   r   )r-   r'   r'   r)   	__floor__`  s
    
zDecimal.__floor__c             C   s0   | j r |  rtdntdt| dtS )Nzcannot round a NaNzcannot round an infinityr%   )rH   r   re   r   rM   r   r   )r-   r'   r'   r)   __ceil__o  s
    
zDecimal.__ceil__c             C   s  t |dd}t |dd}| js$|jr|d kr2t }| jdkrJ|td| S |jdkrb|td|S | jdkrr| }nf|jdkr|}nV| jdkr|s|tdS t| j|jA  }n*|jdkr| s|tdS t| j|jA  }n0t| j|jA t	t
| jt
|j | j|j }|||S )	NT)r   rQ   r   r3   rR   zINF * 0 in fmaz0 * INF in fma)r   rH   r   rG   r]   r	   r:   r5   r4   rY   rM   r6   r   )r-   r   Zthirdr.   productr'   r'   r)   fma~  s6    





zDecimal.fmac             C   s  t |}|tkr|S t |}|tkr(|S |d kr6t }|  }| }| }|sZ|sZ|r|dkrp|td| S |dkr|td|S |dkr|td|S |r| |S |r||S ||S |  r| r| s|tdS |dk r|tdS |s|tdS | |j	kr(|tdS |s@| s@|tdS |
 rPd}n| j}tt|}t|  }t| }	|j| td	|j| | }x t|	jD ]}
t|d	|}qW t||	j|}t|t|dS )
NrT   r   z@pow() 3rd argument not allowed unless all arguments are integersr%   zApow() 2nd argument cannot be negative when 3rd argument specifiedzpow() 3rd argument cannot be 0zSinsufficient precision: pow() 3rd argument must not have more than precision digitszXat least one of pow() 1st argument and 2nd argument must be nonzero; 0**0 is not definedr   )r   r   r   r|   r]   r	   r7   
_isintegerr   r>   _isevenr5   ra   rM   rb   to_integral_valuer   rN   ranger4   rY   )r-   r   modulor.   r   r   Zmodulo_is_nanr;   baseexponentir'   r'   r)   _power_modulo  sl    



zDecimal._power_moduloc             C   s  t | }|j|j }}x |d dkr6|d }|d7 }qW t |}|j|j }}x |d dkrn|d }|d7 }qPW |dkr ||9 }x |d dkr|d }|d7 }qW |dk rd S |d|  }	|jdkr|	 }	| r|jdkr| jt| }
t|	|
 |d }nd}tddd|  |	| S |jdkr|d }|dkr|| @ |krRd S t	|d }|d d }|t
t|krd S t|| |}t|| |}|d ks|d krd S ||krd S d	| }n|d	krt	|d
 d }td	| |\}}|rd S x$|d	 dkr&|d	 }|d8 }qW |d d }|t
t|krJd S t|| |}t|| |}|d ksz|d kr~d S ||krd S d| }nd S |d| krd S | | }tdt||S |dkr|d|  d }}n|dkrt
tt|| | krd S t	|}|dkr@t
tt|| | kr@d S |d|   }}x:|d |d   krrdkrn n|d }|d }qRW x:|d	 |d	   krdkrn n|d	 }|d	 }qW |dkrv|dkr||krd S t||\}}|dkrd S dt	| |  > }x>t|||d  \}}||kr@P n||d  | | }qW ||krn|dksrd S |}|dkr||d t| krd S || }||9 }|d| krd S t|}| r |jdkr | jt| }
t||
 |t
| }nd}td|d|  || S )Nr   r%   r,   r   rI   )rT            ]   A   rr      rS   rT   d   )rb   rM   rN   r;   r   r5   rG   r   r4   _nbitsr_   rY   _decimal_lshift_exactr   ra   	_log10_lb)r-   r   pxxcxeyycyer   r   ZzerosZ
last_digitr   Zemaxr   rn   r3   Zxc_bitsremar   r   Zstr_xcr'   r'   r)   _power_exact  s    :









&&&&


 zDecimal._power_exactc             C   s>  |d k	r|  |||S t|}|tkr*|S |d kr8t }| ||}|rL|S |sd| s`|tdS tS d}| jdkr|	 r|
 sd}n| r|tdS |  } | s|jdkrt|ddS t| S |  r|jdkrt| S t|ddS | tkr|	 rZ|jdkrd}n||jkr"|j}nt|}| j| }|d|j k rxd|j }|t n|t |t d|j }t|dd|   |S |  }| r|jdk|dk krt|ddS t| S d }d}	|  |  }
|dk|jdkkr|
tt|jkrHt|d|jd }n,| }|
tt| krHt|d|d }|d kr| ||jd }|d k	r|dkrtd|j|j}d}	|d kr:|j}t| }|j|j }}t|}|j|j }}|jdkr| }d	}xJt|||||| \}}|d
dtt|| d    rP |d	7 }qW t|t||}|	r0|	 s0t|j|jkr|jd t|j }t|j|jd|  |j| }|  }|!  xt"D ]}d|j#|< qW |$|}|t |j%t& r|t' |j%t( r|t(d|j x:t't&ttt)fD ]}|j%| r|| qW n
|$|}|S )Nz0 ** 0r%   r,   z+x ** y with x negative and y not an integerrI   r   FTrS   rr   r   z
above Emax)*r   r   r   r   r   r]   r	   _Oner5   r   r   r   r4   r:   r~   r>   rM   rG   r   r   r   _log10_exp_boundr_   rY   r?   r   r  r6   rb   rN   r;   _dpowerrD   rE   _signalstrapsr   flagsr   r   r   r   )r-   r   r   r.   r9   Zresult_signZ
multiplierrN   Zself_adjexactZboundr   r  r  r  r  r  r	  r
  extrary   r   Z
newcontextZ	exceptionr'   r'   r)   __pow__  s    














"




zDecimal.__pow__c             C   s"   t |}|tkr|S |j| |dS )N)r.   )r   r   r  )r-   r   r.   r'   r'   r)   __rpow__	  s    zDecimal.__rpow__c             C   s   |d krt  }| jr(| j|d}|r(|S | |}| r>|S |sPt|jddS |j| g|j	 }t
|j}|j}x.|j|d  dkr||k r|d7 }|d8 }qvW t|j|jd | |S )N)r.   rI   r%   r,   )r   rH   r   r   r~   r4   r5   r?   r   r   r_   r6   rG   )r-   r.   r9   dupr   endrN   r'   r'   r)   	normalize	  s$    

zDecimal.normalizec             C   s  t |dd}|d krt }|d kr(|j}| js4|jr|| ||}|rH|S | sX|  r|| rp|  rpt| S |tdS |	 |j
  kr|jksn |tdS | st| jd|j
}||S |  }||jkr|tdS ||j
 d |jkr|tdS | |j
|}| |jkr.|tdS t|j|jkrL|tdS |rl| |jk rl|t |j
| j
kr|| kr|t |t ||}|S )	NT)r   zquantize with one INFz)target exponent out of bounds in quantizerI   z9exponent of quantize result too large for current contextr,   z7quantize result has too many digits for current context)r   r   r=   rH   r   r~   r   r]   r	   r   rG   r?   r4   r5   r   r   r>   r   r_   r6   Eminr   r   r   )r-   rN   r=   r.   r9   r   r'   r'   r)   r   	  sT    






zDecimal.quantizec             C   sD   t |dd}| js|jr8|  r(| p6|  o6| S | j|jkS )NT)r   )r   rH   r   is_infiniterG   )r-   r   r.   r'   r'   r)   same_quantum!
  s
    	zDecimal.same_quantumc             C   s   | j rt| S | s t| jd|S | j|krHt| j| jd| j|   |S t| j| j | }|dk rzt| jd|d } d}| j| }|| |}| jd | pd}|dkrtt	|d }t| j||S )NrI   r%   r   r,   )
rH   r   r4   r5   rG   r6   r_   r   rY   rM   )r-   rN   r=   rq   Zthis_functionr   ry   r'   r'   r)   r   0
  s"    


zDecimal._rescalec             C   sf   |dkrt d| js| s"t| S | |  d | |}| |  krb|| d | |}|S )Nr%   z'argument should be at least 1 in _roundr,   )re   rH   r   r   r   )r-   placesr=   r9   r'   r'   r)   _roundR
  s    

zDecimal._roundc             C   s   | j r"| j|d}|r|S t| S | jdkr4t| S | sFt| jddS |d krTt }|d krb|j}| d|}|| kr|	t
 |	t |S )N)r.   r%   rI   )rH   r   r   rG   r4   r5   r   r=   r   r]   r   r   )r-   r=   r.   r9   r'   r'   r)   to_integral_exacti
  s$    



zDecimal.to_integral_exactc             C   s`   |d krt  }|d kr|j}| jr>| j|d}|r6|S t| S | jdkrPt| S | d|S d S )N)r.   r%   )r   r=   rH   r   r   rG   r   )r-   r=   r.   r9   r'   r'   r)   r   
  s    
zDecimal.to_integral_valuec             C   s  |d krt  }| jrB| j|d}|r(|S |  rB| jdkrBt| S | sdt| jd| jd }||S | jdkrz|	t
dS |jd }t| }|jd? }|jd@ r|jd }t| jd? d }n|j}t| jd d? }|| }|dkr|d| 9 }d	}	nt|d|  \}}
|
 }	||8 }d| }x(|| }||kr:P n|| d? }q$W |	o\|| |k}	|	r|dkr||d|  }n|d|  9 }||7 }n|d
 dkr|d7 }tdt||}| }|t}||}||_|S )N)r.   r%   rI   rT   r,   zsqrt(-x), x > 0r   r   Trr   )r   rH   r   r~   r5   r   r4   rG   r   r]   r	   r>   rb   rN   rM   r_   r6   r   rY   _shallow_copy_set_roundingr   r=   )r-   r.   r9   r>   opr   clr   r  r   r3   r   r=   r'   r'   r)   sqrt
  s`    










zDecimal.sqrtc             C   s   t |dd}|d krt }| js&|jr~|  }| }|s>|r~|dkrX|dkrX| |S |dkrr|dkrr||S | ||S | |}|dkr| |}|dkr|}n| }||S )NT)r   r,   r%   r}   )r   r   rH   r|   r   r   r   compare_total)r-   r   r.   snonr$  r9   r'   r'   r)   r   
  s&    


	
zDecimal.maxc             C   s   t |dd}|d krt }| js&|jr~|  }| }|s>|r~|dkrX|dkrX| |S |dkrr|dkrr||S | ||S | |}|dkr| |}|dkr| }n|}||S )NT)r   r,   r%   r}   )r   r   rH   r|   r   r   r   r'  )r-   r   r.   r(  r)  r$  r9   r'   r'   r)   r   &  s&    



zDecimal.minc             C   s8   | j r
dS | jdkrdS | j| jd  }|dt| kS )NFr%   TrI   )rH   rG   r6   r_   )r-   restr'   r'   r)   r   H  s    
zDecimal._isintegerc             C   s&   | r| j dkrdS | jd| j   dkS )Nr%   Tr}   r   )rG   r6   )r-   r'   r'   r)   r   Q  s    zDecimal._isevenc             C   s.   y| j t| j d S  tk
r(   dS X d S )Nr,   r%   )rG   r_   r6   rk   )r-   r'   r'   r)   r   W  s    zDecimal.adjustedc             C   s   | S )Nr'   )r-   r'   r'   r)   	canonical_  s    zDecimal.canonicalc             C   s.   t |dd}| ||}|r |S | j||dS )NT)r   )r.   )r   r   r   )r-   r   r.   r9   r'   r'   r)   compare_signalg  s
    zDecimal.compare_signalc             C   s`  t |dd}| jr|jstS | js,|jr,tS | j}|  }| }|sL|r||krt| j| jf}t|j|jf}||k r|rtS tS ||kr|rtS tS tS |r|dkrtS |dkrtS |dkrtS |dkrtS n2|dkrtS |dkrtS |dkrtS |dkrtS | |k rtS | |kr$tS | j|jk r@|r<tS tS | j|jkr\|rXtS tS tS )NT)r   r,   rT   )	r   r5   _NegativeOner  r|   r_   r6   _ZerorG   )r-   r   r.   r;   Zself_nanZ	other_nanZself_keyZ	other_keyr'   r'   r)   r'  s  sf    



zDecimal.compare_totalc             C   s&   t |dd}|  }| }||S )NT)r   )r   r   r'  )r-   r   r.   r   or'   r'   r)   compare_total_mag  s    zDecimal.compare_total_magc             C   s   t d| j| j| jS )Nr%   )r4   r6   rG   rH   )r-   r'   r'   r)   r     s    zDecimal.copy_absc             C   s2   | j rtd| j| j| jS td| j| j| jS d S )Nr%   r,   )r5   r4   r6   rG   rH   )r-   r'   r'   r)   r     s    zDecimal.copy_negatec             C   s"   t |dd}t|j| j| j| jS )NT)r   )r   r4   r5   r6   rG   rH   )r-   r   r.   r'   r'   r)   	copy_sign  s    
zDecimal.copy_signc             C   s  |d krt  }| j|d}|r"|S |  dkr2tS | s:tS |  dkrNt| S |j}|  }| jdkr|t	t
|jd d krtdd|jd }n2| jdkr|t	t
|  d d krtdd| d }n| jdkr|| k rtddd|d   d | }n| jdkrD|| d k rDtdd|d  | d }nt| }|j|j }}|jdkrl| }d}xFt|||| \}	}
|	d	d
t	t
|	| d    rP |d7 }qrW tdt
|	|
}| }|t}||}||_|S )N)r.   r}   r,   r%   rS   r   rI   r<   rr   r   )r   r   r~   r.  r  r   r>   r   r5   r_   rY   r?   r4   r   rb   rM   rN   r;   _dexpr!  r"  r   r   r=   )r-   r.   r9   r  adjr#  r$  r   r  ry   rN   r=   r'   r'   r)   rN     sJ    $( "

zDecimal.expc             C   s   dS )NTr'   )r-   r'   r'   r)   is_canonical#  s    zDecimal.is_canonicalc             C   s   | j  S )N)rH   )r-   r'   r'   r)   	is_finite+  s    zDecimal.is_finitec             C   s
   | j dkS )NrR   )rG   )r-   r'   r'   r)   r  3  s    zDecimal.is_infinitec             C   s
   | j dkS )N)r3   rQ   )rG   )r-   r'   r'   r)   r   7  s    zDecimal.is_nanc             C   s*   | j s
| sdS |d krt }|j|  kS )NF)rH   r   r  r   )r-   r.   r'   r'   r)   	is_normal;  s
    
zDecimal.is_normalc             C   s
   | j dkS )Nr3   )rG   )r-   r'   r'   r)   r   C  s    zDecimal.is_qnanc             C   s
   | j dkS )Nr,   )r5   )r-   r'   r'   r)   	is_signedG  s    zDecimal.is_signedc             C   s
   | j dkS )NrQ   )rG   )r-   r'   r'   r)   r   K  s    zDecimal.is_snanc             C   s*   | j s
| sdS |d krt }|  |jk S )NF)rH   r   r   r  )r-   r.   r'   r'   r)   is_subnormalO  s
    
zDecimal.is_subnormalc             C   s   | j  o| jdkS )NrI   )rH   r6   )r-   r'   r'   r)   is_zeroW  s    zDecimal.is_zeroc             C   s   | j t| j d }|dkr4tt|d d d S |dkrXttd| d d d S t| }|j|j }}|dkrt|d|   }t|}t|t| ||k  S |ttd|  |  d S )Nr,      r   r   r}   r%   )rG   r_   r6   rY   rb   rM   rN   )r-   r3  r#  r$  r   numdenr'   r'   r)   _ln_exp_bound[  s    zDecimal._ln_exp_boundc       
      C   s  |d krt  }| j|d}|r"|S | s*tS |  dkr:tS | tkrFtS | jdkr\|t	dS t
| }|j|j }}|j}||   d }x>t|||}|ddttt|| d    rP |d7 }qW tt|dk tt|| }| }|t}	||}|	|_|S )	N)r.   r,   zln of a negative valuerT   rr   r   rS   r%   )r   r   _NegativeInfinityr~   	_Infinityr  r.  r5   r]   r	   rb   rM   rN   r>   r=  _dlogr_   rY   ra   r4   r!  r"  r   r   r=   )
r-   r.   r9   r#  r$  r   r  r  ry   r=   r'   r'   r)   lnt  s:    
$

z
Decimal.lnc             C   s   | j t| j d }|dkr,tt|d S |dkrHttd| d S t| }|j|j }}|dkrt|d|   }td| }t|t| ||k  d S td|  | }t|| |dk  d S )	Nr,   r   r}   r%   r      rT   Z231)rG   r_   r6   rY   rb   rM   rN   )r-   r3  r#  r$  r   r;  r<  r'   r'   r)   r    s    zDecimal._log10_exp_boundc       
      C   sH  |d krt  }| j|d}|r"|S | s*tS |  dkr:tS | jdkrP|tdS | jd dkr| jdd  dt	| jd  krt
| jt	| j d }nt| }|j|j }}|j}||   d }x>t|||}|dd	t	tt|| d    rP |d
7 }qW tt|dk tt|| }| }|t}	||}|	|_|S )N)r.   r,   zlog10 of a negative valuer%   r   rI   rT   rr   r   rS   )r   r   r>  r~   r?  r5   r]   r	   r6   r_   r   rG   rb   rM   rN   r>   r  _dlog10rY   ra   r4   r!  r"  r   r   r=   )
r-   r.   r9   r#  r$  r   r  r  ry   r=   r'   r'   r)   log10  s:    
.$

zDecimal.log10c             C   sV   | j |d}|r|S |d kr"t }|  r.tS | s@|tddS t|  }||S )N)r.   zlogb(0)r,   )	r   r   r~   r?  r]   r
   r   r   r   )r-   r.   r9   r'   r'   r)   logb  s    	zDecimal.logbc             C   s8   | j dks| jdkrdS x| jD ]}|dkr dS q W dS )Nr%   FZ01T)r5   rG   r6   )r-   digr'   r'   r)   
_islogical  s    zDecimal._islogicalc             C   s   |j t| }|dkr$d| | }n|dk r<||j  d  }|j t| }|dkr`d| | }n|dk rx||j  d  }||fS )Nr%   rI   )r>   r_   )r-   r.   opaopbZdifr'   r'   r)   _fill_logical#  s    zDecimal._fill_logicalc             C   sz   |d krt  }t|dd}|  r*| s4|tS | || j|j\}}ddd t||D }t	d|
dptddS )NT)r   rK   c             S   s$   g | ]\}}t t|t|@ qS r'   )rY   rM   ).0r  br'   r'   r)   
<listcomp>>  s    z'Decimal.logical_and.<locals>.<listcomp>r%   rI   )r   r   rG  r]   r	   rJ  r6   rg   zipr4   r`   )r-   r   r.   rH  rI  r{   r'   r'   r)   logical_and0  s    
zDecimal.logical_andc             C   s(   |d krt  }| tdd|j d|S )Nr%   r   )r   logical_xorr4   r>   )r-   r.   r'   r'   r)   logical_invertA  s    zDecimal.logical_invertc             C   sz   |d krt  }t|dd}|  r*| s4|tS | || j|j\}}ddd t||D }t	d|
dptddS )NT)r   rK   c             S   s$   g | ]\}}t t|t|B qS r'   )rY   rM   )rK  r  rL  r'   r'   r)   rM  V  s    z&Decimal.logical_or.<locals>.<listcomp>r%   rI   )r   r   rG  r]   r	   rJ  r6   rg   rN  r4   r`   )r-   r   r.   rH  rI  r{   r'   r'   r)   
logical_orH  s    
zDecimal.logical_orc             C   sz   |d krt  }t|dd}|  r*| s4|tS | || j|j\}}ddd t||D }t	d|
dptddS )NT)r   rK   c             S   s$   g | ]\}}t t|t|A qS r'   )rY   rM   )rK  r  rL  r'   r'   r)   rM  g  s    z'Decimal.logical_xor.<locals>.<listcomp>r%   rI   )r   r   rG  r]   r	   rJ  r6   rg   rN  r4   r`   )r-   r   r.   rH  rI  r{   r'   r'   r)   rP  Y  s    
zDecimal.logical_xorc             C   s   t |dd}|d krt }| js&|jr~|  }| }|s>|r~|dkrX|dkrX| |S |dkrr|dkrr||S | ||S |  | }|dkr| |}|dkr|}n| }||S )NT)r   r,   r%   r}   )	r   r   rH   r|   r   r   r   r   r'  )r-   r   r.   r(  r)  r$  r9   r'   r'   r)   max_magj  s&    


zDecimal.max_magc             C   s   t |dd}|d krt }| js&|jr~|  }| }|s>|r~|dkrX|dkrX| |S |dkrr|dkrr||S | ||S |  | }|dkr| |}|dkr| }n|}||S )NT)r   r,   r%   r}   )	r   r   rH   r|   r   r   r   r   r'  )r-   r   r.   r(  r)  r$  r9   r'   r'   r)   min_mag  s&    


zDecimal.min_magc             C   s   |d krt  }| j|d}|r"|S |  dkr2tS |  dkrTtdd|j | S | }|t	 |
  | |}|| kr|S | tdd| d |S )N)r.   r}   r,   r%   r<   r   )r   r   r~   r>  r4   r>   r   rD   r"  r   _ignore_all_flagsr   r   r   )r-   r.   r9   new_selfr'   r'   r)   
next_minus  s"    

zDecimal.next_minusc             C   s   |d krt  }| j|d}|r"|S |  dkr2tS |  dkrTtdd|j | S | }|t	 |
  | |}|| kr|S | tdd| d |S )N)r.   r,   r}   r<   r%   r   )r   r   r~   r?  r4   r>   r   rD   r"  r   rU  r   r   r   )r-   r.   r9   rV  r'   r'   r)   	next_plus  s"    

zDecimal.next_plusc             C   s   t |dd}|d krt }| ||}|r.|S | |}|dkrJ| |S |dkr^| |}n
| |}| r|t	d|j
 |t |t nD| |jk r|t |t |t |t |s|t |S )NT)r   r%   r}   z Infinite result from next_toward)r   r   r   r   r1  rX  rW  r~   r]   r   r5   r   r   r   r  r   r   r   )r-   r   r.   r9   Z
comparisonr'   r'   r)   next_toward  s4    	








zDecimal.next_towardc             C   s   |   rdS |  rdS |  }|dkr,dS |dkr8dS |  rN| jrJdS dS |d kr\t }| j|d	rv| jrrd
S dS | jrdS dS d S )Nr   r   r,   z	+Infinityr}   z	-Infinityz-Zeroz+Zero)r.   z
-Subnormalz
+Subnormalz-Normalz+Normal)r   r   r~   r9  r5   r   r8  )r-   r.   infr'   r'   r)   number_class  s,    zDecimal.number_classc             C   s   t dS )Nr   )r   )r-   r'   r'   r)   radix,  s    zDecimal.radixc             C   s   |d krt  }t|dd}| ||}|r.|S |jdkrB|tS |j t|  kr`|jksln |tS |  r|t	| S t|}| j
}|jt| }|dkrd| | }n|dk r|| d  }||d  |d |  }t| j|dpd| jS )NT)r   r%   rI   )r   r   r   rG   r]   r	   r>   rM   r~   r   r6   r_   r4   r5   r`   )r-   r   r.   r9   torotrotdigtopadZrotatedr'   r'   r)   rotate0  s,    

 
zDecimal.rotatec             C   s   |d krt  }t|dd}| ||}|r.|S |jdkrB|tS d|j|j  }d|j|j  }|t|  krz|ksn |tS | 	 rt
| S t| j| j| jt| }||}|S )NT)r   r%   r   rT   )r   r   r   rG   r]   r	   r?   r>   rM   r~   r   r4   r5   r6   r   )r-   r   r.   r9   ZliminfZlimsuprz   r'   r'   r)   scalebQ  s"    



zDecimal.scalebc             C   s  |d krt  }t|dd}| ||}|r.|S |jdkrB|tS |j t|  kr`|jksln |tS |  r|t	| S t|}| j
}|jt| }|dkrd| | }n|dk r|| d  }|dk r|d | }n|d|  }||j d  }t| j|dp
d| jS )NT)r   r%   rI   )r   r   r   rG   r]   r	   r>   rM   r~   r   r6   r_   r4   r5   r`   )r-   r   r.   r9   r]  r^  r_  Zshiftedr'   r'   r)   r   j  s2    

 
zDecimal.shiftc             C   s   | j t| ffS )N)	__class__rY   )r-   r'   r'   r)   
__reduce__  s    zDecimal.__reduce__c             C   s   t | tkr| S | t| S )N)typer   rb  rY   )r-   r'   r'   r)   __copy__  s    zDecimal.__copy__c             C   s   t | tkr| S | t| S )N)rd  r   rb  rY   )r-   Zmemor'   r'   r)   __deepcopy__  s    zDecimal.__deepcopy__c             C   sJ  |d krt  }t||d}| jrXt| j|}t|  }|d dkrL|d7 }t|||S |d d krvddg|j |d< |d dkrt	| j| j
| jd } |j}|d }|d k	r|d dkr| |d	 |} nF|d d
kr| | |} n*|d dkrt| j
|kr| ||} | s@| jdkr@|d d
kr@| d|} | jt| j
 }	|d dkr~| sx|d k	rxd	| }
nd	}
nB|d d
kr|	}
n.|d dkr| jdkr|	dkr|	}
nd	}
|
dk rd}d|
  | j
 }nP|
t| j
kr| j
d|
t| j
   }d}n"| j
d |
 p d}| j
|
d  }|	|
 }t| j||||S )N)_localeconvrd  %gGrT   	precisioneEr,   zfF%ZgGr%   irI   rK   )r   _parse_format_specifierrH   _format_signr5   rY   r   _format_alignr   r4   r6   rG   r=   r  r   r_   _format_number)r-   Z	specifierr.   rg  specr;   bodyr=   rk  r   r   ro   rp   rN   r'   r'   r)   
__format__  sZ    
 

zDecimal.__format__)rI   N)NN)N)N)N)N)N)N)FN)N)N)N)TN)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)NN)N)N)NN)N)NN)NN)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)NN)r0   r1   r2   	__slots__rW   classmethodrj   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   ru   r   r   r   r   r   r   r   __radd__r   r   r   __rmul__r   r   r   r   r   r   r   r   r   r   r   r   	__trunc__propertyr   r   r   r   r7   r   r   r   r   r   r   r   r   r   dictr   r   r   r   r   r   r  r  r  r  r   r  r   r  r   r   to_integralr&  r   r   r   r   r   r+  r,  r'  r0  r   r   r1  rN   r4  r5  r  r   r6  r   r7  r   r8  r9  r=  rA  r  rD  rE  rG  rJ  rO  rQ  rR  rP  rS  rT  rW  rX  rY  r[  r\  r`  ra  r   rc  re  rf  rs  r'   r'   r'   r)   r     s  
 -
 !@

	
	
	
	
2
4
	



V
7;!$K
f	>,U n Y="c*"	IK23.*!'Fc             C   s&   t t}| |_||_||_||_|S )N)rV   rW   r   r5   r6   rG   rH   )r;   Zcoefficientr   Zspecialr-   r'   r'   r)   r4     s    
r4   c               @   s$   e Zd Zdd Zdd Zdd ZdS )rF   c             C   s   |  | _d S )N)rD   new_context)r-   r|  r'   r'   r)   __init__  s    z_ContextManager.__init__c             C   s   t  | _t| j | jS )N)r   saved_contextr   r|  )r-   r'   r'   r)   	__enter__  s    
z_ContextManager.__enter__c             C   s   t | j d S )N)r   r~  )r-   tvtbr'   r'   r)   __exit__  s    z_ContextManager.__exit__N)r0   r1   r2   r}  r  r  r'   r'   r'   r)   rF   
  s   rF   c            	   @   s  e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd ZeZdddZdd Zdd Zdd ZdZd d! Zd"d# Zd$d% Zdd'd(Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%dEdF Z&dGdH Z'dIdJ Z(dKdL Z)dMdN Z*dOdP Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]d^ Z2d_d` Z3dadb Z4dcdd Z5dedf Z6dgdh Z7didj Z8dkdl Z9dmdn Z:dodp Z;dqdr Z<dsdt Z=dudv Z>dwdx Z?dydz Z@d{d| ZAd}d~ ZBdd ZCdd ZDdd ZEdd ZFdddZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdd ZUeUZVdS )r   Nc
                s>  yt }
W n tk
r   Y nX |d k	r*|n|
j| _|d k	r>|n|
j| _|d k	rR|n|
j| _|d k	rf|n|
j| _|d k	rz|n|
j| _|d k	r|n|
j| _|	d krg | _n|	| _d kr|
j	
 | _	n.ttstfddt D | _	n| _	 d kr
ttd| _n0t ts4t fddt  D | _n | _d S )Nc             3   s   | ]}|t | kfV  qd S )N)rM   )rK  r   )r  r'   r)   	<genexpr>E  s    z#Context.__init__.<locals>.<genexpr>r%   c             3   s   | ]}|t | kfV  qd S )N)rM   )rK  r   )r  r'   r)   r  L  s    )r   	NameErrorr>   r=   r  r?   r   r   _ignored_flagsr  rD   rX   rz  r  fromkeysr  )r-   r>   r=   r  r?   r   r   r  r  r  Zdcr'   )r  r  r)   r}  ,  s.    

zContext.__init__c             C   s   t |tstd| |dkr<||krtd||||f nJ|dkrb||k rtd||||f n$||k sr||krtd||||f t| ||S )Nz%s must be an integerz-infz%s must be in [%s, %d]. got: %srZ  z%s must be in [%d, %s]. got: %sz%s must be in [%d, %d]. got %s)rX   rM   rk   re   rV   __setattr__)r-   namerm   ZvminZvmaxr'   r'   r)   _set_integer_checkP  s    
zContext._set_integer_checkc             C   sh   t |tstd| x |D ]}|tkrtd| qW x tD ]}||kr>td| q>W t| ||S )Nz%s must be a signal dictz%s is not a valid signal dict)rX   rz  rk   r  KeyErrorrV   r  )r-   r  rz   keyr'   r'   r)   _set_signal_dict^  s    


zContext._set_signal_dictc             C   s   |dkr|  ||ddS |dkr0|  ||ddS |dkrH|  ||ddS |dkr`|  ||ddS |d	krx|  ||ddS |d
kr|tkrtd| t| ||S |dks|dkr| ||S |dkrt| ||S td| d S )Nr>   r,   rZ  r  z-infr%   r?   r   r   r=   z%s: invalid rounding moder  r  r  z.'decimal.Context' object has no attribute '%s')r  _rounding_modesrk   rV   r  r  AttributeError)r-   r  rm   r'   r'   r)   r  i  s(    zContext.__setattr__c             C   s   t d| d S )Nz%s cannot be deleted)r  )r-   r  r'   r'   r)   __delattr__  s    zContext.__delattr__c          	   C   sN   dd | j  D }dd | j D }| j| j| j| j| j| j| j	||ffS )Nc             S   s   g | ]\}}|r|qS r'   r'   )rK  sigr  r'   r'   r)   rM    s    z&Context.__reduce__.<locals>.<listcomp>c             S   s   g | ]\}}|r|qS r'   r'   )rK  r  r  r'   r'   r)   rM    s    )
r  itemsr  rb  r>   r=   r  r?   r   r   )r-   r  r  r'   r'   r)   rc    s
    zContext.__reduce__c             C   s|   g }| dt|   dd | j D }| dd| d  dd | j D }| dd| d  d|d	 S )
NzrContext(prec=%(prec)d, rounding=%(rounding)s, Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d, clamp=%(clamp)dc             S   s   g | ]\}}|r|j qS r'   )r0   )rK  rw   r  r'   r'   r)   rM    s    z$Context.__repr__.<locals>.<listcomp>zflags=[z, ]c             S   s   g | ]\}}|r|j qS r'   )r0   )rK  r  r  r'   r'   r)   rM    s    ztraps=[))rf   varsr  r  rg   r  )r-   r   namesr'   r'   r)   r     s    zContext.__repr__c             C   s   x| j D ]}d| j |< qW d S )Nr%   )r  )r-   flagr'   r'   r)   rE     s    zContext.clear_flagsc             C   s   x| j D ]}d| j |< qW d S )Nr%   )r  )r-   r  r'   r'   r)   clear_traps  s    zContext.clear_trapsc          
   C   s.   t | j| j| j| j| j| j| j| j| j		}|S )N)
r   r>   r=   r  r?   r   r   r  r  r  )r-   ncr'   r'   r)   r!    s    zContext._shallow_copyc          
   C   s6   t | j| j| j| j| j| j| j | j	 | j
	}|S )N)r   r>   r=   r  r?   r   r   r  rD   r  r  )r-   r  r'   r'   r)   rD     s
    zContext.copyc             G   sZ   t ||}|| jkr(| j| f| S d| j|< | j| sN| j| f| S ||d S )Nr,   )_condition_maprA   r  r/   r  r  )r-   Z	conditionZexplanationr(   errorr'   r'   r)   r]     s    


zContext._raise_errorc             C   s
   | j t S )N)_ignore_flagsr  )r-   r'   r'   r)   rU    s    zContext._ignore_all_flagsc             G   s   | j t| | _ t|S )N)r  rc   )r-   r  r'   r'   r)   r    s    zContext._ignore_flagsc             G   s<   |rt |d ttfr|d }x|D ]}| j| q$W d S )Nr%   )rX   rd   rc   r  remove)r-   r  r  r'   r'   r)   _regard_flags  s    
zContext._regard_flagsc             C   s   t | j| j d S )Nr,   )rM   r  r>   )r-   r'   r'   r)   r     s    zContext.Etinyc             C   s   t | j| j d S )Nr,   )rM   r?   r>   )r-   r'   r'   r)   r     s    zContext.Etopc             C   s   | j }|| _ |S )N)r=   )r-   rd  r=   r'   r'   r)   r"    s    zContext._set_roundingrI   c             C   sj   t |tr*|| ksd|kr*| tdS t|| d}| r`t|j| j	| j
 kr`| tdS || S )NrJ   zAtrailing or leading whitespace and underscores are not permitted.)r.   zdiagnostic info too long in NaN)rX   rY   r[   r]   r   r   r|   r_   r6   r>   r   r   )r-   r;  rz   r'   r'   r)   create_decimal  s    zContext.create_decimalc             C   s   t |}|| S )N)r   rj   r   )r-   rw   rz   r'   r'   r)   create_decimal_from_float  s    
z!Context.create_decimal_from_floatc             C   s   t |dd}|j| dS )NT)r   )r.   )r   r   )r-   r  r'   r'   r)   ra     s    zContext.absc             C   s8   t |dd}|j|| d}|tkr0td| n|S d S )NT)r   )r.   zUnable to convert %s to Decimal)r   r   r   rk   )r-   r  rL  r   r'   r'   r)   add2  s
    zContext.addc             C   s   t || S )N)rY   r   )r-   r  r'   r'   r)   _applyG  s    zContext._applyc             C   s   t |tstd| S )Nz,canonical requires a Decimal as an argument.)rX   r   rk   r+  )r-   r  r'   r'   r)   r+  J  s    	
zContext.canonicalc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   r   )r-   r  rL  r'   r'   r)   r   W  s    !zContext.comparec             C   s   t |dd}|j|| dS )NT)r   )r.   )r   r,  )r-   r  rL  r'   r'   r)   r,  {  s     zContext.compare_signalc             C   s   t |dd}||S )NT)r   )r   r'  )r-   r  rL  r'   r'   r)   r'    s    zContext.compare_totalc             C   s   t |dd}||S )NT)r   )r   r0  )r-   r  rL  r'   r'   r)   r0    s    zContext.compare_total_magc             C   s   t |dd}| S )NT)r   )r   r   )r-   r  r'   r'   r)   r     s    
zContext.copy_absc             C   s   t |dd}t|S )NT)r   )r   r   )r-   r  r'   r'   r)   copy_decimal  s    
zContext.copy_decimalc             C   s   t |dd}| S )NT)r   )r   r   )r-   r  r'   r'   r)   r     s    
zContext.copy_negatec             C   s   t |dd}||S )NT)r   )r   r1  )r-   r  rL  r'   r'   r)   r1    s    zContext.copy_signc             C   s8   t |dd}|j|| d}|tkr0td| n|S d S )NT)r   )r.   zUnable to convert %s to Decimal)r   r   r   rk   )r-   r  rL  r   r'   r'   r)   divide  s
    zContext.dividec             C   s8   t |dd}|j|| d}|tkr0td| n|S d S )NT)r   )r.   zUnable to convert %s to Decimal)r   r   r   rk   )r-   r  rL  r   r'   r'   r)   
divide_int'  s
    zContext.divide_intc             C   s8   t |dd}|j|| d}|tkr0td| n|S d S )NT)r   )r.   zUnable to convert %s to Decimal)r   r   r   rk   )r-   r  rL  r   r'   r'   r)   r   >  s
    zContext.divmodc             C   s   t |dd}|j| dS )NT)r   )r.   )r   rN   )r-   r  r'   r'   r)   rN   S  s    zContext.expc             C   s   t |dd}|j||| dS )NT)r   )r.   )r   r   )r-   r  rL  r$  r'   r'   r)   r   k  s    zContext.fmac             C   s   t |tstd| S )Nz/is_canonical requires a Decimal as an argument.)rX   r   rk   r4  )r-   r  r'   r'   r)   r4    s    	
zContext.is_canonicalc             C   s   t |dd}| S )NT)r   )r   r5  )r-   r  r'   r'   r)   r5    s    zContext.is_finitec             C   s   t |dd}| S )NT)r   )r   r  )r-   r  r'   r'   r)   r    s    zContext.is_infinitec             C   s   t |dd}| S )NT)r   )r   r   )r-   r  r'   r'   r)   r     s    zContext.is_nanc             C   s   t |dd}|j| dS )NT)r   )r.   )r   r6  )r-   r  r'   r'   r)   r6    s    zContext.is_normalc             C   s   t |dd}| S )NT)r   )r   r   )r-   r  r'   r'   r)   r     s    zContext.is_qnanc             C   s   t |dd}| S )NT)r   )r   r7  )r-   r  r'   r'   r)   r7    s    zContext.is_signedc             C   s   t |dd}| S )NT)r   )r   r   )r-   r  r'   r'   r)   r     s    zContext.is_snanc             C   s   t |dd}|j| dS )NT)r   )r.   )r   r8  )r-   r  r'   r'   r)   r8    s    zContext.is_subnormalc             C   s   t |dd}| S )NT)r   )r   r9  )r-   r  r'   r'   r)   r9  !  s    zContext.is_zeroc             C   s   t |dd}|j| dS )NT)r   )r.   )r   rA  )r-   r  r'   r'   r)   rA  2  s    z
Context.lnc             C   s   t |dd}|j| dS )NT)r   )r.   )r   rD  )r-   r  r'   r'   r)   rD  H  s    zContext.log10c             C   s   t |dd}|j| dS )NT)r   )r.   )r   rE  )r-   r  r'   r'   r)   rE  d  s    zContext.logbc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   rO  )r-   r  rL  r'   r'   r)   rO  ~  s    zContext.logical_andc             C   s   t |dd}|j| dS )NT)r   )r.   )r   rQ  )r-   r  r'   r'   r)   rQ    s    zContext.logical_invertc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   rR  )r-   r  rL  r'   r'   r)   rR    s    zContext.logical_orc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   rP  )r-   r  rL  r'   r'   r)   rP    s    zContext.logical_xorc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   r   )r-   r  rL  r'   r'   r)   r     s    zContext.maxc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   rS  )r-   r  rL  r'   r'   r)   rS    s    zContext.max_magc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   r   )r-   r  rL  r'   r'   r)   r     s    zContext.minc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   rT  )r-   r  rL  r'   r'   r)   rT  )  s    zContext.min_magc             C   s   t |dd}|j| dS )NT)r   )r.   )r   r   )r-   r  r'   r'   r)   minus:  s    zContext.minusc             C   s8   t |dd}|j|| d}|tkr0td| n|S d S )NT)r   )r.   zUnable to convert %s to Decimal)r   r   r   rk   )r-   r  rL  r   r'   r'   r)   multiplyK  s
    zContext.multiplyc             C   s   t |dd}|j| dS )NT)r   )r.   )r   rW  )r-   r  r'   r'   r)   rW  k  s    zContext.next_minusc             C   s   t |dd}|j| dS )NT)r   )r.   )r   rX  )r-   r  r'   r'   r)   rX    s    zContext.next_plusc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   rY  )r-   r  rL  r'   r'   r)   rY    s     zContext.next_towardc             C   s   t |dd}|j| dS )NT)r   )r.   )r   r  )r-   r  r'   r'   r)   r    s    zContext.normalizec             C   s   t |dd}|j| dS )NT)r   )r.   )r   r[  )r-   r  r'   r'   r)   r[    s    /zContext.number_classc             C   s   t |dd}|j| dS )NT)r   )r.   )r   r   )r-   r  r'   r'   r)   plus   s    zContext.plusc             C   s:   t |dd}|j||| d}|tkr2td| n|S d S )NT)r   )r.   zUnable to convert %s to Decimal)r   r  r   rk   )r-   r  rL  r   r   r'   r'   r)   power  s
    IzContext.powerc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   r   )r-   r  rL  r'   r'   r)   r   a  s    7zContext.quantizec             C   s   t dS )Nr   )r   )r-   r'   r'   r)   r\    s    zContext.radixc             C   s8   t |dd}|j|| d}|tkr0td| n|S d S )NT)r   )r.   zUnable to convert %s to Decimal)r   r   r   rk   )r-   r  rL  r   r'   r'   r)   r     s
    zContext.remainderc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   r   )r-   r  rL  r'   r'   r)   r     s    zContext.remainder_nearc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   r`  )r-   r  rL  r'   r'   r)   r`    s    zContext.rotatec             C   s   t |dd}||S )NT)r   )r   r  )r-   r  rL  r'   r'   r)   r    s    zContext.same_quantumc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   ra  )r-   r  rL  r'   r'   r)   ra     s    zContext.scalebc             C   s   t |dd}|j|| dS )NT)r   )r.   )r   r   )r-   r  rL  r'   r'   r)   r   3  s    zContext.shiftc             C   s   t |dd}|j| dS )NT)r   )r.   )r   r&  )r-   r  r'   r'   r)   r&  Q  s    zContext.sqrtc             C   s8   t |dd}|j|| d}|tkr0td| n|S d S )NT)r   )r.   zUnable to convert %s to Decimal)r   r   r   rk   )r-   r  rL  r   r'   r'   r)   subtractq  s
    zContext.subtractc             C   s   t |dd}|j| dS )NT)r   )r.   )r   r   )r-   r  r'   r'   r)   r     s    zContext.to_eng_stringc             C   s   t |dd}|j| dS )NT)r   )r.   )r   r   )r-   r  r'   r'   r)   to_sci_string  s    zContext.to_sci_stringc             C   s   t |dd}|j| dS )NT)r   )r.   )r   r   )r-   r  r'   r'   r)   r     s    zContext.to_integral_exactc             C   s   t |dd}|j| dS )NT)r   )r.   )r   r   )r-   r  r'   r'   r)   r     s    zContext.to_integral_value)	NNNNNNNNN)N)rI   )N)Wr0   r1   r2   r}  r  r  r  r  rc  r   rE   r  r!  rD   re  r]   rU  r  r  r   r   r   r"  r  r  ra   r  r  r+  r   r,  r'  r0  r   r  r   r1  r  r  r   rN   r   r4  r5  r  r   r6  r   r7  r   r8  r9  rA  rD  rE  rO  rQ  rR  rP  r   rS  r   rT  r  r  rW  rX  rY  r  r[  r  r  r   r\  r   r   r`  r  ra  r   r&  r  r   r  r   r   r{  r'   r'   r'   r)   r     s     
"

$#% #2
P:&" c               @   s&   e Zd ZdZdddZdd ZeZdS )rb   )r;   rM   rN   Nc             C   sf   |d krd | _ d| _d | _nFt|trD|j| _ t|j| _|j| _n|d | _ |d | _|d | _d S )Nr%   r,   rT   )r;   rM   rN   rX   r   r5   r6   rG   )r-   rm   r'   r'   r)   r}    s    



z_WorkRep.__init__c             C   s   d| j | j| jf S )Nz(%r, %r, %r))r;   rM   rN   )r-   r'   r'   r)   r     s    z_WorkRep.__repr__)N)r0   r1   r2   rt  r}  r   r   r'   r'   r'   r)   rb     s   
rb   c             C   s   | j |j k r|}| }n| }|}tt|j}tt|j}|j td|| d  }||j  d |k rpd|_||_ | jd|j |j   9  _|j |_ | |fS )Nr}   rT   r,   r   )rN   r_   rY   rM   r   )r   r   r>   Ztmpr   Ztmp_lenZ	other_lenrN   r'   r'   r)   r     s    r   c             C   sb   | dkrdS |dkr | d|  S t t| }t|t|d }|| k rPd S | d|   S d S )Nr%   r   rI   )rY   ra   r_   rstrip)r3   r   Zstr_nZval_nr'   r'   r)   r  &  s    r  c             C   sF   | dks|dkrt dd}x$||kr@|||  |  d?  }}qW |S )Nr%   z3Both arguments to _sqrt_nearest should be positive.r,   )re   )r3   r  rL  r'   r'   r)   _sqrt_nearest;  s    
r  c             C   s2   d|> | |?  }}|d| |d @  |d@  |k S )Nr,   rT   r'   )r  r   rL  r   r'   r'   r)   _rshift_nearestJ  s    r  c             C   s&   t | |\}}|d| |d@  |k S )NrT   r,   )r   )r  rL  r   r   r'   r'   r)   _div_nearestR  s    r  r   c       	   	   C   s   | | }d}xn||kr*t ||| > |ksF||krzt ||| ? |krzt|| d> |t||t||  | }|d7 }qW tdtt| d|   }t||}t||}x0t|d ddD ]}t||t|| | }qW t|| |S )Nr%   r,   irS   r}   )ra   r  r  r  rM   r_   rY   r   )	r  MLr  RTZyshiftwrx   r'   r'   r)   _ilogZ  s    

r  c       
      C   s   |d7 }t t| }|| || dk }|dkrd| }|| | }|dkrZ| d| 9 } nt| d|  } t| |}t|}t|| |}|| }	nd}t|d|  }	t|	| dS )NrT   r,   r%   r   r   )r_   rY   r  r  _log10_digits)
r$  r   r  r%  rw   r  rx   log_dZlog_10Zlog_tenpowerr'   r'   r)   rC    s     

rC  c       	      C   s   |d7 }t t| }|| || dk }|dkrr|| | }|dkrR| d| 9 } nt| d|  } t| d| }nd}|rt tt|d }|| dkrt|t||  d| }qd}nd}t|| dS )NrT   r,   r%   r   r   )r_   rY   r  r  ra   r  )	r$  r   r  r%  rw   rx   r  r  Z	f_log_tenr'   r'   r)   r@    s"    r@  c               @   s   e Zd Zdd Zdd ZdS )_Log10Memoizec             C   s
   d| _ d S )NZ/23025850929940456840179914546843642076011014886)rq   )r-   r'   r'   r)   r}    s    z_Log10Memoize.__init__c             C   s   |dk rt d|t| jkrd}xLd|| d  }tttd| |d}|| d  d| krdP |d7 }q$W |dd d | _t| jd |d	  S )
Nr%   zp should be nonnegativerS   r   rT   r   rI   r}   r,   )re   r_   rq   rY   r  r  r  rM   )r-   r  r  r  rq   r'   r'   r)   	getdigits  s    	z_Log10Memoize.getdigitsN)r0   r1   r2   r}  r  r'   r'   r'   r)   r    s   r  c       	      C   s   t | |> | }tdtt| d|   }t| |}||> }x.t|d ddD ]}t| ||  || }qRW x6t|d ddD ]"}||d > }t|||  |}qW || S )NirS   r,   r%   r}   rT   )r  rM   r_   rY   r  r   )	r  r  r  r  r  r  ZMshiftr   rx   r'   r'   r)   _iexp  s    
r  c       	      C   s   |d7 }t d|tt|  d }|| }|| }|dkrH| d|  }n| d|   }t|t|\}}t|d| }tt|d| d|| d fS )NrT   r%   r,   r   i  rS   )r   r_   rY   r   r  r  r  )	r$  r   r  r  r   r   ZcshiftZquotr  r'   r'   r)   r2  "  s    r2  c             C   s   t tt|| }t| ||| d }|| }|dkrJ|| d|  }nt|| d|  }|dkrt t| | dk|dkkrd|d  d d|  }	}
qd| d |  }	}
n,t||d  |d \}	}
t|	d}	|
d7 }
|	|
fS )Nr,   r%   r   )r_   rY   ra   r@  r  r2  )r  r  r	  r
  r  rL  Zlxcr   Zpcry   rN   r'   r'   r)   r  F  s    
r  r   F   5   (      r:     r   rr   )	r   2345678r<   c             C   s0   | dkrt dt| }dt| ||d   S )Nr%   z0The argument to _log10_lb should be nonnegative.r   )re   rY   r_   )r$  Z
correctionZstr_cr'   r'   r)   r  p  s    r  c             C   sL   t | tr| S t | tr t| S |r8t | tr8t| S |rHtd|  tS )NzUnable to convert %s to Decimal)rX   r   rM   ri   rj   rk   r   )r   r   Zallow_floatr'   r'   r)   r   {  s    


r   c             C   s   t |tr| |fS t |tjrR| jsDt| jtt| j	|j
 | j} | t|jfS |rrt |tjrr|jdkrr|j}t |trt }|rd|jt< n|td | t|fS ttfS )Nr%   r,   z;strict semantics for mixing floats and Decimals are enabled)rX   r   _numbersZRationalrH   r4   r5   rY   rM   r6   denominatorrG   	numeratorZComplexr   r   ri   r   r  r   r]   rj   r   )r-   r   r   r.   r'   r'   r)   r     s$    

r   r   i?B i)r>   r=   r  r  r?   r  r   r   rU   )r>   r=   r  r  a          # A numeric string consists of:
#    \s*
    (?P<sign>[-+])?              # an optional sign, followed by either...
    (
        (?=\d|\.\d)              # ...a number (with at least one digit)
        (?P<int>\d*)             # having a (possibly empty) integer part
        (\.(?P<frac>\d*))?       # followed by an optional fractional part
        (E(?P<exp>[-+]?\d+))?    # followed by an optional exponent, or...
    |
        Inf(inity)?              # ...an infinity, or...
    |
        (?P<signal>s)?           # ...an (optionally signaling)
        NaN                      # NaN
        (?P<diag>\d*)            # with (possibly empty) diagnostic info.
    )
#    \s*
    \Z
z0*$z50*$z\A
(?:
   (?P<fill>.)?
   (?P<align>[<>=^])
)?
(?P<sign>[-+ ])?
(?P<alt>\#)?
(?P<zeropad>0)?
(?P<minimumwidth>(?!0)\d+)?
(?P<thousands_sep>,)?
(?:\.(?P<precision>0|(?!0)\d+))?
(?P<type>[eEfFgGn%])?
\Z
c             C   s  t | }|d krtd|  | }|d }|d }|d d k	|d< |d rv|d k	rbtd|  |d k	rvtd|  |p|d|d< |pd|d< |d	 d krd
|d	< t|d pd|d< |d d k	rt|d |d< |d dkr|d d ks|d dkrd|d< |d dkrfd|d< |d kr&t }|d d k	r@td|  |d |d< |d |d< |d |d< n*|d d kr|d|d< ddg|d< d|d< |S )NzInvalid format specifier: fillalignzeropadz7Fill character conflicts with '0' in format specifier: z2Alignment conflicts with '0' in format specifier:  >r;   rL   minimumwidthrI   rk  r%   rd  ZgGnr,   r3   ri  thousands_sepzJExplicit thousands separator conflicts with 'n' type in format specifier: groupingdecimal_pointrK   rS   r   )_parse_format_specifier_regexmatchre   	groupdictrM   _locale
localeconv)format_specrg  rn   Zformat_dictr  r  r'   r'   r)   rm    sN    

rm  c       	      C   s   |d }|d }||t |  t |  }|d }|dkrF| | | }nj|dkr\||  | }nT|dkrr| | | }n>|dkrt |d }|d | |  | ||d   }ntd	|S )
Nr  r  r  <r  =^rT   zUnrecognised alignment field)r_   re   )	r;   rr  rq  r  r  Zpaddingr  r{   Zhalfr'   r'   r)   ro  k  s    "ro  c             C   sp   ddl m}m} | sg S | d dkrJt| dkrJ|| d d || d S | d tjkrd| d d S tdd S )Nr%   )chainrepeatr}   rT   r   z unrecognised format for grouping)	itertoolsr  r  r_   r  CHAR_MAXre   )r  r  r  r'   r'   r)   _group_lengths  s    r  c             C   s   |d }|d }g }xt |D ]~}|dkr2tdttt| |d|}|d|t|   | | d    | d |  } ||8 }| s|dkrP |t|8 }qW tt| |d}|d|t|   | | d    |t|S )Nr  r  r%   zgroup length should be positiver,   rI   )r  re   r   r   r_   rf   rg   reversed)rq   rq  	min_widthsepr  groupsr%  r'   r'   r)   _insert_thousands_sep  s     $$r  c             C   s$   | rdS |d dkr|d S dS d S )NrL   r;   z +rK   r'   )is_negativerq  r'   r'   r)   rn    s
    rn  c             C   s   t | |}|s|d r"|d | }|dks6|d dkr\ddddd|d  }|d	||7 }|d d
krp|d
7 }|d r|d t| t| }nd}t|||}t||| |S )NZaltr  r%   rd  rl  r   r   )r   r   rj  ri  z{0}{1:+}rh  r  r  )rn  formatr_   r  ro  )r  ro   rp   rN   rq  r;   Zecharr  r'   r'   r)   rp    s    
rp  ZInfz-Infr   r}   rT   )N)F)r%   )r   )r   )FF)F)N)r,   )x__all__r0   Z	__xname____version__Z__libmpdec_version__Zmathrs   Znumbersr  syscollectionsr&   Z_namedtupler   ImportErrorr   r   r   r   r   r   r   r   r$   maxsizer    r!   r"   r#   ArithmeticErrorr   r   r	   r   ZeroDivisionErrorr
   r   r   r   r   r   r   r   r   rk   r   r  r  r  ZcontextvarsZ
ContextVarr@   r   r   r   rV   r   r4   NumberregisterrF   r   rb   r   rM   rv   r  r  r  r  r  r  rC  r@  r  r  r  r  r2  r  r  r   r   r   r   r   recompileVERBOSE
IGNORECASEr  rZ   r   r   DOTALLr  localer  rm  ro  r  r  rn  rp  r?  r>  r8   r.  r  r-  r:   	hash_infomodulusr   rZ  r   r   r   r   r   r'   r'   r'   r)   <module>u   sb  


&


.                          
             ^

0",#
%$+
	

*
P
%
)