U e5d _@sddlmZddlZddlZddlZddlZddlZddgZddZddZ ej j Z ej j ZedejejBZGd ddejZdS) DecimalNFractiongcdcCsfddl}|dtdt|tkr2t|kr\nn&|p<|dkrPt|| St||St||S)Nrz6fractions.gcd() is deprecated. Use math.gcd() instead.)warningswarnDeprecationWarningtypeintmathr_gcd)abrr!/usr/lib64/python3.8/fractions.pyrs   cCs|r|||}}q|SNrrrrrrr sr aC \A\s* # optional whitespace at the start, then (?P[-+]?) # an optional sign, then (?=\d|\.\d) # lookahead for digit or .digit (?P\d*) # numerator (possibly empty) (?: # followed by (?:/(?P\d+))? # an optional denominator | # or (?:\.(?P\d*))? # an optional fractional part (?:E(?P[-+]?\d+))? # and optional exponent ) \s*\Z # and optional whitespace to finish cseZdZdZdQddfdd Zedd Zed d Zd d ZdRddZ e ddZ e ddZ ddZ ddZddZddZeeej\ZZddZeeej\ZZdd Zeeej\ZZd!d"Zeeej\ZZ d#d$Z!ee!ej"\Z#Z$d%d&Z%ee%e&\Z'Z(d'd(Z)ee)ej*\Z+Z,d)d*Z-d+d,Z.d-d.Z/d/d0Z0d1d2Z1d3d4Z2d5d6Z3d7d8Z4dSd9d:Z5d;d<Z6d=d>Z7d?d@Z8dAdBZ9dCdDZ:dEdFZ;dGdHZdMdNZ?dOdPZ@ZAS)Tr _numerator _denominatorrNT _normalizec sRtt||}|dkrdt|tkr6||_d|_|St|tj rV|j |_|j |_|St|t t frx|\|_|_|St|trZt|}|dkrtd|t|dpd}|d}|rt|}nvd}|d}|rdt|}||t|}||9}|d} | rBt| } | d kr4|d| 9}n|d| 9}|d d krb| }ntd nft|tkrt|krnnn@t|tj rt|tj r|j |j |j |j }}ntd |d krtd||rBt|tkrt|kr(nnt||} |d kr2| } n t||} || }|| }||_||_|S)Nz Invalid literal for Fraction: %rZnum0denomdecimal exprZsign-z2argument should be a string or a Rational instancez+both arguments should be Rational instanceszFraction(%s, 0))superr__new__r r rr isinstancenumbersRational numerator denominatorfloatras_integer_ratiostr_RATIONAL_FORMATmatch ValueErrorgrouplen TypeErrorZeroDivisionErrorr rr ) clsr%r&rselfmrrZscalerg __class__rrr!Tsx            $     $   zFraction.__new__cCsDt|tjr||St|ts8td|j|t|jf||S)Nz.%s.from_float() only takes floats, not %r (%s))r"r#Integralr'r/__name__r r()r1frrr from_floats  zFraction.from_floatcCsVddlm}t|tjr&|t|}n$t||sJtd|j|t|jf|| S)Nrrz2%s.from_decimal() only takes Decimals, not %r (%s)) rrr"r#r7r r/r8r r()r1Zdecrrrr from_decimals   zFraction.from_decimalcCs |j|jfSrrr2rrrr(szFraction.as_integer_ratio@Bc Cs|dkrtd|j|kr"t|Sd\}}}}|j|j}}||}|||} | |krZq|||||| f\}}}}||||}}q<|||} t|| ||| |} t||} t| |t| |kr| S| SdS)Nrz$max_denominator should be at least 1)rrrr)r,rrrabs) r2Zmax_denominatorZp0Zq0Zp1Zq1ndrZq2kZbound1Zbound2rrrlimit_denominators$      zFraction.limit_denominatorcCs|jSr)rrrrrr%szFraction.numeratorcCs|jSr)rrCrrrr&szFraction.denominatorcCsd|jj|j|jfS)Nz %s(%s, %s))r6r8rrr<rrr__repr__"szFraction.__repr__cCs(|jdkrt|jSd|j|jfSdS)Nrz%s/%s)rr)rr<rrr__str__'s  zFraction.__str__csTfdd}djd|_j|_fdd}djd|_j|_||fS)NcsPt|ttfr||St|tr0t||St|trHt||StSdSr)r"r rr'complexNotImplementedrfallback_operatormonomorphic_operatorrrforward~s   z-Fraction._operator_fallbacks..forward__csZt|tjr||St|tjr4t|t|St|tjrRt|t|StSdSr)r"r#r$ZRealr'ComplexrFrGrrrHrrreverses    z-Fraction._operator_fallbacks..reverseZ__r)r8__doc__)rJrIrKrOrrHr_operator_fallbacks.sP  zFraction._operator_fallbackscCs,|j|j}}t|j||j|||Srr&rr%rrdadbrrr_addsz Fraction._addcCs,|j|j}}t|j||j|||SrrRrSrrr_subsz Fraction._subcCst|j|j|j|jSrrr%r&rrrr_mulsz Fraction._mulcCst|j|j|j|jSrrXrrrr_divs  z Fraction._divcCs|j|j|j|jSrr%r&rrrr _floordivszFraction._floordivcCs:|j|j}}t|j|||j\}}|t|||fSr)r&divmodr%r)rrrTrUZdivZn_modrrr_divmodszFraction._divmodcCs,|j|j}}t|j||j|||SrrRrSrrr_modsz Fraction._modcCst|tjr|jdkr|j}|dkr>t|j||j|ddS|jdkrft|j| |j| ddSt|j | |j | ddSqt|t|Sn t||SdS)NrrFr) r"r#r$r&r%rrrr')rrZpowerrrr__pow__s&       zFraction.__pow__cCs\|jdkr|jdkr||jSt|tjrrrrCrrr__abs__szFraction.__abs__cCs*|jdkr|j |j S|j|jSdS)NrrrCrrr __trunc__s zFraction.__trunc__cCs |j|jSrr[rCrrr __floor__ szFraction.__floor__cCs|j |j Srr[rCrrr__ceil__szFraction.__ceil__cCs|dkrZt|j|j\}}|d|jkr,|S|d|jkrB|dS|ddkrR|S|dSdt|}|dkrtt|||Stt|||SdS)Nrrrr)r]r%r&r>rround)r2ZndigitsZfloorZ remaindershiftrrr __round__s  zFraction.__round__cCsPt|jtdt}|st}nt|j|t}|dkr:|n| }|dkrLdS|S)Nrr)powr_PyHASH_MODULUS _PyHASH_INFr>r)r2ZdinvZhash_resultrrr__hash__,s zFraction.__hash__cCst|tkr |j|ko|jdkSt|tjrD|j|jkoB|j|jkSt|tj r`|j dkr`|j }t|t rt |s~t |rd|kS|||kSntSdS)Nrr)r r rrr"r#r$r%r&rMimagrealr'r isnanisinfr:rGrrrr__eq__Bs     zFraction.__eq__cCsht|tjr&||j|j|j|jSt|tr`t |sDt |rN|d|S||| |Snt SdS)Nrt) r"r#r$rr&rr%r'r rwrxr:rG)r2otheroprrr_richcmpWs     zFraction._richcmpcCs||tjSr)r|operatorltrrrr__lt__mszFraction.__lt__cCs||tjSr)r|r}gtrrrr__gt__qszFraction.__gt__cCs||tjSr)r|r}lerrrr__le__uszFraction.__le__cCs||tjSr)r|r}gerrrr__ge__yszFraction.__ge__cCs t|jSr)boolrrCrrr__bool__}szFraction.__bool__cCs|jt|ffSr)r6r)r<rrr __reduce__szFraction.__reduce__cCs t|tkr|S||j|jSrr rr6rrr<rrr__copy__s zFraction.__copy__cCs t|tkr|S||j|jSrr)r2Zmemorrr __deepcopy__s zFraction.__deepcopy__)rN)r=)N)Br8 __module__ __qualname__ __slots__r! classmethodr:r;r(rBpropertyr%r&rDrErQrVr}add__add____radd__rWsub__sub____rsub__rYmul__mul____rmul__rZtruediv __truediv__ __rtruediv__r\floordiv __floordiv__ __rfloordiv__r^r] __divmod__ __rdivmod__r_mod__mod____rmod__r`rardrerfrgrhrirlrsryr|rrrrrrrr __classcell__rrr5rr<sbm   7  k )rrr r#r}resys__all__rr hash_infomodulusrpinfrqcompileVERBOSE IGNORECASEr*r$rrrrrs