B /g^\c@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.)warningsZwarnZDeprecationWarningtypeintmathr_gcd)abrr !/usr/lib64/python3.7/fractions.pyrs   cCsx|r|||}}qW|SNr r r r r rr 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: %rZnumZ0denomdecimal exprZsignz-z2argument should be a string or a Rational instancez+both arguments should be Rational instanceszFraction(%s, 0))Zsuperr__new__rrrr isinstancenumbersRational numerator denominatorfloatras_integer_ratiostr_RATIONAL_FORMATZmatch ValueErrorZgroupZlen TypeErrorZZeroDivisionErrorr rr ) clsrr rselfZmrrZscalerZg __class__r rrTsr            $   $   zFraction.__new__cCsDt|tjr||St|ts8td|j|t|jf||S)Nz.%s.from_float() only takes floats, not %r (%s))rrIntegralr!r&__name__rr")r'Zfr r r 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)) rrrrr+rr&r,rr")r'Zdecrr r r 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_denominatorZp0Zq0Zp1Zq1ZnZdr Zq2ZkZbound1Zbound2r r rlimit_denominators&      zFraction.limit_denominatorcC|jSr)rr r r rrzFraction.numeratorcCr2r)rr3r r rr r4zFraction.denominatorcCsd|jj|j|jfS)Nz %s(%s, %s))r*r,rrr(r r r__repr__zFraction.__repr__cCs(|jdkrt|jSd|j|jfSdS)Nrz%s/%s)rr#rr5r r r__str__s  zFraction.__str__csTfdd}djd|_j|_fdd}djd|_j|_||fS)NcsPt|ttfr||St|tr0t||St|trHt||StSdSr)rrrr!complexNotImplementedrfallback_operatormonomorphic_operatorr rforwardvs   z-Fraction._operator_fallbacks..forwardZ__csZt|tjr||St|tjr4t|t|St|tjrRt|t|StSdSr)rrrZRealr!Complexr9r:r r r;r rreverses    z-Fraction._operator_fallbacks..reverseZ__r)r,Z__doc__)r=r<r>rAr r;r_operator_fallbacks&sP  zFraction._operator_fallbackscCs,|j|j}}t|j||j|||Srr rrr r ZdaZdbr r r_addz Fraction._addcCs,|j|j}}t|j||j|||SrrCrDr r r_subrFz Fraction._subcCst|j|j|j|jSrrrr rr r r_mulr4z Fraction._mulcCst|j|j|j|jSrrHrr r r_divs z Fraction._divcCst||Srr floorrr r r __floordiv__r4zFraction.__floordiv__cCst||SrrKr@r r r __rfloordiv__r4zFraction.__rfloordiv__cCs||}|||Srr )r r divr r r__mod__r7zFraction.__mod__cCs||}|||Srr )r r rOr r r__rmod__r7zFraction.__rmod__cCst|tjr|jdkr|j}|dkr>t|j||j|ddS|jdkrft|j| |j| ddSt|j | |j | ddSqt|t|Sn t||SdS)NrrFr) rrrr rrrrr!)r r Zpowerr r r__pow__s         zFraction.__pow__cCs\|jdkr|jdkr||jSt|tjrs