a
    ze_                     @   st   d dl mZ d dlZd dlZd dlZd dlZd dlZdgZejj	Z
ejjZedejejB ZG dd dejZdS )    DecimalNFractionaC  
    \A\s*                      # optional whitespace at the start, then
    (?P<sign>[-+]?)            # an optional sign, then
    (?=\d|\.\d)                # lookahead for digit or .digit
    (?P<num>\d*)               # numerator (possibly empty)
    (?:                        # followed by
       (?:/(?P<denom>\d+))?    # an optional denominator
    |                          # or
       (?:\.(?P<decimal>\d*))? # an optional fractional part
       (?:E(?P<exp>[-+]?\d+))? # and optional exponent
    )
    \s*\Z                      # and optional whitespace to finish
c                       s  e Zd ZdZdQdd fddZedd	 Zed
d Zdd ZdRddZ	e
dd Ze
dd Zdd Zdd Zdd Zdd Zeeej\ZZdd Zeeej\ZZdd  Zeeej\ZZd!d" Zeeej\ZZ d#d$ Z!ee!ej"\Z#Z$d%d& Z%ee%e&\Z'Z(d'd( Z)ee)ej*\Z+Z,d)d* Z-d+d, Z.d-d. Z/d/d0 Z0d1d2 Z1d3d4 Z2d5d6 Z3d7d8 Z4dSd9d:Z5d;d< Z6d=d> Z7d?d@ Z8dAdB Z9dCdD Z:dEdF Z;dGdH Z<dIdJ Z=dKdL Z>dMdN Z?dOdP Z@  ZAS )Tr   
_numerator_denominatorr   NT
_normalizec                   s"  t t| | }|d u rdt|tu r6||_d|_|S t|tj	rV|j
|_|j|_|S t|ttfrx| \|_|_|S t|trZt|}|d u rtd| t|dpd}|d}|rt|}nvd}|d}|rdt| }|| t| }||9 }|d}	|	rBt|	}	|	d	kr4|d|	 9 }n|d|	  9 }|d
dkrb| }ntdnft|t  u rt|u rn nn@t|tj	rt|tj	r|j
|j |j
|j  }}ntd|d	krtd| |rt||}
|d	k r|
 }
||
 }||
 }||_||_|S )N   z Invalid literal for Fraction: %rnum0denomdecimal
   expr   sign-z2argument should be a string or a Rational instancez+both arguments should be Rational instanceszFraction(%s, 0))superr   __new__typeintr   r   
isinstancenumbersRational	numeratordenominatorfloatr   as_integer_ratiostr_RATIONAL_FORMATmatch
ValueErrorgrouplen	TypeErrorZeroDivisionErrormathgcd)clsr   r   r	   selfmr   r   Zscaler   g	__class__ /usr/lib/python3.9/fractions.pyr   >   st    







$




zFraction.__new__c                 C   sD   t |tjr| |S t |ts8td| j|t|jf | |  S )Nz.%s.from_float() only takes floats, not %r (%s))r   r   Integralr   r$   __name__r   r   )r(   fr.   r.   r/   
from_float   s    
zFraction.from_floatc                 C   sV   ddl m} t|tjr&|t|}n$t||sJtd| j|t|jf | |	  S )Nr   r   z2%s.from_decimal() only takes Decimals, not %r (%s))
r   r   r   r   r0   r   r$   r1   r   r   )r(   Zdecr   r.   r.   r/   from_decimal   s    
zFraction.from_decimalc                 C   s   | j | jfS Nr   r)   r.   r.   r/   r      s    zFraction.as_integer_ratio@B c                 C   s   |dk rt d| j|kr"t| S d\}}}}| j| j }}|| }|||  }	|	|krZq|||||  |	f\}}}}||||   }}q<|| | }
t||
|  ||
|  }t||}t||  t||  kr|S |S d S )Nr
   z$max_denominator should be at least 1)r   r
   r
   r   )r!   r   r   r   abs)r)   Zmax_denominatorZp0Zq0Zp1Zq1ndaZq2kZbound1Zbound2r.   r.   r/   limit_denominator   s$     

zFraction.limit_denominatorc                 C   s   | j S r5   )r   r;   r.   r.   r/   r      s    zFraction.numeratorc                 C   s   | j S r5   )r   r>   r.   r.   r/   r     s    zFraction.denominatorc                 C   s   d| j j| j| jf S )Nz
%s(%s, %s))r-   r1   r   r   r6   r.   r.   r/   __repr__  s    zFraction.__repr__c                 C   s(   | j dkrt| jS d| j| j f S d S )Nr
   z%s/%s)r   r   r   r6   r.   r.   r/   __str__  s    

zFraction.__str__c                    sT    fdd}d j  d |_ j|_ fdd}d j  d |_ j|_||fS )Nc                    sP   t |ttfr| |S t |tr0 t| |S t |trH t| |S tS d S r5   )r   r   r   r   complexNotImplementedr;   bfallback_operatormonomorphic_operatorr.   r/   forwardd  s    


z-Fraction._operator_fallbacks.<locals>.forward__c                    sZ   t |tjr|| S t |tjr4 t|t| S t |tjrR t|t| S tS d S r5   )r   r   r   ZRealr   ComplexrA   rB   rD   r;   rE   r.   r/   reversep  s    
z-Fraction._operator_fallbacks.<locals>.reverseZ__r)r1   __doc__)rG   rF   rH   rL   r.   rE   r/   _operator_fallbacks  s    P	
zFraction._operator_fallbacksc                 C   s,   | j |j  }}t| j| |j|  || S r5   r   r   r   r;   rD   dadbr.   r.   r/   _add  s    zFraction._addc                 C   s,   | j |j  }}t| j| |j|  || S r5   rO   rP   r.   r.   r/   _sub  s    zFraction._subc                 C   s   t | j|j | j|j S r5   r   r   r   rC   r.   r.   r/   _mul  s    zFraction._mulc                 C   s   t | j|j | j|j S r5   rU   rC   r.   r.   r/   _div  s    
zFraction._divc                 C   s   | j |j | j|j   S r5   r   r   rC   r.   r.   r/   	_floordiv  s    zFraction._floordivc                 C   s:   | j |j  }}t| j| ||j \}}|t||| fS r5   )r   divmodr   r   )r;   rD   rQ   rR   ZdivZn_modr.   r.   r/   _divmod  s    zFraction._divmodc                 C   s,   | j |j  }}t| j| |j|  || S r5   rO   rP   r.   r.   r/   _mod  s    zFraction._modc                 C   s   t |tjr|jdkr|j}|dkr>t| j| | j| ddS | jdkrft| j|  | j|  ddS t| j |  | j |  ddS qt| t| S nt| | S d S )Nr
   r   Fr   )	r   r   r   r   r   r   r   r   r   )r;   rD   Zpowerr.   r.   r/   __pow__  s&    



zFraction.__pow__c                 C   s\   | j dkr| jdkr|| j S t|tjr<t|j|j|  S | j dkrP|| j S |t|  S )Nr
   r   )	r   r   r   r   r   r   r   r   r   rK   r.   r.   r/   __rpow__  s    


zFraction.__rpow__c                 C   s   t | j| jddS NFr   r   r   r   r>   r.   r.   r/   __pos__  s    zFraction.__pos__c                 C   s   t | j | jddS r_   r`   r>   r.   r.   r/   __neg__  s    zFraction.__neg__c                 C   s   t t| j| jddS r_   )r   r8   r   r   r>   r.   r.   r/   __abs__  s    zFraction.__abs__c                 C   s*   | j dk r| j  | j  S | j | j S d S )Nr   r   r>   r.   r.   r/   	__trunc__  s    
zFraction.__trunc__c                 C   s   | j | j S r5   rX   r>   r.   r.   r/   	__floor__  s    zFraction.__floor__c                 C   s   | j  | j  S r5   rX   r>   r.   r.   r/   __ceil__  s    zFraction.__ceil__c                 C   s   |d u rZt | j| j\}}|d | jk r,|S |d | jkrB|d S |d dkrR|S |d S dt| }|dkrtt| | |S tt| | | S d S )N   r
   r   r   )rZ   r   r   r8   r   round)r)   ndigitsfloor	remaindershiftr.   r.   r/   	__round__  s    zFraction.__round__c                 C   sd   zt | jdt}W n ty(   t}Y n0 ttt| j| }| jdkrN|n| }|dkr`dS |S )Nr   )powr   _PyHASH_MODULUSr!   _PyHASH_INFhashr8   r   )r)   ZdinvZhash_resultr.   r.   r/   __hash__  s    
zFraction.__hash__c                 C   s   t |tu r | j|ko| jdkS t|tjrD| j|jkoB| j|jkS t|tj	r`|j
dkr`|j}t|trt|s~t|rd|kS | | |kS ntS d S )Nr
   r           )r   r   r   r   r   r   r   r   r   rJ   imagrealr   r&   isnanisinfr3   rB   rC   r.   r.   r/   __eq__3  s    

zFraction.__eq__c                 C   sh   t |tjr&|| j|j | j|j S t |tr`t	|sDt
|rN|d|S || | |S ntS d S )Nrv   )r   r   r   r   r   r   r   r   r&   ry   rz   r3   rB   )r)   otheropr.   r.   r/   _richcmpH  s    


zFraction._richcmpc                 C   s   |  |tjS r5   )r~   operatorltrC   r.   r.   r/   __lt__^  s    zFraction.__lt__c                 C   s   |  |tjS r5   )r~   r   gtrC   r.   r.   r/   __gt__b  s    zFraction.__gt__c                 C   s   |  |tjS r5   )r~   r   lerC   r.   r.   r/   __le__f  s    zFraction.__le__c                 C   s   |  |tjS r5   )r~   r   gerC   r.   r.   r/   __ge__j  s    zFraction.__ge__c                 C   s
   t | jS r5   )boolr   r>   r.   r.   r/   __bool__n  s    zFraction.__bool__c                 C   s   | j t| ffS r5   )r-   r   r6   r.   r.   r/   
__reduce__v  s    zFraction.__reduce__c                 C   s    t | tkr| S | | j| jS r5   r   r   r-   r   r   r6   r.   r.   r/   __copy__y  s    zFraction.__copy__c                 C   s    t | tkr| S | | j| jS r5   r   )r)   memor.   r.   r/   __deepcopy__~  s    zFraction.__deepcopy__)r   N)r7   )N)Br1   
__module____qualname__	__slots__r   classmethodr3   r4   r   r=   propertyr   r   r?   r@   rN   rS   r   add__add____radd__rT   sub__sub____rsub__rV   mul__mul____rmul__rW   truediv__truediv____rtruediv__rY   floordiv__floordiv____rfloordiv__r[   rZ   
__divmod____rdivmod__r\   mod__mod____rmod__r]   r^   ra   rb   rc   rd   re   rf   rm   ru   r{   r~   r   r   r   r   r   r   r   r   __classcell__r.   r.   r,   r/   r   &   sb   i


7

k
!)r   r   r&   r   r   resys__all__	hash_infomodulusrq   infrr   compileVERBOSE
IGNORECASEr   r   r   r.   r.   r.   r/   <module>   s   
