eU,SrSSKJr SSKrSSKrSSKrSSKrSSKrSSKrS/r \RRr \RRr\R"SS9S5r\R""S\R$\R&-5rSS jrS r\R""S \R.\R$-5R0r\R""S \R.\R$-5R0r"S S\R65rg)z/Fraction, infinite-precision, rational numbers.DecimalNFractioni@)maxsizec[US[5n[[[U55U-5nUS:aUOU*nUS:XaS$U$![a [ nN'f=f)Nr)pow_PyHASH_MODULUShashabs ValueError _PyHASH_INF) numerator denominatordinvhash_results //opt/imh/python3.13/lib/python3.13/fractions.py_hash_algorithmrse2;O4(T#i.)D011nU5&F22)6)+ sAAAa \A\s* # optional whitespace at the start, (?P[-+]?) # an optional sign, then (?=\d|\.\d) # lookahead for digit or .digit (?P\d*|\d+(_\d+)*) # numerator (possibly empty) (?: # followed by (?:\s*/\s*(?P\d+(_\d+)*))? # an optional denominator | # or (?:\.(?P\d*|\d+(_\d+)*))? # an optional fractional part (?:E(?P[-+]?\d+(_\d+)*))? # and optional exponent ) \s*\Z # and optional whitespace to finish cUS:a USU--nO USU*--n[XS- -U5upEUS:XaUS-S:XaUS-nU(aUS:OUS:nU[U54$)a)Round a rational number to the nearest multiple of a given power of 10. Rounds the rational number n/d to the nearest integer multiple of 10**exponent, rounding to the nearest even integer multiple in the case of a tie. Returns a pair (sign: bool, significand: int) representing the rounded value (-1)**sign * significand * 10**exponent. If no_neg_zero is true, then the returned sign will always be False when the significand is zero. Otherwise, the sign reflects the sign of the input. d must be positive, but n and d need not be relatively prime. r r )divmodr )ndexponent no_neg_zeroqrsigns r_round_to_exponentr"Jsx1} R\ R(] !Av, "DAAv!a%1* R1q5QUD Q<cUS:XaSSSU- 4$[[U55[U5pC[U5[U5- XC:*-nXR- n[XU5upx[[U55US-:Xa US-nUS- nXxU4$)auRound a rational number to a given number of significant figures. Rounds the rational number n/d to the given number of significant figures using the round-ties-to-even rule, and returns a triple (sign: bool, significand: int, exponent: int) representing the rounded value (-1)**sign * significand * 10**exponent. In the special case where n = 0, returns a significand of zero and an exponent of 1 - figures, for compatibility with formatting. Otherwise, the returned significand satisfies 10**(figures - 1) <= significand < 10**figures. d must be positive, but n and d need not be relatively prime. figures must be positive. rFrr)strr lenr") rrfiguresstr_nstr_dmrr! significands r_round_to_figuresr,gs" AvaW$$s1v;A5 E SZ5>2A{H*1:D 3{ ! + A  h &&r#ay (?: (?P.)? (?P[<>=^]) )? (?P[-+ ]?) # Alt flag forces a slash and denominator in the output, even for # integer-valued Fraction objects. (?P\#)? # We don't implement the zeropad flag since there's no single obvious way # to interpret it. (?P0|[1-9][0-9]*)? (?P[,_])? a (?: (?P.)? (?P[<>=^]) )? (?P[-+ ]?) (?Pz)? (?P\#)? # A '0' that's *not* followed by another digit is parsed as a minimum width # rather than a zeropad flag. (?P0(?=[0-9]))? (?P0|[1-9][0-9]*)? (?P[,_])? (?:\.(?P0|[1-9][0-9]*))? (?P[eEfFgG%]) c^\rSrSrSrSrS1U4Sjjr\S5r\S5r \U4Sj5r Sr S r S2S jr \S 5r\S 5rS rSrSrSrSrS3SjrSr\"\\R25urrSr\"\\R:5urrSr \"\ \RB5ur"r#Sr$\"\$\RJ5ur&r'Sr(\"\(\RRS5ur*r+Sr,\"\,\-S5ur.r/Sr0\"\0\RbS5ur2r3Sr4Sr5Sr6Sr7Sr8\Rr4S jr:S!r;S"rS%r?S&r@S'rAS(rBS)rCS*rDS+rES,rFS-rGS.rHS/rIS0rJU=rK$)5ra1This class implements rational numbers. In the two-argument form of the constructor, Fraction(8, 6) will produce a rational number equivalent to 4/3. Both arguments must be Rational. The numerator defaults to 0 and the denominator defaults to 1 so that Fraction(3) == 3 and Fraction() == 0. Fractions can also be constructed from: - numeric strings similar to those accepted by the float constructor (for example, '-2.3' or '1e10') - strings of the form '123/456' - float and Decimal instances - other Rational instances (including integers)  _numerator _denominatorc>[[U] U5nUGc[U5[LaXlSUlU$[U[R5(a$URUlURUlU$[U[[45(aUR5uUlUlU$[U[5(Ga[ R#U5nUc[%SU-5e[ UR'S5=(d S5nUR'S5nU(a [ U5nOSnUR'S5nU(a4UR)SS5nS [+U5-nX-[ U5-nX'-nUR'S 5nU(a#[ U5nUS :a US U--nO US U*--nUR'S 5S :XaU*nO[-S5e[U5[s=La[U5LaO OO{[U[R5(aQ[U[R5(a2URUR-URUR-p!O [-S5eUS :Xa[/SU-5e[0R2"X5n US :aU *n X-nX)-nXlX#lU$)aConstructs a Rational. Takes a string like '3/2' or '1.5', another Rational instance, a numerator/denominator pair, or a float. Examples -------- >>> Fraction(10, -8) Fraction(-5, 4) >>> Fraction(Fraction(1, 7), 5) Fraction(1, 35) >>> Fraction(Fraction(1, 7), Fraction(2, 3)) Fraction(3, 14) >>> Fraction('314') Fraction(314, 1) >>> Fraction('-35/4') Fraction(-35, 4) >>> Fraction('3.1415') # conversion from numeric string Fraction(6283, 2000) >>> Fraction('-47e-2') # string may include a decimal exponent Fraction(-47, 100) >>> Fraction(1.47) # direct construction from float (exact conversion) Fraction(6620291452234629, 4503599627370496) >>> Fraction(2.25) Fraction(9, 4) >>> Fraction(Decimal('1.47')) Fraction(147, 100) rz Invalid literal for Fraction: %rnum0denomdecimal_rexprr!-z2argument should be a string or a Rational instancez+both arguments should be Rational instancesFraction(%s, 0))superr__new__typeintr0r1 isinstancenumbersRationalrrfloatras_integer_ratior%_RATIONAL_FORMATmatchrgroupreplacer& TypeErrorZeroDivisionErrormathgcd) clsrrselfr*r5r6scaler9g __class__s rr=Fraction.__new__s>Xs+C0  I#%"+$%! Iw'7'788"+"5"5$-$9$9! Iw'7885>5O5O5Q2!2 Is++$**959$%G%.&/00 5#6 ("%e*K"#Kggi0G")//#r": "CL 0$-$5G $D #, ''%.C!#h!8%S0I'2t83K776?c)!* I !9::)_ 8tK'8 8 G$4$455 {G$4$4 5 5##k&=&==%% (=(==# 12 2 ! #$5 $AB B HHY , ?A  #' r#c [U[R5(aU"U5$[U[5(d4[ UR <SU<S[ U5R <S35eUR"UR56$)zrConverts a finite float to a rational number, exactly. Beware that Fraction.from_float(0.3) != Fraction(3, 10). z%.from_float() only takes floats, not  ()) r@rAIntegralrCrI__name__r>_from_coprime_intsrD)rMfs r from_floatFraction.from_float4sq a)) * *q6MAu%% \\1d1g.>.>@A A%%q'9'9';<rXrD)rMdecrs r from_decimalFraction.from_decimalBsv $ c7++ , ,#c(#CC))sDI$6$689 9%%s';';'=>>r#cF>[[U] U5nXlX#lU$)zConvert a pair of ints to a rational number, for internal use. The ratio of integers should be in lowest terms and the denominator should be positive. )r<rr=r0r1)rMrrobjrQs rrXFraction._from_coprime_intsNs&Hc*3/"& r#c URS:H$)z*Return True if the Fraction is an integer.rr1rNs r is_integerFraction.is_integerZs  A%%r#c2URUR4$)zReturn a pair of integers, whose ratio is equal to the original Fraction. The ratio is in lowest terms and has a positive denominator. r/res rrDFraction.as_integer_ratio^s !2!233r#cUS:a [S5eURU::a [U5$Sup#pEURURpvXg-nX8U--n X:aOXEX(U--U 4up#pEXvX-- pvM'X- U-n SU-X:U---UR::a[R XE5$[R X*U--X:U--5$)aClosest Fraction to self with denominator at most max_denominator. >>> Fraction('3.141592653589793').limit_denominator(10) Fraction(22, 7) >>> Fraction('3.141592653589793').limit_denominator(100) Fraction(311, 99) >>> Fraction(4321, 8765).limit_denominator(10000) Fraction(4321, 8765) rz$max_denominator should be at least 1)rrrr)rr1rr0rX) rNmax_denominatorp0q0p1q1rraq2ks rlimit_denominatorFraction.limit_denominatores@ Q CD D    /D> !# 1 11AbDB#R"Wb0NBBeq  " $ Q3R4=D-- -..r6 6..rB$wR4@ @r#cUR$N)r0rqs rrFraction.numerators ||r#cUR$rwrdrxs rrFraction.denominators ~~r#cnURR<SUR<SUR<S3$)z repr(self)(z, rU)rQrWr0r1res r__repr__Fraction.__repr__s*#~~66#0A0AC Cr#cURS:Xa[UR5$UR<SUR<3$)z str(self)r/)r1r%r0res r__str__Fraction.__str__s4    !t' '"oot/@/@A Ar#c"US=(d SnUS=(d SnUSS:XaSOUSn[US5n[US =(d S 5nUS =(d SnURURpU S :dU(a[ U5US X3n O[ U5Un US:aSOUn X&[ U 5- [ U 5- -n US:XaX-U -$US:XaX-U -$US:Xa[ U 5S-n U SU U -U -XS-$X-U -$)z}Helper method for __format__. Handles fill, alignment, signs, and thousands separators in the case of no presentation type. fill align>r!r:r8alt minimumwidthr4 thousands_seprrr<^rkN)boolr?r0r1r r&)rNrFrrpos_signalternate_formrrrrbodyr!paddinghalfs r_format_generalFraction._format_generalsAV}#g%#v#-25=eEl+507C8 o.4"  1 11 q5N!fm_-Qq.ABD!fm_-D!esT2SY>? C<>D( ( c\;( ( c\w<1$D5D>D(4/'%.@ @>D( (r#c ^^ US=(d SnUS=(d SnUSS:XaSOUSn[US5n[US 5n[US 5n[US =(d S 5nUS m [US=(d S5n USn U S;=(a U(+n U(+n U S;aSOSn U S;a7U *nU S:XaUS-n[URURX5unnSnU nOkU S;a [ U S5OU S-n[ URURU5unnnU S;=(d US:=(d UU-S:*nU(aUS- OU*nU S:XaSnOU(a U UU-S3nOSnUS US-S3nU(aSOUnUS[U5U- mU[U5U- SnU (aURS 5nU (a U(dSOS nUU-U-nU(a=U[U5- [U5- nTRT (a S!U-S"-S-OU5mT (aJS[T5S- S!--nTSUSRUU 4S#j[U[T5S!555-mTU-nX([U5- [U5- -nUS:XaUU-U-$US$:XaUU-U-$US%:Xa[U5S-nUSUU-U-UUS-$UU-U-$)&z?Helper method for __format__; handles float presentation types.rrrrr!r:r8rrzeropadrr4r precision6presentation_typegGEFGEezfF%%rkFreErz+03drN.c38># UHnTTXS--v M g7f)rN).0posleadingrs r /Fraction._format_float_style.."s&4SC  %!zH C'A $6!2!2H%K !HkJ!I%,Iq!]  /@!2!2G/= +Hk8"T),a<,g%+  (2! yI  #F *+Hy,@+FGFF )a-12 sH23v;233v;245 !((-I$YBC y(61 &T2S]BKmm,9K1$q({G S\A-22Ijy)BGG4 CL!<4-G!T2SY>? C<T>D( ( c\$;( ( c\w<1$D5D>D(4/'$%.@ @'>D( (r#c[U5=n(aURU5$[U5=n(aUSbUScURU5$[ SU<S[ U5R <35e)zAFormat this fraction according to the given format specification.rrzInvalid format specifier z for object of type )%_GENERAL_FORMAT_SPECIFICATION_MATCHERr#_FLOAT_FORMAT_SPECIFICATION_MATCHERrrr>rW)rN format_specrFs r __format__Fraction.__format__5s:+F F5 F''. .7 D D5 DW~%y)9)A//66' 7""&t*"5"5!8 :  r#c^^^UUU4SjnSTR-S-UlTRUlUUU4SjnSTR-S-UlTRUlX44$)a Generates forward and reverse operators given a purely-rational operator and a function from the operator module. Use this like: __op__, __rop__ = _operator_fallbacks(just_rational_op, operator.op) In general, we want to implement the arithmetic operations so that mixed-mode operations either call an implementation whose author knew about the types of both arguments, or convert both to the nearest built in type and do the operation there. In Fraction, that means that we define __add__ and __radd__ as: def __add__(self, other): # Both types have numerators/denominator attributes, # so do the operation directly if isinstance(other, (int, Fraction)): return Fraction(self.numerator * other.denominator + other.numerator * self.denominator, self.denominator * other.denominator) # float and complex don't have those operations, but we # know about those types, so special case them. elif isinstance(other, float): return float(self) + other elif isinstance(other, complex): return complex(self) + other # Let the other type take over. return NotImplemented def __radd__(self, other): # radd handles more types than add because there's # nothing left to fall back to. if isinstance(other, numbers.Rational): return Fraction(self.numerator * other.denominator + other.numerator * self.denominator, self.denominator * other.denominator) elif isinstance(other, Real): return float(other) + float(self) elif isinstance(other, Complex): return complex(other) + complex(self) return NotImplemented There are 5 different cases for a mixed-type addition on Fraction. I'll refer to all of the above code that doesn't refer to Fraction, float, or complex as "boilerplate". 'r' will be an instance of Fraction, which is a subtype of Rational (r : Fraction <: Rational), and b : B <: Complex. The first three involve 'r + b': 1. If B <: Fraction, int, float, or complex, we handle that specially, and all is well. 2. If Fraction falls back to the boilerplate code, and it were to return a value from __add__, we'd miss the possibility that B defines a more intelligent __radd__, so the boilerplate should return NotImplemented from __add__. In particular, we don't handle Rational here, even though we could get an exact answer, in case the other type wants to do something special. 3. If B <: Fraction, Python tries B.__radd__ before Fraction.__add__. This is ok, because it was implemented with knowledge of Fraction, so it can handle those instances before delegating to Real or Complex. The next two situations describe 'b + r'. We assume that b didn't know about Fraction in its implementation, and that it uses similar boilerplate code: 4. If B <: Rational, then __radd_ converts both to the builtin rational type (hey look, that's us) and proceeds. 5. Otherwise, __radd__ tries to find the nearest common base ABC, and fall back to its builtin type. Since this class doesn't subclass a concrete type, there's no implementation to fall back to, so we need to try as hard as possible to return an actual value, or the user will get a TypeError. cB>[U[5(aT"X5$[U[5(aT"U[U55$[U[5(aT"[U5U5$T(a'[U[5(aT"[ U5U5$[ $rw)r@rr?rCcomplexNotImplemented)rqbfallback_operatorhandle_complexmonomorphic_operators rforward-Fraction._operator_fallbacks..forwards}!X&&+A11As##+Ax{;;Au%%(q155Jq'$:$:(Q77%%r#__ch>[U[R5(aT"[U5U5$[U[R5(aT"[ U5[ U55$T(a:[U[R 5(aT"[U5[U55$[$rw) r@rArBrRealrCComplexrr)rrqrrrs rreverse-Fraction._operator_fallbacks..reverses{!W--..+HQK;;Aw||,,(q58<<Jq'//$B$B(WQZ@@%%r#__r)rW__doc__)rrrrrs``` r_operator_fallbacksFraction._operator_fallbacksFsgb & "3"<"<s' amm+1LMMr#FcURURp2[URU-X!R-5upEU[XRU-54$)z(a // b, a % b))rrrr)rqrrrdivn_mods r_divmodFraction._divmodDsC BAKK",b;;.>? HUG,,,r#cURURp2[URU-URU--X#-5$)za % b)rrr)rqrrrs r_mod Fraction._modLs7 Br)akkB.>?IIr#c[U[R5(Ga URS:XaURnUS:a0[ R URU-URU-5$URS:a2[ R URU*-URU*-5$URS:Xa[SURU*--5e[ R UR*U*-UR*U*-5$[U5[U5-$[U[[45(a[U5U-$[$)za ** b If b is not an integer, the result will be a float or complex since roots are generally irrational. If b is an integer, the result will be rational. rrr;) r@rArBrrrrXr0r1rJrCrr)rqrpowers r__pow__Fraction.__pow__Ss> a)) * *}}! A:#66q||u7L78~~7NPP\\A%#66q~~%7O78||v7MOO\\Q&+,=,-NNuf,D-EFF$66UF7R9: 5&7PRR Qx58++ E7+ , ,8q= ! !r#c:URS:XaURS:aXR-$[U[R5(a#[ UR UR5U-$URS:XaXR-$U[U5-$)za ** brr) r1r0r@rArBrrrrC)rrqs r__rpow__Fraction.__rpow__ssz >>Q 1<<1#4 $ $ a)) * *AKK71< < >>Q  $ $E!H}r#cV[RURUR5$)z++a: Coerces a subclass instance to FractionrrXr0r1rxs r__pos__Fraction.__pos__s**1<<HHr#cX[RUR*UR5$)z-arrxs r__neg__Fraction.__neg__s**ALL=!..IIr#ch[R[UR5UR5$)zabs(a))rrXr r0r1rxs r__abs__Fraction.__abs__s"**3q||+>1 1r#c4URUR-$)z math.floor(a)r/rxs r __floor__Fraction.__floor__s||q~~--r#c8UR*UR-*$)z math.ceil(a)r/rxs r__ceil__Fraction.__ceil__s,,!..011r#c*UcMURn[URU5up4US-U:aU$US-U:aUS-$US-S:XaU$US-$S[U5-nUS:a[ [ X-5U5$[ [ X- 5U-5$)z/round(self, ndigits) Rounds half toward even. rkrrr)r1rr0r rround)rNndigitsrfloor remaindershifts r __round__Fraction.__round__s ?!!A%dooq9 E1}q  Q"qy a qy CL  Q;E$,/7 7E$,/%78 8r#cB[URUR5$)z hash(self))rr0r1res r__hash__Fraction.__hash__st0A0ABBr#ch[U5[La%URU:H=(a URS:H$[ U[ R 5(a9URUR:H=(a URUR:H$[ U[ R5(aURS:Xa URn[ U[5(aN[R"U5(d[R"U5(aSU:H$XR!U5:H$["$)za == brr)r>r?r0r1r@rArBrrrimagrealrCrKisnanisinfrZrrs r__eq__Fraction.__eq__s 7c><<1$<1)< < a)) * *LLAKK/4NNamm3 5 a ) )affkA a  zz!}} 1 axLLO++" !r#c[U[R5(a7U"URUR-UR UR -5$[U[5(aV[R"U5(d[R"U5(a U"SU5$U"XRU55$[$)a3Helper for comparison operators, for internal use only. Implement comparison between a Rational instance `self`, and either another Rational instance or a float `other`. If `other` is not a Rational instance or a float, return NotImplemented. `op` should be one of the six standard comparison operators. r') r@rArBr0rr1rrCrKr*r+rZr)rNotherops r_richcmpFraction._richcmps eW-- . .doo(9(99''%//9; ; eU # #zz%  DJJu$5$5#u~%$ 677! !r#cBURU[R5$)za < b)r1operatorltrs r__lt__Fraction.__lt__zz!X[[))r#cBURU[R5$)za > b)r1r4gtrs r__gt__Fraction.__gt__r8r#cBURU[R5$)za <= b)r1r4lers r__le__Fraction.__le__r8r#cBURU[R5$)za >= b)r1r4gers r__ge__Fraction.__ge__r8r#c,[UR5$)za != 0)rr0rxs r__bool__Fraction.__bool__sALL!!r#cJURURUR44$rw)rQr0r1res r __reduce__Fraction.__reduce__s $2C2C DEEr#cx[U5[:XaU$URURUR5$rwr>rrQr0r1res r__copy__Fraction.__copy__. : !K~~doot/@/@AAr#cx[U5[:XaU$URURUR5$rwrL)rNmemos r __deepcopy__Fraction.__deepcopy__ rOr#)r1r0)rN)i@B)Trw)LrW __module__ __qualname____firstlineno__r __slots__r= classmethodrZr^rXrfrDrtpropertyrrr~rrrrrrr4add__add____radd__rsub__sub____rsub__rmul__mul____rmul__rtruediv __truediv__ __rtruediv__rfloordiv __floordiv__ __rfloordiv__rr __divmod__ __rdivmod__rmod__mod____rmod__rrrr r indexrrrrr!r$r,r1r6r;r?rCrFrIrMrR__static_attributes__ __classcell__)rQs@rrrs(/IgR = = ? ?  &47ArC B )D_)B $,0l d D,D(,,?GX D,D(,,?GX =,D(,,?GX1(!4D(:J:J KKN#6iARARTY"ZL-- 2'65IJ J ,D(,,FGX"@ IJN#..:2.2 94C"*",****"FB BBr#)F)rr6r functoolsrKrAr4resys__all__ hash_infomodulusr infr lru_cachercompileVERBOSE IGNORECASErEr"r,DOTALL fullmatchrrrBrrr#rr~s6  , --''mm  w'*(*@:: ZZ"-- !":$'P)+ 4YY )'Y&$')jj2YY''Y$$] Bw] Br#