B /g^\@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.7/fractions.pyrs   cCsx|r|||}}qW|S)Nr)r rrrrr 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 ZdRd dZe ddZ e ddZ ddZ ddZ ddZddZeeej\ZZddZeeej\ZZddZeeej\ZZdd Zeeej\ZZd!d"Z d#d$Z!d%d&Z"d'd(Z#d)d*Z$d+d,Z%d-d.Z&d/d0Z'd1d2Z(d3d4Z)d5d6Z*d7d8Z+dSd9d:Z,d;d<Z-d=d>Z.d?d@Z/dAdBZ0dCdDZ1dEdFZ2dGdHZ3dIdJZ4dKdLZ5dMdNZ6dOdPZ7Z8S)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__rrrTsr            $   $   zFraction.__new__cCsDt|tjr||St|ts8td|j|t|jf||S)Nz.%s.from_float() only takes floats, not %r (%s))rrIntegralr"r*__name__r r#)r,frrr from_floats   zFraction.from_floatcCsVddlm}t|tjr&|t|}n$t||sJtd|j|t|jf|| S)Nr)rz2%s.from_decimal() only takes Decimals, not %r (%s)) rrrrr1r r*r2r r#)r,Zdecrrrr from_decimals   zFraction.from_decimal@Bc Cs|dkrtd|j|kr"t|Sd\}}}}|j|j}}xP||}|||} | |kr\P|||||| f\}}}}||||}}q>W|||} t|| ||| |} t||} t| |t| |kr| S| SdS)Nrz$max_denominator should be at least 1)rrrr)r'rrrabs) r-Zmax_denominatorZp0Zq0Zp1Zq1ndr Zq2kZbound1Zbound2rrrlimit_denominators&      zFraction.limit_denominatorcCs|jS)N)r)r rrrr szFraction.numeratorcCs|jS)N)r)r rrrr!szFraction.denominatorcCsd|jj|j|jfS)Nz %s(%s, %s))r0r2rr)r-rrr__repr__szFraction.__repr__cCs(|jdkrt|jSd|j|jfSdS)Nrz%s/%s)rr$r)r-rrr__str__s  zFraction.__str__csTfdd}djd|_j|_fdd}djd|_j|_||fS)NcsPt|ttfr||St|tr0t||St|trHt||StSdS)N)rr rr"complexNotImplemented)r r)fallback_operatormonomorphic_operatorrrforwardvs   z-Fraction._operator_fallbacks..forward__csZt|tjr||St|tjr4t|t|St|tjrRt|t|StSdS)N)rrrZRealr"Complexr>r?)rr )r@rArrreverses    z-Fraction._operator_fallbacks..reverseZ__r)r2__doc__)rAr@rBrEr)r@rAr_operator_fallbacks&sP  zFraction._operator_fallbackscCs,|j|j}}t|j||j|||S)N)r!rr )r rdadbrrr_addsz Fraction._addcCs,|j|j}}t|j||j|||S)N)r!rr )r rrHrIrrr_subsz Fraction._subcCst|j|j|j|jS)N)rr r!)r rrrr_mulsz Fraction._mulcCst|j|j|j|jS)N)rr r!)r rrrr_divs z Fraction._divcCst||S)N)r floor)r rrrr __floordiv__szFraction.__floordiv__cCst||S)N)r rN)rr rrr __rfloordiv__szFraction.__rfloordiv__cCs||}|||S)Nr)r rdivrrr__mod__szFraction.__mod__cCs||}|||S)Nr)rr rQrrr__rmod__szFraction.__rmod__cCst|tjr|jdkr|j}|dkr>t|j||j|ddS|jdkrft|j| |j| ddSt|j | |j | ddSqt|t|Sn t||SdS)NrrF)r) rrrr!r rrrr")r rZpowerrrr__pow__s         zFraction.__pow__cCs\|jdkr|jdkr||jSt|tjrs