%PDF- %PDF-
Direktori : /usr/local/lib/python3.8/__pycache__/ |
Current File : //usr/local/lib/python3.8/__pycache__/fractions.cpython-38.pyc |
U na _ @ s d Z ddlmZ ddlZddlZddlZddlZddlZddgZdd Z dd Z ejjZ ejjZed ejejB ZG d d dejZdS )z+Fraction, infinite-precision, real numbers. DecimalNFractiongcdc C sf ddl }|dtd t| t kr2t|kr\n n&|p<| dk rPt| | S t| |S t| |S )zCalculate the Greatest Common Divisor of a and b. Unless b==0, the result will have the same sign as b (so that when b is divided by it, the result comes out positive). r Nz6fractions.gcd() is deprecated. Use math.gcd() instead. )warningswarnDeprecationWarningtypeintmathr _gcd)abr r %/usr/local/lib/python3.8/fractions.pyr s c C s |r|| | } }q | S Nr r r r r r r s r aC \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 Zd ZdZdZdRdd fddZed d Zedd Zd d Z dSddZ edd Zedd Z dd Zdd Zdd Zdd Zeeej\ZZdd Zeeej\ZZd d! Zeeej\ZZd"d# Zeeej\Z Z!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/ Z0d0d1 Z1d2d3 Z2d4d5 Z3d6d7 Z4d8d9 Z5dTd:d;Z6d<d= Z7d>d? Z8d@dA Z9dBdC Z:dDdE Z;dFdG Z<dHdI Z=dJdK Z>dLdM Z?dNdO Z@dPdQ ZA ZBS )Ur a] This 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_denominatorr NT _normalizec sR t t| | }|dkrdt|tkr6||_d|_|S t|tj rV|j |_|j|_|S t|tt frx| \|_|_|S t|trZt|}|dkrtd| t|dpd}|d}|rt|}nvd}|d}|rdt| }|| t| }||9 }|d } | rBt| } | d kr4|d| 9 }n|d| 9 }|ddkrb| }ntd nft|t krt|krn nn@t|tj rt|tj r|j |j |j |j }}ntd|d krtd| |rBt|t krt|kr(n nt||} |d k r2| } n t||} || }|| }||_||_|S )a Constructs 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) N z Invalid literal for Fraction: %rnum0denomdecimal expr sign-z2argument should be a string or a Rational instancez+both arguments should be Rational instanceszFraction(%s, 0))superr __new__r r r r isinstancenumbersRational numeratordenominatorfloatr as_integer_ratiostr_RATIONAL_FORMATmatch ValueErrorgrouplen TypeErrorZeroDivisionErrorr r r )clsr' r( r selfmr r Zscaler g __class__r r r# T sx $ $ zFraction.__new__c C sD t |tjr| |S t |ts8td| j|t|jf | | S )zConverts a finite float to a rational number, exactly. Beware that Fraction.from_float(0.3) != Fraction(3, 10). z.%s.from_float() only takes floats, not %r (%s))r$ r% Integralr) r1 __name__r r* )r3 fr r r from_float s zFraction.from_floatc C sV ddl m} t|tjr&|t|}n$t||sJtd| j|t|jf | | S )zAConverts a finite Decimal instance to a rational number, exactly.r r z2%s.from_decimal() only takes Decimals, not %r (%s)) r r r$ r% r9 r r1 r: r r* )r3 Zdecr r r r from_decimal s zFraction.from_decimalc C s | j | jfS )zReturn the integer ratio as a tuple. Return a tuple of two integers, whose ratio is equal to the Fraction and with a positive denominator. r r4 r r r r* s zFraction.as_integer_ratio@B c C s |dk rt d| j|kr"t| S d\}}}}| j| j }}|| }||| } | |krZq||||| | f\}}}}|||| }}q<|| | } t|| | || | }t||}t|| t|| kr|S |S dS )aW Closest 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) r z$max_denominator should be at least 1)r r r r N)r. r r r abs) r4 Zmax_denominatorZp0Zq0Zp1Zq1ndr Zq2kZbound1Zbound2r r r limit_denominator s$ zFraction.limit_denominatorc C s | j S r )r r r r r r' s zFraction.numeratorc C s | j S r )r rE r r r r( s zFraction.denominatorc C s d| j j| j| jf S )z repr(self)z %s(%s, %s))r8 r: r r r>