grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026B ÂlèôjCTßã%@sºdZddddddddd d d d d ddddddddddddddddddd d!d"d#d$d%g%ZeZd&Zd'Zd(Zd)d*lZd)d*lZ d)d*l Z yd)d+l m Z e dd,ƒZWnek r°d-d.„ZYnXdZdZdZdZdZdZdZdZd/Zd/Ze jd0kròd1Zd1Zd2Zn d3Zd3Zd4Zeed5ZGd6d„deƒZ Gd7d„de ƒZ!Gd8d „d e ƒZ"Gd9d„de"ƒZ#Gd:d „d e e$ƒZ%Gd;d„de"ƒZ&Gdd„de"ƒZ)Gd?d „d e ƒZ*Gd@d „d e ƒZ+GdAd„de(e*ƒZ,GdBd„de(e*e+ƒZ-GdCd„de e.ƒZ/e!e%e(e,e*e-e"e+e/g Z0e#e"e&e"e'e"e)e"iZ1eeeeeeeefZ2d)d*l3Z3e3 4dD¡Z5dEd„Z6dFd„Z7[3d•dGd„Z8GdHd„de9ƒZ:d–dJdK„Z;e j< =e:¡GdLdM„dMe9ƒZ>GdNd„de9ƒZ?GdOdP„dPe9ƒZ@d—dQdR„ZAeBjCZDdSdT„ZEdUdV„ZFdWdX„ZGdYdZ„ZHd˜d\d]„ZId^d_„ZJd`da„ZKGdbdc„dce9ƒZLeLƒjMZNd™ddde„ZOdfdg„ZPdhdi„ZQdjdkdldmdndodpdqdrdsœ fdtdu„ZRdšdvdw„ZSd›dxdy„ZTe?dzee%e,e"ggd{d|d5d)d}�ZUe?d~ee%e,e"e!e-ggd�ZVe?d~eggd�ZWd)d*lXZXeX Yd€eXjZeXj[B¡j\Z]eX Yd�¡j\Z^eX Yd‚¡j\Z_eX YdƒeXjZeXj`B¡Za[Xy d)d*lbZcWnek �rYnXdœd„d…„Zdd†d‡„Zedˆd‰„Zfd�dŠd‹„ZgdŒd�„ZhdŽd�„Zie:d�ƒZje:d‘ƒZke:d’ƒZle:d)ƒZme:d5ƒZne:d“ƒZoejekfZpe jqjrZse jqjtZue jqjvZwexdqesd”esƒZy[ d*S)ža¿ This is an implementation of decimal floating point arithmetic based on the General Decimal Arithmetic Specification: http://speleotrove.com/decimal/decarith.html and IEEE standard 854-1987: http://en.wikipedia.org/wiki/IEEE_854-1987 Decimal floating point has finite precision with arbitrarily large bounds. The purpose of this module is to support arithmetic using familiar "schoolhouse" rules and to avoid some of the tricky representation issues associated with binary floating point. The package is especially useful for financial applications or for contexts where users have expectations that are at odds with binary floating point (for instance, in binary floating point, 1.00 % 0.1 gives 0.09999999999999995 instead of 0.0; Decimal('1.00') % Decimal('0.1') returns the expected Decimal('0.00')). Here are some examples of using the decimal module: >>> from decimal import * >>> setcontext(ExtendedContext) >>> Decimal(0) Decimal('0') >>> Decimal('1') Decimal('1') >>> Decimal('-.0123') Decimal('-0.0123') >>> Decimal(123456) Decimal('123456') >>> Decimal('123.45e12345678') Decimal('1.2345E+12345680') >>> Decimal('1.33') + Decimal('1.27') Decimal('2.60') >>> Decimal('12.34') + Decimal('3.87') - Decimal('18.41') Decimal('-2.20') >>> dig = Decimal(1) >>> print(dig / Decimal(3)) 0.333333333 >>> getcontext().prec = 18 >>> print(dig / Decimal(3)) 0.333333333333333333 >>> print(dig.sqrt()) 1 >>> print(Decimal(3).sqrt()) 1.73205080756887729 >>> print(Decimal(3) ** 123) 4.85192780976896427E+58 >>> inf = Decimal(1) / Decimal(0) >>> print(inf) Infinity >>> neginf = Decimal(-1) / Decimal(0) >>> print(neginf) -Infinity >>> print(neginf + inf) NaN >>> print(neginf * inf) -Infinity >>> print(dig / 0) Infinity >>> getcontext().traps[DivisionByZero] = 1 >>> print(dig / 0) Traceback (most recent call last): ... ... ... decimal.DivisionByZero: x / 0 >>> c = Context() >>> c.traps[InvalidOperation] = 0 >>> print(c.flags[InvalidOperation]) 0 >>> c.divide(Decimal(0), Decimal(0)) Decimal('NaN') >>> c.traps[InvalidOperation] = 1 >>> print(c.flags[InvalidOperation]) 1 >>> c.flags[InvalidOperation] = 0 >>> print(c.flags[InvalidOperation]) 0 >>> print(c.divide(Decimal(0), Decimal(0))) Traceback (most recent call last): ... ... ... decimal.InvalidOperation: 0 / 0 >>> print(c.flags[InvalidOperation]) 1 >>> c.flags[InvalidOperation] = 0 >>> c.traps[InvalidOperation] = 0 >>> print(c.divide(Decimal(0), Decimal(0))) NaN >>> print(c.flags[InvalidOperation]) 1 >>> ÚDecimalÚContextÚ DecimalTupleÚDefaultContextÚ BasicContextÚExtendedContextÚDecimalExceptionÚClampedÚInvalidOperationÚDivisionByZeroÚInexactÚRoundedÚ SubnormalÚOverflowÚ UnderflowÚFloatOperationÚDivisionImpossibleÚInvalidContextÚConversionSyntaxÚDivisionUndefinedÚ ROUND_DOWNÚ ROUND_HALF_UPÚROUND_HALF_EVENÚ ROUND_CEILINGÚ ROUND_FLOORÚROUND_UPÚROUND_HALF_DOWNÚ ROUND_05UPÚ setcontextÚ getcontextÚ localcontextÚMAX_PRECÚMAX_EMAXÚMIN_EMINÚ MIN_ETINYÚ HAVE_THREADSÚHAVE_CONTEXTVARZdecimalz1.70z2.4.2éN)Ú namedtuplezsign digits exponentcGs|S)N©)Úargsr(r(ú//opt/alt/python37/lib64/python3.7/_pydecimal.pyÚ¤ór+TlÿÿÿÿlÿÇNÎZolüÿÿÿÿÇNÎZoi@üTiÀ«æéc@seZdZdZdd„ZdS)ra1Base exception class. Used exceptions derive from this. If an exception derives from another exception besides this (such as Underflow (Inexact, Rounded, Subnormal) that indicates that it is only called if the others are present. This isn't actually used for anything, though. handle -- Called when context._raise_error is called and the trap_enabler is not set. First argument is self, second is the context. More arguments can be given, those being after the explanation in _raise_error (For example, context._raise_error(NewError, '(-x)!', self._sign) would call NewError().handle(context, self._sign).) To define a new exception, it should be sufficient to have it derive from DecimalException. cGsdS)Nr()ÚselfÚcontextr)r(r(r*ÚhandleÓszDecimalException.handleN)Ú__name__Ú __module__Ú __qualname__Ú__doc__r0r(r(r(r*rÀsc@seZdZdZdS)ra)Exponent of a 0 changed to fit bounds. This occurs and signals clamped if the exponent of a result has been altered in order to fit the constraints of a specific concrete representation. This may occur when the exponent of a zero result would be outside the bounds of a representation, or when a large normal number would have an encoded exponent that cannot be represented. In this latter case, the exponent is reduced to fit and the corresponding number of zero digits are appended to the coefficient ("fold-down"). N)r1r2r3r4r(r(r(r*r×s c@seZdZdZdd„ZdS)r a0An invalid operation was performed. Various bad things cause this: Something creates a signaling NaN -INF + INF 0 * (+-)INF (+-)INF / (+-)INF x % 0 (+-)INF % x x._rescale( non-integer ) sqrt(-x) , x > 0 0 ** 0 x ** (non-integer) x ** (+-)INF An operand is invalid The result of the operation after these is a quiet positive NaN, except when the cause is a signaling NaN, in which case the result is also a quiet NaN, but with the original sign, and an optional diagnostic information. cGs,|r(t|dj|djddƒ}| |¡StS)Nr&ÚnT)Ú_dec_from_tripleÚ_signÚ_intÚ_fix_nanÚ_NaN)r.r/r)Úansr(r(r*r0ús zInvalidOperation.handleN)r1r2r3r4r0r(r(r(r*r ãsc@seZdZdZdd„ZdS)rzÜTrying to convert badly formed string. This occurs and signals invalid-operation if a string is being converted to a number and it does not conform to the numeric string syntax. The result is [0,qNaN]. cGstS)N)r:)r.r/r)r(r(r*r0szConversionSyntax.handleN)r1r2r3r4r0r(r(r(r*rsc@seZdZdZdd„ZdS)r a²Division by 0. This occurs and signals division-by-zero if division of a finite number by zero was attempted (during a divide-integer or divide operation, or a power operation with negative right-hand operand), and the dividend was not zero. The result of the operation is [sign,inf], where sign is the exclusive or of the signs of the operands for divide, or is 1 for an odd power of -0, for power. cGst|S)N)Ú_SignedInfinity)r.r/Úsignr)r(r(r*r0szDivisionByZero.handleN)r1r2r3r4r0r(r(r(r*r s c@seZdZdZdd„ZdS)rzóCannot perform the division adequately. This occurs and signals invalid-operation if the integer result of a divide-integer or remainder operation had too many digits (would be longer than precision). The result is [0,qNaN]. cGstS)N)r:)r.r/r)r(r(r*r0"szDivisionImpossible.handleN)r1r2r3r4r0r(r(r(r*rsc@seZdZdZdd„ZdS)rzîUndefined result of division. This occurs and signals invalid-operation if division by zero was attempted (during a divide-integer, divide, or remainder operation), and the dividend is also zero. The result is [0,qNaN]. cGstS)N)r:)r.r/r)r(r(r*r0-szDivisionUndefined.handleN)r1r2r3r4r0r(r(r(r*r%sc@seZdZdZdS)r a­Had to round, losing information. This occurs and signals inexact whenever the result of an operation is not exact (that is, it needed to be rounded and any discarded digits were non-zero), or if an overflow or underflow condition occurs. The result in all cases is unchanged. The inexact signal may be tested (or trapped) to determine if a given operation (or sequence of operations) was inexact. N)r1r2r3r4r(r(r(r*r 0s c@seZdZdZdd„ZdS)raìInvalid context. Unknown rounding, for example. This occurs and signals invalid-operation if an invalid context was detected during an operation. This can occur if contexts are not checked on creation and either the precision exceeds the capability of the underlying concrete representation or an unknown or unsupported rounding was specified. These aspects of the context need only be checked when the values are required to be used. The result is [0,qNaN]. cGstS)N)r:)r.r/r)r(r(r*r0GszInvalidContext.handleN)r1r2r3r4r0r(r(r(r*r<s c@seZdZdZdS)r aÙNumber got rounded (not necessarily changed during rounding). This occurs and signals rounded whenever the result of an operation is rounded (that is, some zero or non-zero digits were discarded from the coefficient), or if an overflow or underflow condition occurs. The result in all cases is unchanged. The rounded signal may be tested (or trapped) to determine if a given operation (or sequence of operations) caused a loss of precision. N)r1r2r3r4r(r(r(r*r Js c@seZdZdZdS)r a˜Exponent < Emin before rounding. This occurs and signals subnormal whenever the result of a conversion or operation is subnormal (that is, its adjusted exponent is less than Emin, before any rounding). The result in all cases is unchanged. The subnormal signal may be tested (or trapped) to determine if a given or operation (or sequence of operations) yielded a subnormal result. N)r1r2r3r4r(r(r(r*r Vs c@seZdZdZdd„ZdS)raNumerical overflow. This occurs and signals overflow if the adjusted exponent of a result (from a conversion or from an operation that is not an attempt to divide by zero), after rounding, would be greater than the largest value that can be handled by the implementation (the value Emax). The result depends on the rounding mode: For round-half-up and round-half-even (and for round-half-down and round-up, if implemented), the result of the operation is [sign,inf], where sign is the sign of the intermediate result. For round-down, the result is the largest finite number that can be represented in the current precision, with the sign of the intermediate result. For round-ceiling, the result is the same as for round-down if the sign of the intermediate result is 1, or is [0,inf] otherwise. For round-floor, the result is the same as for round-down if the sign of the intermediate result is 0, or is [1,inf] otherwise. In all cases, Inexact and Rounded will also be raised. cGsŽ|jttttfkrt|S|dkrR|jtkr4t|St|d|j|j |jdƒS|dkrŠ|jt krlt|St|d|j|j |jdƒSdS)Nr&Ú9r-) Úroundingrrrrr<rr6ÚprecÚEmaxr)r.r/r=r)r(r(r*r0ws     zOverflow.handleN)r1r2r3r4r0r(r(r(r*rasc@seZdZdZdS)raxNumerical underflow with result rounded to 0. This occurs and signals underflow if a result is inexact and the adjusted exponent of the result would be smaller (more negative) than the smallest value that can be handled by the implementation (the value Emin). That is, the result is both inexact and subnormal. The result after an underflow will be a subnormal number rounded, if necessary, so that its exponent is not less than Etiny. This may result in 0 with the sign of the intermediate result and an exponent of Etiny. In all cases, Inexact, Rounded, and Subnormal will also be raised. N)r1r2r3r4r(r(r(r*r‡s c@seZdZdZdS)raœEnable stricter semantics for mixing floats and Decimals. If the signal is not trapped (default), mixing floats and Decimals is permitted in the Decimal() constructor, context.create_decimal() and all comparison operators. Both conversion and comparisons are exact. Any occurrence of a mixed operation is silently recorded by setting FloatOperation in the context flags. Explicit conversions with Decimal.from_float() or context.create_decimal_from_float() do not set the flag. Otherwise (the signal is trapped), only equality comparisons and explicit conversions are silent. All other mixed operations raise FloatOperation. N)r1r2r3r4r(r(r(r*r–s Zdecimal_contextcCs2yt ¡Stk r,tƒ}t |¡|SXdS)z½Returns this thread's context. If this thread does not yet have a context, returns a new context and sets this thread's context. New contexts are copies of DefaultContext. N)Ú_current_context_varÚgetÚ LookupErrorrÚset)r/r(r(r*r¼s  cCs,|tttfkr| ¡}| ¡t |¡dS)z%Set this thread's context to context.N)rrrÚcopyÚ clear_flagsrBrE)r/r(r(r*rÊscCs|dkrtƒ}t|ƒS)abReturn a context manager for a copy of the supplied context Uses a copy of the current context if no context is specified The returned context manager creates a local decimal context in a with statement: def sin(x): with localcontext() as ctx: ctx.prec += 2 # Rest of sin calculation algorithm # uses a precision 2 greater than normal return +s # Convert result to normal precision def sin(x): with localcontext(ExtendedContext): # Rest of sin calculation algorithm # uses the Extended Context from the # General Decimal Arithmetic Specification return +s # Convert result to normal context >>> setcontext(DefaultContext) >>> print(getcontext().prec) 28 >>> with localcontext(): ... ctx = getcontext() ... ctx.prec += 2 ... print(ctx.prec) ... 30 >>> with localcontext(ExtendedContext): ... print(getcontext().prec) ... 9 >>> print(getcontext().prec) 28 N)rÚ_ContextManager)Zctxr(r(r*rÓs$c @sÐeZdZdZdZdòdd„Zedd„ƒZd d „Zd d „Z dód d„Z dd„Z dd„Z dd„Z dôdd„Zdõdd„Zdödd„Zd÷dd„Zdødd„Zdùdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zdúd*d+„Zdûd,d-„Zdüd.d/„Zdýd0d1„Zdþd3d4„Zdÿd5d6„ZeZ�dd7d8„Z�dd9d:„Z �dd;d<„Z!e!Z"�dd=d>„Z#d?d@„Z$�ddAdB„Z%�ddCdD„Z&�ddEdF„Z'�ddGdH„Z(�ddIdJ„Z)�d dKdL„Z*�d dMdN„Z+�d dOdP„Z,dQdR„Z-dSdT„Z.e.Z/e0dUdV„ƒZ1e0dWdX„ƒZ2dYdZ„Z3d[d\„Z4d]d^„Z5d_d`„Z6dadb„Z7dcdd„Z8dedf„Z9dgdh„Z:didj„Z;dkdl„Ze?e7e8e9e:e;edq�Z@�d drds„ZAdtdu„ZBdvdw„ZC�d dxdy„ZD�ddzd{„ZEd|d}„ZF�dd~d„ZG�dd€d�„ZH�dd‚dƒ„ZI�dd„d…„ZJ�dd†d‡„ZKdˆd‰„ZLdŠd‹„ZM�ddŒd�„ZN�ddŽd�„ZOeOZP�dd�d‘„ZQ�dd’d“„ZR�dd”d•„ZSd–d—„ZTd˜d™„ZUdšd›„ZVdœd�„ZW�ddždŸ„ZX�dd d¡„ZY�dd¢d£„ZZd¤d¥„Z[d¦d§„Z\�dd¨d©„Z]�ddªd«„Z^d¬d­„Z_d®d¯„Z`d°d±„Zad²d³„Zb�dd´dµ„Zcd¶d·„Zdd¸d¹„Zedºd»„Zf�dd¼d½„Zgd¾d¿„ZhdÀdÁ„Zi�d dÂdÄZjdÄdÅ„Zk�d!dÆdÇ„Zl�d"dÈdÉ„ZmdÊdË„ZndÌdÍ„Zo�d#dÎdÏ„Zp�d$dÐdÑ„Zq�d%dÒdÓ„Zr�d&dÔdÕ„Zs�d'dÖdׄZt�d(dØdÙ„Zu�d)dÚdÛ„Zv�d*dÜdÝ„Zw�d+dÞdß„Zx�d,dàdá„Zydâdã„Zz�d-dädå„Z{�d.dædç„Z|�d/dèdé„Z}dêdë„Z~dìdí„Zdîdï„Z€�d0dðdñ„Z�dS(1rz,Floating point class for decimal arithmetic.)Ú_expr8r7Ú _is_specialÚ0Nc CsŒt |¡}t|tƒ�r$t| ¡ dd¡ƒ}|dkrP|dkr@tƒ}| t d|¡S|  d¡dkrfd|_ nd|_ |  d ¡}|dk rÆ|  d ¡pŠd}t |  d ¡pšd ƒ}tt ||ƒƒ|_ |t|ƒ|_d |_nZ|  d¡}|dk �rtt |päd ƒƒ d ¡|_ |  d¡�rd|_nd|_n d |_ d|_d|_|St|t ƒ�rf|dk�rBd|_ nd|_ d|_tt|ƒƒ|_ d |_|St|tƒ�r–|j|_|j |_ |j |_ |j|_|St|tƒ�rÌ|j|_ t|j ƒ|_ t |jƒ|_d |_|St|ttfƒ�r&t|ƒdk�ròtdƒ‚t|dt ƒ�r|ddk�stdƒ‚|d|_ |ddk�rHd |_ |d|_d|_nÚg} x^|dD]R} t| t ƒ�ržd| k�r~dk�ržnn| �s’| dk�r¦|  | ¡ntdƒ‚�qVW|ddk�rÞd tt| ƒ¡|_ |d|_d|_nDt|dt ƒ�rd tt| �pdgƒ¡|_ |d|_d |_ntdƒ‚|St|tƒ�r||dk�rBtƒ}| td¡t |¡}|j|_|j |_ |j |_ |j|_|St d|ƒ‚dS)aêCreate a decimal point instance. >>> Decimal('3.14') # string input Decimal('3.14') >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent) Decimal('3.14') >>> Decimal(314) # int Decimal('314') >>> Decimal(Decimal(314)) # another decimal instance Decimal('314') >>> Decimal(' 3.14 \n') # leading and trailing whitespace okay Decimal('3.14') Ú_ÚNzInvalid literal for Decimal: %rr=ú-r-r&ÚintZfracÚexprKFÚdiagÚsignalÚNr5ÚFTéztInvalid tuple size in creation of Decimal from list or tuple. The list or tuple should have exactly three elements.)r&r-z|Invalid sign. The first value in the tuple should be an integer; either 0 for a positive number or 1 for a negative number.éé zTThe second value in the tuple must be composed of integers in the range 0 through 9.)r5rSzUThe third value in the tuple must be an integer, or one of the strings 'F', 'n', 'N'.z;strict semantics for mixing floats and Decimals are enabledzCannot convert %r to Decimal)!ÚobjectÚ__new__Ú isinstanceÚstrÚ_parserÚstripÚreplacerÚ _raise_errorrÚgroupr7rOr8ÚlenrIrJÚlstripÚabsrÚ_WorkRepr=rPÚlistÚtupleÚ ValueErrorÚappendÚjoinÚmapÚfloatrÚ from_floatÚ TypeError) ÚclsÚvaluer/r.ÚmÚintpartÚfracpartrPrQÚdigitsZdigitr(r(r*rY s¬               (      zDecimal.__new__cCsÌt|tƒr,|dkrdnd}d}tt|ƒƒ}nzt|tƒržt |¡sJt |¡rV|t|ƒƒSt  d|¡dkrld}nd}t|ƒ  ¡\}}|  ¡d}t|d|ƒ}nt dƒ‚t ||| ƒ}|tkrÀ|S||ƒSdS)a.Converts a float to a decimal number, exactly. Note that Decimal.from_float(0.1) is not the same as Decimal('0.1'). Since 0.1 is not exactly representable in binary floating point, the value is stored as the nearest representable value which is 0x1.999999999999ap-4. The exact equivalent of the value in decimal is 0.1000000000000000055511151231257827021181583404541015625. >>> Decimal.from_float(0.1) Decimal('0.1000000000000000055511151231257827021181583404541015625') >>> Decimal.from_float(float('nan')) Decimal('NaN') >>> Decimal.from_float(float('inf')) Decimal('Infinity') >>> Decimal.from_float(-float('inf')) Decimal('-Infinity') >>> Decimal.from_float(-0.0) Decimal('-0') r&r-gð?ézargument must be int or float.N)rZrOr[rcrkÚ_mathZisinfZisnanÚreprZcopysignÚas_integer_ratioÚ bit_lengthrmr6r)rnÚfr=ÚkÚcoeffr5ÚdÚresultr(r(r*rl s$    zDecimal.from_floatcCs(|jr$|j}|dkrdS|dkr$dSdS)zrReturns whether the number is not actually one. 0 if a number 1 if NaN 2 if sNaN r5r-rSrVr&)rJrI)r.rPr(r(r*Ú_isnanÍszDecimal._isnancCs|jdkr|jrdSdSdS)zyReturns whether the number is infinite 0 if finite or not a number 1 if +INF -1 if -INF rTéÿÿÿÿr-r&)rIr7)r.r(r(r*Ú _isinfinityÜs  zDecimal._isinfinitycCs|| ¡}|dkrd}n| ¡}|s&|rx|dkr4tƒ}|dkrJ| td|¡S|dkr`| td|¡S|rn| |¡S| |¡SdS)z½Returns whether the number is not actually one. if self, other are sNaN, signal if self, other are NaN return nan return 0 Done before operations. NFrVÚsNaNr&)r~rr_r r9)r.Úotherr/Ú self_is_nanÚ other_is_nanr(r(r*Ú _check_nansés"   zDecimal._check_nanscCsv|dkrtƒ}|js|jrr| ¡r0| td|¡S| ¡rF| td|¡S| ¡r\| td|¡S| ¡rr| td|¡SdS)aCVersion of _check_nans used for the signaling comparisons compare_signal, __le__, __lt__, __ge__, __gt__. Signal InvalidOperation if either self or other is a (quiet or signaling) NaN. Signaling NaNs take precedence over quiet NaNs. Return 0 if neither operand is a NaN. Nzcomparison involving sNaNzcomparison involving NaNr&)rrJÚis_snanr_r Úis_qnan)r.r‚r/r(r(r*Ú_compare_check_nans s(  zDecimal._compare_check_nanscCs|jp|jdkS)zuReturn True if self is nonzero; otherwise return False. NaNs and infinities are considered nonzero. rK)rJr8)r.r(r(r*Ú__bool__*szDecimal.__bool__cCs|js |jr8| ¡}| ¡}||kr(dS||kr4dSdS|sP|sDdSd|j S|s^d|jS|j|jkrndS|j|jkr~dS| ¡}| ¡}||krî|jd|j|j}|jd|j|j}||krÎdS||krâd|j Sd|jSn ||k�rd|jSd|j SdS)z¸Compare the two non-NaN decimal instances self and other. Returns -1 if self < other, 0 if self == other and 1 if self > other. This routine is for internal use only.r&rr-rKN)rJr€r7Úadjustedr8rI)r.r‚Zself_infZ other_infÚ self_adjustedZother_adjustedÚ self_paddedZ other_paddedr(r(r*Ú_cmp1s>         z Decimal._cmpcCs<t||dd�\}}|tkr|S| ||¡r.dS| |¡dkS)NT)Ú equality_opFr&)Ú_convert_for_comparisonÚNotImplementedr…r�)r.r‚r/r(r(r*Ú__eq__qs  zDecimal.__eq__cCs<t||ƒ\}}|tkr|S| ||¡}|r.dS| |¡dkS)NFr&)r�r�rˆr�)r.r‚r/r;r(r(r*Ú__lt__ys zDecimal.__lt__cCs<t||ƒ\}}|tkr|S| ||¡}|r.dS| |¡dkS)NFr&)r�r�rˆr�)r.r‚r/r;r(r(r*Ú__le__‚s zDecimal.__le__cCs<t||ƒ\}}|tkr|S| ||¡}|r.dS| |¡dkS)NFr&)r�r�rˆr�)r.r‚r/r;r(r(r*Ú__gt__‹s zDecimal.__gt__cCs<t||ƒ\}}|tkr|S| ||¡}|r.dS| |¡dkS)NFr&)r�r�rˆr�)r.r‚r/r;r(r(r*Ú__ge__”s zDecimal.__ge__cCs>t|dd�}|js|r0|jr0| ||¡}|r0|St| |¡ƒS)zàCompare self to other. Return a decimal value: a or b is a NaN ==> Decimal('NaN') a < b ==> Decimal('-1') a == b ==> Decimal('0') a > b ==> Decimal('1') T)Úraiseit)Ú_convert_otherrJr…rr�)r.r‚r/r;r(r(r*Úcompare�s   zDecimal.comparecCs’|jr4| ¡rtdƒ‚n| ¡r$tS|jr0t StS|jdkrNtd|jt ƒ}ntt |j t ƒ}t |j ƒ|t }|dkr||n| }|dkrŽdS|S)zx.__hash__() <==> hash(x)z"Cannot hash a signaling NaN value.r&é réþÿÿÿ) rJr†rmÚis_nanÚ _PyHASH_NANr7Ú _PyHASH_INFrIÚpowÚ_PyHASH_MODULUSÚ _PyHASH_10INVrOr8)r.Zexp_hashZhash_r;r(r(r*Ú__hash__¯s  zDecimal.__hash__cCst|jttt|jƒƒ|jƒS)zeRepresents the number as a triple tuple. To show the internals exactly as they are. )rr7rfrjrOr8rI)r.r(r(r*Úas_tupleÉszDecimal.as_tuplecCsØ|jr | ¡rtdƒ‚ntdƒ‚|s(dSt|jƒ}|jdkrR|d|jd}}nr|j }x(|dkr‚|ddkr‚|d}|d8}q\W|j }t|| @ ¡d|ƒ}|r¸||L}||8}d||>}|j rÐ| }||fS)a�Express a finite Decimal instance in the form n / d. Returns a pair (n, d) of integers. When called on an infinity or NaN, raises OverflowError or ValueError respectively. >>> Decimal('3.14').as_integer_ratio() (157, 50) >>> Decimal('-123e5').as_integer_ratio() (-12300000, 1) >>> Decimal('0.00').as_integer_ratio() (0, 1) z#cannot convert NaN to integer ratioz(cannot convert Infinity to integer ratio)r&r-r&r™r-rt) rJr›rgÚ OverflowErrorrOr8rIÚminrxr7)r.r5r|Zd5Zd2Zshift2r(r(r*rwÐs,     zDecimal.as_integer_ratiocCs dt|ƒS)z0Represents the number as an instance of Decimal.z Decimal('%s'))r[)r.r(r(r*Ú__repr__szDecimal.__repr__Fc Csbddg|j}|jrL|jdkr&|dS|jdkr>|d|jS|d|jS|jt|jƒ}|jdkrt|d krt|}n6|s~d }n,|jd krš|d d d }n|d d d }|dkrÌd }d d | |j}nL|t|jƒkrø|jd |t|jƒ}d}n |jd|…}d |j|d…}||k�r(d}n*|dk�r8tƒ}ddg|jd||}||||S)z–Return string representation of the number in scientific notation. Captures all of the information in the underlying representation. rMrNrTZInfinityr5ÚNaNr�r&iúÿÿÿr-rKrUÚ.NÚeÚEz%+d)r7rJrIr8rarÚcapitals) r.Úengr/r=Ú leftdigitsÚdotplacerqrrrPr(r(r*Ú__str__s:     zDecimal.__str__cCs|jd|d�S)a,Convert to a string, using engineering notation if an exponent is needed. Engineering notation has an exponent which is a multiple of 3. This can leave up to 3 digits to the left of the decimal place and may require the addition of either one or two trailing zeros. T)r«r/)r®)r.r/r(r(r*Ú to_eng_string;szDecimal.to_eng_stringcCsR|jr|j|d�}|r|S|dkr(tƒ}|s@|jtkr@| ¡}n| ¡}| |¡S)zRReturns a copy with the sign switched. Rounds, if it has reason. )r/N)rJr…rr?rÚcopy_absÚ copy_negateÚ_fix)r.r/r;r(r(r*Ú__neg__Ds  zDecimal.__neg__cCsR|jr|j|d�}|r|S|dkr(tƒ}|s@|jtkr@| ¡}nt|ƒ}| |¡S)zhReturns a copy, unless it is a sNaN. Rounds the number (if more than precision digits) )r/N)rJr…rr?rr°rr²)r.r/r;r(r(r*Ú__pos__Zs  zDecimal.__pos__TcCsJ|s | ¡S|jr&|j|d�}|r&|S|jr:|j|d�}n |j|d�}|S)zÉReturns the absolute value of self. If the keyword argument 'round' is false, do not round. The expression self.__abs__(round=False) is equivalent to self.copy_abs(). )r/)r°rJr…r7r³r´)r.Úroundr/r;r(r(r*Ú__abs__os  zDecimal.__abs__c Csht|ƒ}|tkr|S|dkr"tƒ}|js.|jr‚| ||¡}|rB|S| ¡rr|j|jkrj| ¡rj| td¡St |ƒS| ¡r‚t |ƒSt |j |j ƒ}d}|j t kr®|j|jkr®d}|sæ|sæt |j|jƒ}|rÌd}t|d|ƒ}| |¡}|S|�st||j |jdƒ}| ||j ¡}| |¡}|S|�sVt||j |jdƒ}| ||j ¡}| |¡}|St|ƒ}t|ƒ}t|||jƒ\}}tƒ} |j|jk�rú|j|jk�r´t|d|ƒ}| |¡}|S|j|jk�rÌ||}}|jdk�ròd| _|j|j|_|_nd| _n&|jdk�rd| _d\|_|_nd| _|jdk�r<|j|j| _n|j|j| _|j| _t | ƒ}| |¡}|S)zbReturns self + other. -INF + INF (or the reverse) cause InvalidOperation errors. Nz -INF + INFr&r-rK)r&r&)r—r�rrJr…r€r7r_r rr¤rIr?rr6r²Úmaxr@Ú_rescalerdÚ _normalizer=rOrP) r.r‚r/r;rPZ negativezeror=Úop1Úop2r}r(r(r*Ú__add__…s|              zDecimal.__add__cCsHt|ƒ}|tkr|S|js |jr6|j||d�}|r6|S|j| ¡|d�S)zReturn self - other)r/)r—r�rJr…r¼r±)r.r‚r/r;r(r(r*Ú__sub__Ýs zDecimal.__sub__cCs"t|ƒ}|tkr|S|j||d�S)zReturn other - self)r/)r—r�r½)r.r‚r/r(r(r*Ú__rsub__ëszDecimal.__rsub__cCs@t|ƒ}|tkr|S|dkr"tƒ}|j|jA}|js:|jrŽ| ||¡}|rN|S| ¡rn|sf| td¡St |S| ¡rŽ|s†| td¡St |S|j |j }|r¢|s¼t |d|ƒ}|  |¡}|S|j dkrât ||j |ƒ}|  |¡}|S|j dk�r t ||j |ƒ}|  |¡}|St|ƒ}t|ƒ}t |t|j|jƒ|ƒ}|  |¡}|S)z\Return self * other. (+-) INF * 0 (or its reverse) raise InvalidOperation. Nz (+-)INF * 0z 0 * (+-)INFrKÚ1)r—r�rr7rJr…r€r_r r<rIr6r²r8rdr[rO)r.r‚r/Z resultsignr;Z resultexprºr»r(r(r*Ú__mul__ósH             zDecimal.__mul__c CsÊt|ƒ}|tkrtS|dkr"tƒ}|j|jA}|js:|jrž| ||¡}|rN|S| ¡rj| ¡rj| td¡S| ¡rzt |S| ¡rž| t d¡t |d|  ¡ƒS|sÀ|s²| t d¡S| td|¡S|sÖ|j|j}d}nÚt|jƒt|jƒ|jd}|j|j|}t|ƒ}t|ƒ} |dk�r:t|jd || jƒ\}} nt|j| jd | ƒ\}} | �rt|d dk�r°|d7}n<|j|j} x.|| k�r®|d dk�r®|d }|d7}�q‚Wt |t|ƒ|ƒ}| |¡S) zReturn self / other.Nz(+-)INF/(+-)INFzDivision by infinityrKz0 / 0zx / 0r&r-r™rt)r—r�rr7rJr…r€r_r r<rr6ÚEtinyrr rIrar8r@rdÚdivmodrOr[r²) r.r‚r/r=r;rPr{Úshiftrºr»Ú remainderÚ ideal_expr(r(r*Ú __truediv__,sP          zDecimal.__truediv__c Cs|j|jA}| ¡r|j}nt|j|jƒ}| ¡| ¡}|rN| ¡sN|dkrht|ddƒ| ||j¡fS||jk�r t |ƒ}t |ƒ}|j |j kr¬|j d|j |j 9_ n|j d|j |j 9_ t |j |j ƒ\}} |d|jk�r t|t |ƒdƒt|jt | ƒ|ƒfS| td¡} | | fS)z½Return (self // other, self % other), to context.prec precision. Assumes that neither self nor other is a NaN, that self is not infinite and that other is nonzero. ršrKr&r™z%quotient too large in //, % or divmod)r7r€rIr¤rŠr6r¸r?r@rdrPrOrÂr[r_r) r.r‚r/r=rÅÚexpdiffrºr»ÚqÚrr;r(r(r*Ú_dividegs*    zDecimal._dividecCs"t|ƒ}|tkr|S|j||d�S)z)Swaps self/other and returns __truediv__.)r/)r—r�rÆ)r.r‚r/r(r(r*Ú __rtruediv__ˆszDecimal.__rtruediv__cCsÖt|ƒ}|tkr|S|dkr"tƒ}| ||¡}|r:||fS|j|jA}| ¡r~| ¡rj| td¡}||fSt|| td¡fS|s´|sš| t d¡}||fS| t d|¡| td¡fS|  ||¡\}}|  |¡}||fS)z6 Return (self // other, self % other) Nzdivmod(INF, INF)zINF % xz divmod(0, 0)zx // 0zx % 0) r—r�rr…r7r€r_r r<rr rÊr²)r.r‚r/r;r=ZquotientrÄr(r(r*Ú __divmod__�s0      zDecimal.__divmod__cCs"t|ƒ}|tkr|S|j||d�S)z(Swaps self/other and returns __divmod__.)r/)r—r�rÌ)r.r‚r/r(r(r*Ú __rdivmod__³szDecimal.__rdivmod__cCsˆt|ƒ}|tkr|S|dkr"tƒ}| ||¡}|r6|S| ¡rJ| td¡S|sj|r^| td¡S| td¡S| ||¡d}|  |¡}|S)z self % other NzINF % xzx % 0z0 % 0r-) r—r�rr…r€r_r rrÊr²)r.r‚r/r;rÄr(r(r*Ú__mod__ºs"     zDecimal.__mod__cCs"t|ƒ}|tkr|S|j||d�S)z%Swaps self/other and returns __mod__.)r/)r—r�rÎ)r.r‚r/r(r(r*Ú__rmod__ÕszDecimal.__rmod__c CsÐ|dkrtƒ}t|dd�}| ||¡}|r.|S| ¡rB| td¡S|sb|rV| td¡S| td¡S| ¡r|t|ƒ}| |¡St |j |j ƒ}|s¦t |j d|ƒ}| |¡S|  ¡|  ¡}||jdkrÎ| t¡S|d krî| ||j¡}| |¡St|ƒ}t|ƒ}|j|jk�r(|jd |j|j9_n|jd |j|j9_t|j|jƒ\}} d | |d@|jk�r~| |j8} |d7}|d |jk�r˜| t¡S|j } | d k�r¶d| } | } t | t| ƒ|ƒ}| |¡S) zI Remainder nearest to 0- abs(remainder-near) <= other/2 NT)r–zremainder_near(infinity, x)zremainder_near(x, 0)zremainder_near(0, 0)rKr-ršr™rVr&)rr—r…r€r_r rrr²r¤rIr6r7rŠr@rr¸r?rdrPrOrÂr[) r.r‚r/r;Úideal_exponentrÇrºr»rÈrÉr=r(r(r*Úremainder_nearÜsZ         zDecimal.remainder_nearcCsœt|ƒ}|tkr|S|dkr"tƒ}| ||¡}|r6|S| ¡rb| ¡rR| td¡St|j|jAS|sŒ|r€| t d|j|jA¡S| t d¡S|  ||¡dS)z self // otherNz INF // INFzx // 0z0 // 0r&) r—r�rr…r€r_r r<r7r rrÊ)r.r‚r/r;r(r(r*Ú __floordiv__'s$   zDecimal.__floordiv__cCs"t|ƒ}|tkr|S|j||d�S)z*Swaps self/other and returns __floordiv__.)r/)r—r�rÒ)r.r‚r/r(r(r*Ú __rfloordiv__CszDecimal.__rfloordiv__cCs8| ¡r(| ¡rtdƒ‚|jr"dnd}nt|ƒ}t|ƒS)zFloat representation.z%Cannot convert signaling NaN to floatz-nanÚnan)r~r†rgr7r[rk)r.Úsr(r(r*Ú __float__Js zDecimal.__float__cCst|jr(| ¡rtdƒ‚n| ¡r(tdƒ‚d|j}|jdkrT|t|jƒd|jS|t|jd|j…pjdƒSdS)z1Converts self to an int, truncating if necessary.zCannot convert NaN to integerz"Cannot convert infinity to integerrr&r™NrK) rJr~rgr€r£r7rIrOr8)r.rÕr(r(r*Ú__int__Ts   zDecimal.__int__cCs|S)Nr()r.r(r(r*Úrealcsz Decimal.realcCstdƒS)Nr&)r)r.r(r(r*Úimaggsz Decimal.imagcCs|S)Nr()r.r(r(r*Ú conjugatekszDecimal.conjugatecCs tt|ƒƒS)N)Úcomplexrk)r.r(r(r*Ú __complex__nszDecimal.__complex__cCsR|j}|j|j}t|ƒ|krJ|t|ƒ|d… d¡}t|j||jdƒSt|ƒS)z2Decapitate the payload of a NaN to fit the contextNrKT) r8r@Úclamprarbr6r7rIr)r.r/ZpayloadZmax_payload_lenr(r(r*r9qs   zDecimal._fix_nancCsX|jr | ¡r| |¡St|ƒS| ¡}| ¡}|s€|j|g|j}tt |j |ƒ|ƒ}||j krx|  t ¡t |jd|ƒSt|ƒSt|jƒ|j |j}||krÆ|  td|j¡}|  t¡|  t¡|S||k}|rÖ|}|j |k�rüt|jƒ|j |} | dk�rt |jd|dƒ}d} |j|j} | || ƒ} |jd| …�p>d} | dk�r~tt| ƒdƒ} t| ƒ|jk�r~| dd…} |d7}||k�rš|  td|j¡}nt |j| |ƒ}| �r¾|�r¾|  t¡|�rÎ|  t¡| �rÞ|  t¡|  t¡|�sø|  t ¡|S|�r |  t¡|jdk�rP|j |k�rP|  t ¡|jd|j |} t |j| |ƒSt|ƒS)zÜRound if it is necessary to keep self within prec precision. Rounds and fixes the exponent. Does not raise on a sNaN. Arguments: self - Decimal instance context - context used. rKz above Emaxr&r¿r-Nr)rJr~r9rrÁÚEtoprArÝr¤r·rIr_rr6r7rar8r@rr r Ú_pick_rounding_functionr?r[rOrr )r.r/rÁrÞÚexp_maxZnew_expZexp_minr;Zself_is_subnormalrsZrounding_methodÚchangedr{rŒr(r(r*r²}sn                     z Decimal._fixcCst|j|ƒrdSdSdS)z(Also known as round-towards-0, truncate.r&rN)Ú _all_zerosr8)r.r@r(r(r*Ú _round_downãs zDecimal._round_downcCs | |¡ S)zRounds away from 0.)rã)r.r@r(r(r*Ú _round_upêszDecimal._round_upcCs*|j|dkrdSt|j|ƒr"dSdSdS)zRounds 5 up (away from 0)Z56789r-r&rN)r8râ)r.r@r(r(r*Ú_round_half_upîs  zDecimal._round_half_upcCst|j|ƒrdS| |¡SdS)z Round 5 downrN)Ú _exact_halfr8rå)r.r@r(r(r*Ú_round_half_down÷s zDecimal._round_half_downcCs8t|j|ƒr*|dks&|j|ddkr*dS| |¡SdS)z!Round 5 to even, rest to nearest.r&r-Ú02468rN)rær8rå)r.r@r(r(r*Ú_round_half_evenþs zDecimal._round_half_evencCs |jr| |¡S| |¡ SdS)z(Rounds up (not away from 0 if negative.)N)r7rã)r.r@r(r(r*Ú_round_ceilings zDecimal._round_ceilingcCs |js| |¡S| |¡ SdS)z'Rounds down (not towards 0 if negative)N)r7rã)r.r@r(r(r*Ú _round_floor s zDecimal._round_floorcCs0|r |j|ddkr | |¡S| |¡ SdS)z)Round down unless digit prec-1 is 0 or 5.r-Z05N)r8rã)r.r@r(r(r*Ú _round_05ups zDecimal._round_05up)rrrrrrrrcCsb|dk r2t|tƒstdƒ‚tdd| ƒ}| |¡S|jrR| ¡rJtdƒ‚ntdƒ‚t|  dt ¡ƒS)aÊRound self to the nearest integer, or to a given precision. If only one argument is supplied, round a finite Decimal instance self to the nearest integer. If self is infinite or a NaN then a Python exception is raised. If self is finite and lies exactly halfway between two integers then it is rounded to the integer with even last digit. >>> round(Decimal('123.456')) 123 >>> round(Decimal('-456.789')) -457 >>> round(Decimal('-3.0')) -3 >>> round(Decimal('2.5')) 2 >>> round(Decimal('3.5')) 4 >>> round(Decimal('Inf')) Traceback (most recent call last): ... OverflowError: cannot round an infinity >>> round(Decimal('NaN')) Traceback (most recent call last): ... ValueError: cannot round a NaN If a second argument n is supplied, self is rounded to n decimal places using the rounding mode for the current context. For an integer n, round(self, -n) is exactly equivalent to self.quantize(Decimal('1En')). >>> round(Decimal('123.456'), 0) Decimal('123') >>> round(Decimal('123.456'), 2) Decimal('123.46') >>> round(Decimal('123.456'), -2) Decimal('1E+2') >>> round(Decimal('-Infinity'), 37) Decimal('NaN') >>> round(Decimal('sNaN123'), 0) Decimal('NaN123') Nz+Second argument to round should be integralr&r¿zcannot round a NaNzcannot round an infinity) rZrOrmr6ÚquantizerJr›rgr£r¸r)r.r5rPr(r(r*Ú __round__&s/   zDecimal.__round__cCs0|jr | ¡rtdƒ‚ntdƒ‚t| dt¡ƒS)zãReturn the floor of self, as an integer. For a finite Decimal instance self, return the greatest integer n such that n <= self. If self is infinite or a NaN then a Python exception is raised. zcannot round a NaNzcannot round an infinityr&)rJr›rgr£rOr¸r)r.r(r(r*Ú __floor__ds  zDecimal.__floor__cCs0|jr | ¡rtdƒ‚ntdƒ‚t| dt¡ƒS)zâReturn the ceiling of self, as an integer. For a finite Decimal instance self, return the least integer n such that n >= self. If self is infinite or a NaN then a Python exception is raised. zcannot round a NaNzcannot round an infinityr&)rJr›rgr£rOr¸r)r.r(r(r*Ú__ceil__ss  zDecimal.__ceil__cCst|dd�}t|dd�}|js$|jrÚ|dkr2tƒ}|jdkrJ| td|¡S|jdkrb| td|¡S|jdkrr|}nf|jdkr‚|}nV|jdkr®|sœ| td¡St|j|jA}n*|jdkrØ|sÈ| td ¡St|j|jA}n0t|j|jAt t |j ƒt |j ƒƒ|j|jƒ}|  ||¡S) a:Fused multiply-add. Returns self*other+third with no rounding of the intermediate product self*other. self and other are multiplied together, with no rounding of the result. The third operand is then added to the result, and a single final rounding is performed. T)r–NrSr�r5rTzINF * 0 in fmaz0 * INF in fma) r—rJrrIr_r r<r7r6r[rOr8r¼)r.r‚Zthirdr/Úproductr(r(r*Úfma‚s6          z Decimal.fmac CsÖt|ƒ}|tkr|St|ƒ}|tkr(|S|dkr6tƒ}| ¡}| ¡}| ¡}|sZ|sZ|rÂ|dkrp| td|¡S|dkr†| td|¡S|dkrœ| td|¡S|rª| |¡S|r¸| |¡S| |¡S| ¡rÚ| ¡rÚ| ¡sæ| td¡S|dkrú| td¡S|�s | td¡S| ¡|j k�r(| td¡S|�s@|�s@| td ¡S|  ¡�rPd}n|j }t t |ƒƒ}t| ¡ƒ}t| ¡ƒ} |j |td |j|ƒ|}x t| jƒD]} t|d |ƒ}�q¢Wt|| j |ƒ}t|t|ƒdƒS) z!Three argument version of __pow__NrVr�z@pow() 3rd argument not allowed unless all arguments are integersr&zApow() 2nd argument cannot be negative when 3rd argument specifiedzpow() 3rd argument cannot be 0zSinsufficient precision: pow() 3rd argument must not have more than precision digitszXat least one of pow() 1st argument and 2nd argument must be nonzero; 0**0 is not definedr™)r—r�rr~r_r r9Ú _isintegerrŠr@Ú_isevenr7rcrOrdÚto_integral_valueržrPÚranger6r[) r.r‚Úmodulor/rƒr„Z modulo_is_nanr=ÚbaseÚexponentÚir(r(r*Ú _power_modulo®sl         zDecimal._power_modulocCst|ƒ}|j|j}}x |ddkr6|d}|d7}qWt|ƒ}|j|j}}x |ddkrn|d}|d7}qPW|dk�r ||9}x |ddkr¢|d}|d7}q„W|dkr°dS|d|} |jdkrÌ| } | ¡�r|jdk�r|jt|ƒ} t| | |dƒ} nd} tddd| | | ƒS|jdk�rÆ|d} | dk�rÌ|| @|k�rRdSt |ƒd} |dd }|t t |ƒƒk�r€dSt | ||ƒ} t |||ƒ}| dk�s°|dk�r´dS| |k�rÂdSd | }nÎ| d k�r–t |ƒd d } t d | |ƒ\}}|�rdSx$|d dk�r&|d }| d8} �qW|dd }|t t |ƒƒk�rJdSt | ||ƒ} t |||ƒ}| dk�sz|dk�r~dS| |k�rŒdSd | }ndS|d|k�r¬dS| |}tdt |ƒ|ƒS|dk�rä|d|d}}nä|dk�rt t t||ƒƒƒ| k�rdSt |ƒ}|dk�r@t t t|ƒ|ƒƒ| k�r@dS|d| }}x:|d |d k�rrdk�rŠnn|d }|d }�qRWx:|d |d k�r®dk�rÆnn|d }|d }�qŽW|dk�rv|dk�rê||k�rêdSt ||ƒ\}}|dk�rdSdt |ƒ | >}x>t |||dƒ\}}||k�r@Pn||d||}�qW||k�rn|dk�srdS|}|dk�rš||dt|ƒk�ršdS||}||9}|d|k�r¼dSt |ƒ}| ¡�r|jdk�r|jt|ƒ} t|| |t |ƒƒ} nd} td|d| || ƒS)ahAttempt to compute self**other exactly. Given Decimals self and other and an integer p, attempt to compute an exact result for the power self**other, with p digits of precision. Return None if self**other is not exactly representable in p digits. Assumes that elimination of special cases has already been performed: self and other must both be nonspecial; self must be positive and not numerically equal to 1; other must be nonzero. For efficiency, other._exp should not be too large, so that 10**abs(other._exp) is a feasible calculation.r™r&r-Nr¿rK)rVéééé]éArtérUrVéd)rdrOrPr=rór7rIr¤r6Ú_nbitsrar[Ú_decimal_lshift_exactrÂrcÚ _log10_lb)r.r‚ÚpÚxÚxcÚxeÚyÚycÚyerùrÐZzerosZ last_digitr¨ZemaxrÄrpr5Zxc_bitsÚremÚarÈrÉZstr_xcr(r(r*Ú _power_exactsÒ:                  &&&&    zDecimal._power_exactcCs>|dk r| |||¡St|ƒ}|tkr*|S|dkr8tƒ}| ||¡}|rL|S|sd|s`| td¡StSd}|jdkr |  ¡rˆ|  ¡s˜d}n|r˜| td¡S|  ¡}|sÂ|jdkrºt |ddƒSt |S| ¡rè|jdkrÜt |St |ddƒS|tk�rŽ|  ¡�rZ|jdk�rd}n||jk�r"|j}nt|ƒ}|j|}|d|jk�rxd|j}| t¡n| t¡| t¡d|j}t |dd| |ƒS| ¡}| ¡�rÈ|jdk|dkk�rÀt |ddƒSt |Sd}d} | ¡| ¡} |dk|jdkk�r| tt|jƒƒk�rHt |d|jdƒ}n,| ¡} | tt| ƒƒk�rHt |d| dƒ}|dk�rŒ| ||jd¡}|dk �rŒ|dk�rˆt d|j|jƒ}d } |dk�r:|j} t|ƒ} | j| j}}t|ƒ}|j|j}}|jdk�rÚ| }d }xJt||||| |ƒ\}}|d d tt|ƒƒ| d�rP|d 7}�qàWt |t|ƒ|ƒ}| �r0|  ¡�s0t|jƒ|jk�rŽ|jdt|jƒ}t |j|jd||j|ƒ}|  ¡}| !¡xt"D]}d|j#|<�q¤W| $|¡}| t¡|j%t&�râ| t'¡|j%t(�rþ| t(d |j¡x:t't&ttt)fD]}|j%|�r| |¡�qWn | $|¡}|S)aHReturn self ** other [ % modulo]. With two arguments, compute self**other. With three arguments, compute (self**other) % modulo. For the three argument form, the following restrictions on the arguments hold: - all three arguments must be integral - other must be nonnegative - either self or other (or both) must be nonzero - modulo must be nonzero and must have at most p digits, where p is the context precision. If any of these restrictions is violated the InvalidOperation flag is raised. The result of pow(self, other, modulo) is identical to the result that would be obtained by computing (self**other) % modulo with unbounded precision, but is computed more efficiently. It is always exact. Nz0 ** 0r&r-z+x ** y with x negative and y not an integerrKr¿FTrUrtr™z above Emax)*rûr—r�rr…r_r Ú_Oner7rórôr±r6r<r€r@rOrIr r rŠÚ_log10_exp_boundrar[rArÁrr8rdrPr=Ú_dpowerrFrGÚ_signalsÚtrapsr²Úflagsr rrr)r.r‚r÷r/r;Z result_signZ multiplierrPZself_adjÚexactZboundrÁrrrr r r r Úextrar{rÇZ newcontextZ exceptionr(r(r*Ú__pow__ðsÊ                        "         zDecimal.__pow__cCs"t|ƒ}|tkr|S|j||d�S)z%Swaps self/other and returns __pow__.)r/)r—r�r)r.r‚r/r(r(r*Ú__rpow__È szDecimal.__rpow__cCs¼|dkrtƒ}|jr(|j|d�}|r(|S| |¡}| ¡r>|S|sPt|jddƒS|j| ¡g|j }t |j ƒ}|j }x.|j |ddkr¢||kr¢|d7}|d8}qvWt|j|j d|…|ƒS)z?Normalize- strip trailing 0s, change anything equal to 0 to 0e0N)r/rKr&r-) rrJr…r²r€r6r7rArÞrÝrar8rI)r.r/r;ÚdupràÚendrPr(r(r*Ú normalizeÏ s$    zDecimal.normalizecCs¦t|dd�}|dkrtƒ}|dkr(|j}|js4|jr|| ||¡}|rH|S| ¡sX| ¡r|| ¡rp| ¡rpt|ƒS| td¡S|  ¡|j kr˜|j ks¦n| td¡S|sÄt |j d|j ƒ}| |¡S| ¡}||j krâ| td¡S||j d|jk�r| td ¡S| |j |¡}| ¡|j k�r.| td¡St|jƒ|jk�rL| td ¡S|�rl| ¡|jk�rl| t¡|j |j k�r˜||k�rŽ| t¡| t¡| |¡}|S) z‡Quantize self so its exponent is the same as that of exp. Similar to self._rescale(exp._exp) but with error checking. T)r–Nzquantize with one INFz)target exponent out of bounds in quantizerKz9exponent of quantize result too large for current contextr-z7quantize result has too many digits for current context)r—rr?rJr…r€rr_r rÁrIrAr6r7r²rŠr@r¸rar8ÚEminr r r )r.rPr?r/r;r‹r(r(r*ríè sT          zDecimal.quantizecCsDt|dd�}|js|jr8| ¡r(| ¡p6| ¡o6| ¡S|j|jkS)a=Return True if self and other have the same exponent; otherwise return False. If either operand is a special value, the following rules are used: * return True if both operands are infinities * return True if both operands are NaNs * otherwise, return False. T)r–)r—rJr›Ú is_infiniterI)r.r‚r/r(r(r*Ú same_quantum% s  zDecimal.same_quantumcCsÆ|jrt|ƒS|s t|jd|ƒS|j|krHt|j|jd|j||ƒSt|jƒ|j|}|dkrzt|jd|dƒ}d}|j|}|||ƒ}|jd|…pžd}|dkr¸tt |ƒdƒ}t|j||ƒS)asRescale self so that the exponent is exp, either by padding with zeros or by truncating digits, using the given rounding mode. Specials are returned without change. This operation is quiet: it raises no flags, and uses no information from the context. exp = exp to scale to (an integer) rounding = rounding mode rKr&r¿r-N) rJrr6r7rIr8rarßr[rO)r.rPr?rsZ this_functionrár{r(r(r*r¸4 s"    zDecimal._rescalecCsf|dkrtdƒ‚|js|s"t|ƒS| | ¡d||¡}| ¡| ¡krb| | ¡d||¡}|S)a"Round a nonzero, nonspecial Decimal to a fixed number of significant figures, using the given rounding mode. Infinities, NaNs and zeros are returned unaltered. This operation is quiet: it raises no flags, and uses no information from the context. r&z'argument should be at least 1 in _roundr-)rgrJrr¸rŠ)r.Úplacesr?r;r(r(r*Ú_roundV s  zDecimal._roundcCsŽ|jr"|j|d�}|r|St|ƒS|jdkr4t|ƒS|sFt|jddƒS|dkrTtƒ}|dkrb|j}| d|¡}||kr€|  t ¡|  t ¡|S)aVRounds to a nearby integer. If no rounding mode is specified, take the rounding mode from the context. This method raises the Rounded and Inexact flags when appropriate. See also: to_integral_value, which does exactly the same as this method except that it doesn't raise Inexact or Rounded. )r/r&rKN) rJr…rrIr6r7rr?r¸r_r r )r.r?r/r;r(r(r*Úto_integral_exactm s$      zDecimal.to_integral_exactcCs`|dkrtƒ}|dkr|j}|jr>|j|d�}|r6|St|ƒS|jdkrPt|ƒS| d|¡SdS)z@Rounds to the nearest integer, without raising inexact, rounded.N)r/r&)rr?rJr…rrIr¸)r.r?r/r;r(r(r*rõŠ s  zDecimal.to_integral_valuecCsà|dkrtƒ}|jrB|j|d�}|r(|S| ¡rB|jdkrBt|ƒS|sdt|jd|jdƒ}| |¡S|jdkrz|  t d¡S|j d}t |ƒ}|j d?}|j d@r¾|jd}t|jƒd?d}n|j}t|jƒdd?}||}|dkrø|d |9}d } nt|d | ƒ\}} | } ||8}d|} x(|| } | | k�r:Pn | | d?} �q$W| �o\| | |k} | �r”|dk�r|| d|} n| d| 9} ||7}n| d dk�rª| d7} tdt| ƒ|ƒ}| ¡}| t¡} | |¡}| |_|S) zReturn the square root of self.N)r/r&rKrVr-zsqrt(-x), x > 0r™rTrt)rrJr…r€r7rr6rIr²r_r r@rdrPrOrar8rÂr[Ú _shallow_copyÚ _set_roundingrr?)r.r/r;r@Úopr¨ÚcÚlrÃrrÄr5rÈr?r(r(r*Úsqrt� s`              z Decimal.sqrtcCs¶t|dd�}|dkrtƒ}|js&|jr~| ¡}| ¡}|s>|r~|dkrX|dkrX| |¡S|dkrr|dkrr| |¡S| ||¡S| |¡}|dkrš| |¡}|dkr¨|}n|}| |¡S)z Returns the larger value. Like max(self, other) except if one is not a number, returns NaN (and signals if one is sNaN). Also rounds. T)r–Nr-r&r)r—rrJr~r²r…r�Ú compare_total)r.r‚r/ÚsnÚonr&r;r(r(r*r· s&       z Decimal.maxcCs¶t|dd�}|dkrtƒ}|js&|jr~| ¡}| ¡}|s>|r~|dkrX|dkrX| |¡S|dkrr|dkrr| |¡S| ||¡S| |¡}|dkrš| |¡}|dkr¨|}n|}| |¡S)z¡Returns the smaller value. Like min(self, other) except if one is not a number, returns NaN (and signals if one is sNaN). Also rounds. T)r–Nr-r&r)r—rrJr~r²r…r�r))r.r‚r/r*r+r&r;r(r(r*r¤* s&       z Decimal.mincCs8|jr dS|jdkrdS|j|jd…}|dt|ƒkS)z"Returns whether self is an integerFr&TNrK)rJrIr8ra)r.Úrestr(r(r*róL s  zDecimal._isintegercCs&|r|jdkrdS|jd|jdkS)z:Returns True if self is even. Assumes self is an integer.r&Trrè)rIr8)r.r(r(r*rôU szDecimal._isevencCs.y|jt|jƒdStk r(dSXdS)z$Return the adjusted exponent of selfr-r&N)rIrar8rm)r.r(r(r*rŠ[ szDecimal.adjustedcCs|S)z«Returns the same Decimal object. As we do not have different encodings for the same number, the received object already is in its canonical form. r()r.r(r(r*Ú canonicalc szDecimal.canonicalcCs.t|dd�}| ||¡}|r |S|j||d�S)z¶Compares self to the other operand numerically. It's pretty much like compare(), but all NaNs signal, with signaling NaNs taking precedence over quiet NaNs. T)r–)r/)r—rˆr˜)r.r‚r/r;r(r(r*Úcompare_signalk s   zDecimal.compare_signalcCs`t|dd�}|jr|jstS|js,|jr,tS|j}| ¡}| ¡}|sL|�r||kr t|jƒ|jf}t|jƒ|jf}||krˆ|r„tStS||krœ|r˜tStStS|rÖ|dkr°tS|dkr¼tS|dkrÈtS|dkrÔtSn2|dkrâtS|dkrîtS|dkrútS|dk�rtS||k�rtS||k�r$tS|j|jk�r@|�rrtr™)rr…r€r0rrr@rŠr7rar[rAr6rÁrdrOrPr=Ú_dexpr#r$rr²r?) r.r/r;rÚadjr%r&r¨rr{rPr?r(r(r*rPÜ sJ   $(  "  z Decimal.expcCsdS)zÃReturn True if self is canonical; otherwise return False. Currently, the encoding of a Decimal instance is always canonical, so this method returns True for any Decimal. Tr()r.r(r(r*Ú is_canonical' szDecimal.is_canonicalcCs|j S)z�Return True if self is finite; otherwise return False. A Decimal instance is considered finite if it is neither infinite nor a NaN. )rJ)r.r(r(r*Ú is_finite/ szDecimal.is_finitecCs |jdkS)z8Return True if self is infinite; otherwise return False.rT)rI)r.r(r(r*r7 szDecimal.is_infinitecCs |jdkS)z>Return True if self is a qNaN or sNaN; otherwise return False.)r5rS)rI)r.r(r(r*r›; szDecimal.is_nancCs*|js |sdS|dkrtƒ}|j| ¡kS)z?Return True if self is a normal number; otherwise return False.FN)rJrrrŠ)r.r/r(r(r*Ú is_normal? s  zDecimal.is_normalcCs |jdkS)z;Return True if self is a quiet NaN; otherwise return False.r5)rI)r.r(r(r*r‡G szDecimal.is_qnancCs |jdkS)z8Return True if self is negative; otherwise return False.r-)r7)r.r(r(r*Ú is_signedK szDecimal.is_signedcCs |jdkS)z?Return True if self is a signaling NaN; otherwise return False.rS)rI)r.r(r(r*r†O szDecimal.is_snancCs*|js |sdS|dkrtƒ}| ¡|jkS)z9Return True if self is subnormal; otherwise return False.FN)rJrrŠr)r.r/r(r(r*Ú is_subnormalS s  zDecimal.is_subnormalcCs|j o|jdkS)z6Return True if self is a zero; otherwise return False.rK)rJr8)r.r(r(r*Úis_zero[ szDecimal.is_zerocCsÆ|jt|jƒd}|dkr4tt|ddƒƒdS|dkrXttd|ddƒƒdSt|ƒ}|j|j}}|dkr¨t|d| ƒ}t|ƒ}t|ƒt|ƒ||kS|ttd| |ƒƒdS)zÌCompute a lower bound for the adjusted exponent of self.ln(). In other words, compute r such that self.ln() >= 10**r. Assumes that self is finite and positive and that self != 1. r-ér™ršrr&)rIrar8r[rdrOrP)r.r5r%r&r¨ÚnumÚdenr(r(r*Ú _ln_exp_bound_ szDecimal._ln_exp_boundc Cs |dkrtƒ}|j|d�}|r"|S|s*tS| ¡dkr:tS|tkrFtS|jdkr\| t d¡St |ƒ}|j |j }}|j }|| ¡d}x>t|||ƒ}|ddttt|ƒƒƒ|dr¼P|d7}qŠWtt |d kƒtt|ƒƒ| ƒ}| ¡}| t¡} | |¡}| |_|S) z/Returns the natural (base e) logarithm of self.N)r/r-zln of a negative valuerVrtr™rUr&)rr…Ú_NegativeInfinityr€Ú _Infinityrr0r7r_r rdrOrPr@r?Ú_dlograr[rcr6r#r$rr²r?) r.r/r;r%r&r¨rr r{r?r(r(r*Úlnx s:    $   z Decimal.lncCsÊ|jt|jƒd}|dkr,tt|ƒƒdS|dkrHttd|ƒƒdSt|ƒ}|j|j}}|dkr t|d| ƒ}td|ƒ}t|ƒt|ƒ||kdStd| |ƒ}t|ƒ||dkdS) zÎCompute a lower bound for the adjusted exponent of self.log10(). In other words, find r such that self.log10() >= 10**r. Assumes that self is finite and positive and that self != 1. r-ršrr&r™éçrVZ231)rIrar8r[rdrOrP)r.r5r%r&r¨r=r>r(r(r*rª s  zDecimal._log10_exp_boundc CsH|dkrtƒ}|j|d�}|r"|S|s*tS| ¡dkr:tS|jdkrP| td¡S|jddkr˜|jdd…dt |jƒdkr˜t |j t |jƒdƒ}nŠt |ƒ}|j |j}}|j}|| ¡d}x>t|||ƒ}|d d t tt|ƒƒƒ|drøP|d 7}qÆWtt |dkƒtt|ƒƒ| ƒ}| ¡}| t¡} | |¡}| |_|S) z&Returns the base 10 logarithm of self.N)r/r-zlog10 of a negative valuer&r¿rKrVrtr™rU)rr…r@r€rAr7r_r r8rarrIrdrOrPr@rÚ_dlog10r[rcr6r#r$rr²r?) r.r/r;r%r&r¨rr r{r?r(r(r*Úlog10È s:   . $   z Decimal.log10cCsV|j|d�}|r|S|dkr"tƒ}| ¡r.tS|s@| tdd¡St| ¡ƒ}| |¡S)aM Returns the exponent of the magnitude of self's MSD. The result is the integer which is the exponent of the magnitude of the most significant digit of self (as though it were truncated to a single digit while maintaining the value of that digit and without limiting the resulting exponent). )r/Nzlogb(0)r-) r…rr€rAr_r rrŠr²)r.r/r;r(r(r*Úlogbû s  z Decimal.logbcCs8|jdks|jdkrdSx|jD]}|dkr dSq WdS)z×Return True if self is a logical operand. For being logical, it must be a finite number with a sign of 0, an exponent of 0, and a coefficient whose digits must all be either 0 or 1. r&FZ01T)r7rIr8)r.Údigr(r(r*Ú _islogical s  zDecimal._islogicalcCs€|jt|ƒ}|dkr$d||}n|dkr<||j d…}|jt|ƒ}|dkr`d||}n|dkrx||j d…}||fS)Nr&rK)r@ra)r.r/ÚopaÚopbZdifr(r(r*Ú _fill_logical' szDecimal._fill_logicalcCsz|dkrtƒ}t|dd�}| ¡r*| ¡s4| t¡S| ||j|j¡\}}d dd„t||ƒDƒ¡}t d|  d¡ptddƒS) z;Applies an 'and' operation between self and other's digits.NT)r–rMcSs$g|]\}}tt|ƒt|ƒ@ƒ‘qSr()r[rO)Ú.0rÚbr(r(r*ú B sz'Decimal.logical_and..r&rK) rr—rIr_r rLr8riÚzipr6rb)r.r‚r/rJrKr}r(r(r*Ú logical_and4 s  zDecimal.logical_andcCs(|dkrtƒ}| tdd|jdƒ|¡S)zInvert all its digits.Nr&r¿)rÚ logical_xorr6r@)r.r/r(r(r*Úlogical_invertE szDecimal.logical_invertcCsz|dkrtƒ}t|dd�}| ¡r*| ¡s4| t¡S| ||j|j¡\}}d dd„t||ƒDƒ¡}t d|  d¡ptddƒS) z:Applies an 'or' operation between self and other's digits.NT)r–rMcSs$g|]\}}tt|ƒt|ƒBƒ‘qSr()r[rO)rMrrNr(r(r*rOZ sz&Decimal.logical_or..r&rK) rr—rIr_r rLr8rirPr6rb)r.r‚r/rJrKr}r(r(r*Ú logical_orL s  zDecimal.logical_orcCsz|dkrtƒ}t|dd�}| ¡r*| ¡s4| t¡S| ||j|j¡\}}d dd„t||ƒDƒ¡}t d|  d¡ptddƒS) z;Applies an 'xor' operation between self and other's digits.NT)r–rMcSs$g|]\}}tt|ƒt|ƒAƒ‘qSr()r[rO)rMrrNr(r(r*rOk sz'Decimal.logical_xor..r&rK) rr—rIr_r rLr8rirPr6rb)r.r‚r/rJrKr}r(r(r*rR] s  zDecimal.logical_xorcCs¾t|dd�}|dkrtƒ}|js&|jr~| ¡}| ¡}|s>|r~|dkrX|dkrX| |¡S|dkrr|dkrr| |¡S| ||¡S| ¡ | ¡¡}|dkr¢| |¡}|dkr°|}n|}| |¡S)z8Compares the values numerically with their sign ignored.T)r–Nr-r&r) r—rrJr~r²r…r°r�r))r.r‚r/r*r+r&r;r(r(r*Úmax_magn s&      zDecimal.max_magcCs¾t|dd�}|dkrtƒ}|js&|jr~| ¡}| ¡}|s>|r~|dkrX|dkrX| |¡S|dkrr|dkrr| |¡S| ||¡S| ¡ | ¡¡}|dkr¢| |¡}|dkr°|}n|}| |¡S)z8Compares the values numerically with their sign ignored.T)r–Nr-r&r) r—rrJr~r²r…r°r�r))r.r‚r/r*r+r&r;r(r(r*Úmin_magŒ s&      zDecimal.min_magcCs |dkrtƒ}|j|d�}|r"|S| ¡dkr2tS| ¡dkrTtdd|j| ¡ƒS| ¡}| t ¡|  ¡|  |¡}||kr„|S|  tdd|  ¡dƒ|¡S)z=Returns the largest representable number smaller than itself.N)r/rr-r&r>r¿)rr…r€r@r6r@rÞrFr$rÚ_ignore_all_flagsr²r½rÁ)r.r/r;Únew_selfr(r(r*Ú next_minusª s"     zDecimal.next_minuscCs |dkrtƒ}|j|d�}|r"|S| ¡dkr2tS| ¡dkrTtdd|j| ¡ƒS| ¡}| t ¡|  ¡|  |¡}||kr„|S|  tdd|  ¡dƒ|¡S)z=Returns the smallest representable number larger than itself.N)r/r-rr>r&r¿)rr…r€rAr6r@rÞrFr$rrWr²r¼rÁ)r.r/r;rXr(r(r*Ú next_plusÁ s"     zDecimal.next_pluscCsÞt|dd�}|dkrtƒ}| ||¡}|r.|S| |¡}|dkrJ| |¡S|dkr^| |¡}n | |¡}| ¡r–| t d|j ¡| t ¡| t ¡nD|  ¡|jkrÚ| t¡| t¡| t ¡| t ¡|sÚ| t¡|S)a‹Returns the number closest to self, in the direction towards other. The result is the closest representable number to self (excluding self) that is in the direction towards other, unless both have the same value. If the two operands are numerically equal, then the result is a copy of self with the sign set to be the same as the sign of other. T)r–Nr&rz Infinite result from next_toward)r—rr…r�r3rZrYr€r_rr7r r rŠrrr r)r.r‚r/r;Z comparisonr(r(r*Ú next_towardØ s4             zDecimal.next_towardcCsˆ| ¡r dS| ¡rdS| ¡}|dkr,dS|dkr8dS| ¡rN|jrJdSdS|d kr\tƒ}|j|d �rv|jrrd Sd S|jr€d SdSd S)aReturns an indication of the class of self. The class is one of the following strings: sNaN NaN -Infinity -Normal -Subnormal -Zero +Zero +Subnormal +Normal +Infinity r�r¦r-z +Infinityrz -Infinityz-Zeroz+ZeroN)r/z -Subnormalz +Subnormalz-Normalz+Normal)r†r‡r€r;r7rr:)r.r/Úinfr(r(r*Ú number_classs, zDecimal.number_classcCstdƒS)z'Just returns 10, as this is Decimal, :)r™)r)r.r(r(r*Úradix0sz Decimal.radixcCsö|dkrtƒ}t|dd�}| ||¡}|r.|S|jdkrB| t¡S|j t|ƒkr`|jksln| t¡S| ¡r|t |ƒSt|ƒ}|j }|jt |ƒ}|dkr®d||}n|dkrÄ|| d…}||d…|d|…}t |j | d¡pîd|jƒS)z5Returns a rotated copy of self, value-of-other times.NT)r–r&rK)rr—r…rIr_r r@rOr€rr8rar6r7rb)r.r‚r/r;ÚtorotÚrotdigÚtopadZrotatedr(r(r*Úrotate4s,      zDecimal.rotatecCs¾|dkrtƒ}t|dd�}| ||¡}|r.|S|jdkrB| t¡Sd|j|j}d|j|j}|t|ƒkrz|ks†n| t¡S|  ¡r–t |ƒSt |j |j |jt|ƒƒ}| |¡}|S)z>Returns self operand after adding the second value to its exp.NT)r–r&ršrV)rr—r…rIr_r rAr@rOr€rr6r7r8r²)r.r‚r/r;ZliminfZlimsupr|r(r(r*ÚscalebUs"      zDecimal.scalebcCs|dkrtƒ}t|dd�}| ||¡}|r.|S|jdkrB| t¡S|j t|ƒkr`|jksln| t¡S| ¡r|t |ƒSt|ƒ}|j }|jt |ƒ}|dkr®d||}n|dkrÄ|| d…}|dkrÚ|d|…}n|d|}||j d…}t |j | d¡�p d|jƒS)z5Returns a shifted copy of self, value-of-other times.NT)r–r&rK)rr—r…rIr_r r@rOr€rr8rar6r7rb)r.r‚r/r;r_r`raZshiftedr(r(r*rÃns2       z Decimal.shiftcCs|jt|ƒffS)N)Ú __class__r[)r.r(r(r*Ú __reduce__•szDecimal.__reduce__cCst|ƒtkr|S| t|ƒ¡S)N)Útyperrdr[)r.r(r(r*Ú__copy__˜s zDecimal.__copy__cCst|ƒtkr|S| t|ƒ¡S)N)rfrrdr[)r.Zmemor(r(r*Ú __deepcopy__�s zDecimal.__deepcopy__cCsJ|dkrtƒ}t||d�}|jrXt|j|ƒ}t| ¡ƒ}|ddkrL|d7}t|||ƒS|ddkrvddg|j|d<|ddkr˜t |j|j |j dƒ}|j }|d}|dk �r|dd krÎ|  |d |¡}nF|dd krê| | |¡}n*|dd k�rt|j ƒ|k�r|  ||¡}|�s@|j d k�r@|dd k�r@| d |¡}|j t|j ƒ} |dd k�r~|�sx|dk �rxd |} nd } nB|dd k�r’| } n.|dd k�rÀ|j d k�r¼| dk�r¼| } nd } | d k�ràd} d| |j } nP| t|j ƒk�r|j d| t|j ƒ} d} n"|j d| …�p d} |j | d…} | | } t|j| | | |ƒS)a|Format a Decimal instance according to the given specifier. The specifier should be a standard format specifier, with the form described in PEP 3101. Formatting types 'e', 'E', 'f', 'F', 'g', 'G', 'n' and '%' are supported. If the formatting type is omitted it defaults to 'g' or 'G', depending on the value of context.capitals. N)Ú _localeconvrfú%ÚgÚGrVÚ precisionÚeEr-zfF%ZgGr&iúÿÿÿrKrM)rÚ_parse_format_specifierrJÚ _format_signr7r[r°Ú _format_alignrªr6r8rIr?r!r¸raÚ_format_number)r.Z specifierr/riÚspecr=Úbodyr?rmr¬r­rqrrrPr(r(r*Ú __format__¤sZ               zDecimal.__format__)rKN)NN)N)N)N)N)N)N)FN)N)N)N)TN)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)NN)N)N)NN)N)NN)NN)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)NN)‚r1r2r3r4Ú __slots__rYÚ classmethodrlr~r€r…rˆr‰r�r‘r’r“r”r•r˜r¡r¢rwr¥r®r¯r³r´r¶r¼Ú__radd__r½r¾rÀÚ__rmul__rÆrÊrËrÌrÍrÎrÏrÑrÒrÓrÖr×Ú __trunc__ÚpropertyrØrÙrÚrÜr9r²rãrärårçrérêrërìÚdictrßrîrïrðròrûrrrrrírr¸r!r"rõÚ to_integralr(r·r¤rórôrŠr-r.r)r2r°r±r3rPr6r7rr›r8r‡r9r†r:r;r?rCrrFrGrIrLrQrSrTrRrUrVrYrZr[r]r^rbrcrÃrergrhrur(r(r(r*rs  - !@  2 4    V   7 ;!  $    K       f  > , Un Y   = "   c * "  I   K   2 3          . * !  'FcCs&t t¡}||_||_||_||_|S)z½Create a decimal instance directly, without any validation, normalization (e.g. removal of leading zeros) or argument conversion. This function is for *internal use only*. )rXrYrr7r8rIrJ)r=Z coefficientrùZspecialr.r(r(r*r6ös  r6c@s(eZdZdZdd„Zdd„Zdd„ZdS) rHz­Context manager class to support localcontext(). Sets a copy of the supplied context in __enter__() and restores the previous decimal context in __exit__() cCs| ¡|_dS)N)rFÚ new_context)r.r~r(r(r*Ú__init__sz_ContextManager.__init__cCstƒ|_t|jƒ|jS)N)rÚ saved_contextrr~)r.r(r(r*Ú __enter__s z_ContextManager.__enter__cCst|jƒdS)N)rr€)r.ÚtÚvÚtbr(r(r*Ú__exit__sz_ContextManager.__exit__N)r1r2r3r4rr�r…r(r(r(r*rHsrHc @s¬eZdZdZd¦dd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z dd„ZeZd§dd„Zdd„Zdd„Zdd „ZdZd!d"„Zd#d$„Zd%d&„Zd¨d(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Zd2d3„Zd4d5„Zd6d7„Zd8d9„Z d:d;„Z!dd?„Z#d@dA„Z$dBdC„Z%dDdE„Z&dFdG„Z'dHdI„Z(dJdK„Z)dLdM„Z*dNdO„Z+dPdQ„Z,dRdS„Z-dTdU„Z.dVdW„Z/dXdY„Z0dZd[„Z1d\d]„Z2d^d_„Z3d`da„Z4dbdc„Z5ddde„Z6dfdg„Z7dhdi„Z8djdk„Z9dldm„Z:dndo„Z;dpdq„Zdvdw„Z?dxdy„Z@dzd{„ZAd|d}„ZBd~d„ZCd€d�„ZDd‚dƒ„ZEd„d…„ZFd†d‡„ZGd©dˆd‰„ZHdŠd‹„ZIdŒd�„ZJdŽd�„ZKd�d‘„ZLd’d“„ZMd”d•„ZNd–d—„ZOd˜d™„ZPdšd›„ZQdœd�„ZRdždŸ„ZSd d¡„ZTd¢d£„ZUd¤d¥„ZVeVZWdS)ªraßContains the context for a Decimal instance. Contains: prec - precision (for use in rounding, division, square roots..) rounding - rounding type (how you round) traps - If traps[exception] = 1, then the exception is raised when it is caused. Otherwise, a value is substituted in. flags - When an exception is caused, flags[exception] is set. (Whether or not the trap_enabler is set) Should be reset by user of Decimal instance. Emin - Minimum exponent Emax - Maximum exponent capitals - If 1, 1*10^1 is printed as 1E+1. If 0, printed as 1e1 clamp - If 1, change exponents if too high (Default 0) Nc s>yt} Wntk rYnX|dk r*|n| j|_|dk r>|n| j|_|dk rR|n| j|_|dk rf|n| j|_|dk rz|n| j|_|dk rŽ|n| j|_| dkr¦g|_n| |_ˆdkrÂ| j   ¡|_ n.t ˆt ƒsêt ‡fdd„t ˆDƒƒ|_ nˆ|_ ˆdk�r t  t d¡|_n0t ˆt ƒ�s4t ‡fdd„t ˆDƒƒ|_nˆ|_dS)Nc3s|]}|t|ˆkƒfVqdS)N)rO)rMrÕ)rr(r*ú Isz#Context.__init__..r&c3s|]}|t|ˆkƒfVqdS)N)rO)rMrÕ)rr(r*r†Ps)rÚ NameErrorr@r?rrArªrÝÚ_ignored_flagsrrFrZr|rÚfromkeysr) r.r@r?rrArªrÝrrrˆZdcr()rrr*r0s.   zContext.__init__cCs”t|tƒstd|ƒ‚|dkr<||kr†td||||fƒ‚nJ|dkrb||kr†td||||fƒ‚n$||ksr||kr†td||||fƒ‚t |||¡S)Nz%s must be an integerz-infz%s must be in [%s, %d]. got: %sr\z%s must be in [%d, %s]. got: %sz%s must be in [%d, %d]. got %s)rZrOrmrgrXÚ __setattr__)r.ÚnameroZvminZvmaxr(r(r*Ú_set_integer_checkTs  zContext._set_integer_checkcCsht|tƒstd|ƒ‚x |D]}|tkrtd|ƒ‚qWx tD]}||kr>td|ƒ‚q>Wt |||¡S)Nz%s must be a signal dictz%s is not a valid signal dict)rZr|rmrÚKeyErrorrXrŠ)r.r‹r|Úkeyr(r(r*Ú_set_signal_dictbs    zContext._set_signal_dictcCsä|dkr| ||dd¡S|dkr0| ||dd¡S|dkrH| ||dd¡S|dkr`| ||dd¡S|d krx| ||dd¡S|d kr¢|tkr”td |ƒ‚t |||¡S|d ks²|d kr¾| ||¡S|dkrÔt |||¡Std|ƒ‚dS)Nr@r-r\rz-infr&rArªrÝr?z%s: invalid rounding moderrrˆz.'decimal.Context' object has no attribute '%s')rŒÚ_rounding_modesrmrXrŠr�ÚAttributeError)r.r‹ror(r(r*rŠms(  zContext.__setattr__cCstd|ƒ‚dS)Nz%s cannot be deleted)r‘)r.r‹r(r(r*Ú __delattr__†szContext.__delattr__c CsNdd„|j ¡Dƒ}dd„|j ¡Dƒ}|j|j|j|j|j|j|j ||ffS)NcSsg|]\}}|r|‘qSr(r()rMÚsigrƒr(r(r*rO‹sz&Context.__reduce__..cSsg|]\}}|r|‘qSr(r()rMr“rƒr(r(r*rOŒs) rÚitemsrrdr@r?rrArªrÝ)r.rrr(r(r*reŠs zContext.__reduce__cCs|g}| dt|ƒ¡dd„|j ¡Dƒ}| dd |¡d¡dd„|j ¡Dƒ}| dd |¡d¡d |¡d S) zShow the current context.zrContext(prec=%(prec)d, rounding=%(rounding)s, Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d, clamp=%(clamp)dcSsg|]\}}|r|j‘qSr()r1)rMryrƒr(r(r*rO˜sz$Context.__repr__..zflags=[z, ú]cSsg|]\}}|r|j‘qSr()r1)rMr‚rƒr(r(r*rOšsztraps=[ú))rhÚvarsrr”rir)r.rÕÚnamesr(r(r*r¥‘s zContext.__repr__cCsx|jD]}d|j|<qWdS)zReset all flags to zeror&N)r)r.Úflagr(r(r*rGžs zContext.clear_flagscCsx|jD]}d|j|<qWdS)zReset all traps to zeror&N)r)r.r™r(r(r*Ú clear_traps£s zContext.clear_trapsc Cs.t|j|j|j|j|j|j|j|j|j ƒ }|S)z!Returns a shallow copy from self.) rr@r?rrArªrÝrrrˆ)r.Úncr(r(r*r#¨szContext._shallow_copyc Cs6t|j|j|j|j|j|j|j ¡|j  ¡|j ƒ }|S)zReturns a deep copy from self.) rr@r?rrArªrÝrrFrrˆ)r.r›r(r(r*rF¯s z Context.copycGsZt ||¡}||jkr(|ƒj|f|žŽSd|j|<|j|sN|ƒj|f|žŽS||ƒ‚dS)a#Handles an error If the flag is in _ignored_flags, returns the default response. Otherwise, it sets the flag, then, if the corresponding trap_enabler is set, it reraises the exception. Otherwise, it returns the default value after setting the flag. r-N)Ú_condition_maprCrˆr0rr)r.Z conditionZ explanationr)Úerrorr(r(r*r_¸s    zContext._raise_errorcCs |jtŽS)z$Ignore all flags, if they are raised)Ú _ignore_flagsr)r.r(r(r*rWÎszContext._ignore_all_flagscGs|jt|ƒ|_t|ƒS)z$Ignore the flags, if they are raised)rˆre)r.rr(r(r*ržÒszContext._ignore_flagscGs<|rt|dttfƒr|d}x|D]}|j |¡q$WdS)z+Stop ignoring the flags, if they are raisedr&N)rZrfrerˆÚremove)r.rr™r(r(r*Ú _regard_flagsÙs zContext._regard_flagscCst|j|jdƒS)z!Returns Etiny (= Emin - prec + 1)r-)rOrr@)r.r(r(r*rÁãsz Context.EtinycCst|j|jdƒS)z,Returns maximum exponent (= Emax - prec + 1)r-)rOrAr@)r.r(r(r*rÞçsz Context.EtopcCs|j}||_|S)aÓSets the rounding type. Sets the rounding type, and returns the current (previous) rounding type. Often used like: context = context.copy() # so you don't change the calling context # if an error occurs in the middle. rounding = context._set_rounding(ROUND_UP) val = self.__sub__(other, context=context) context._set_rounding(rounding) This will make it round up for that operation. )r?)r.rfr?r(r(r*r$ëszContext._set_roundingrKcCsjt|tƒr*|| ¡ksd|kr*| td¡St||d�}| ¡r`t|jƒ|j |j kr`| td¡S|  |¡S)z›Creates a new Decimal instance but using self as context. This method implements the to-number operation of the IBM Decimal specification.rLzAtrailing or leading whitespace and underscores are not permitted.)r/zdiagnostic info too long in NaN) rZr[r]r_rrr~rar8r@rÝr²)r.r=r|r(r(r*Úcreate_decimalþs zContext.create_decimalcCst |¡}| |¡S)aÏCreates a new Decimal instance from a float but rounding using self as the context. >>> context = Context(prec=5, rounding=ROUND_DOWN) >>> context.create_decimal_from_float(3.1415926535897932) Decimal('3.1415') >>> context = Context(prec=5, traps=[Inexact]) >>> context.create_decimal_from_float(3.1415926535897932) Traceback (most recent call last): ... decimal.Inexact: None )rrlr²)r.ryr|r(r(r*Úcreate_decimal_from_floats z!Context.create_decimal_from_floatcCst|dd�}|j|d�S)a[Returns the absolute value of the operand. If the operand is negative, the result is the same as using the minus operation on the operand. Otherwise, the result is the same as using the plus operation on the operand. >>> ExtendedContext.abs(Decimal('2.1')) Decimal('2.1') >>> ExtendedContext.abs(Decimal('-100')) Decimal('100') >>> ExtendedContext.abs(Decimal('101.5')) Decimal('101.5') >>> ExtendedContext.abs(Decimal('-101.5')) Decimal('101.5') >>> ExtendedContext.abs(-1) Decimal('1') T)r–)r/)r—r¶)r.rr(r(r*rc!s z Context.abscCs8t|dd�}|j||d�}|tkr0td|ƒ‚n|SdS)a«Return the sum of the two operands. >>> ExtendedContext.add(Decimal('12'), Decimal('7.00')) Decimal('19.00') >>> ExtendedContext.add(Decimal('1E+2'), Decimal('1.01E+4')) Decimal('1.02E+4') >>> ExtendedContext.add(1, Decimal(2)) Decimal('3') >>> ExtendedContext.add(Decimal(8), 5) Decimal('13') >>> ExtendedContext.add(5, 5) Decimal('10') T)r–)r/zUnable to convert %s to DecimalN)r—r¼r�rm)r.rrNrÉr(r(r*Úadd6s  z Context.addcCst| |¡ƒS)N)r[r²)r.rr(r(r*Ú_applyKszContext._applycCst|tƒstdƒ‚| ¡S)zûReturns the same Decimal object. As we do not have different encodings for the same number, the received object already is in its canonical form. >>> ExtendedContext.canonical(Decimal('2.50')) Decimal('2.50') z,canonical requires a Decimal as an argument.)rZrrmr-)r.rr(r(r*r-Ns zContext.canonicalcCst|dd�}|j||d�S)a…Compares values numerically. If the signs of the operands differ, a value representing each operand ('-1' if the operand is less than zero, '0' if the operand is zero or negative zero, or '1' if the operand is greater than zero) is used in place of that operand for the comparison instead of the actual operand. The comparison is then effected by subtracting the second operand from the first and then returning a value according to the result of the subtraction: '-1' if the result is less than zero, '0' if the result is zero or negative zero, or '1' if the result is greater than zero. >>> ExtendedContext.compare(Decimal('2.1'), Decimal('3')) Decimal('-1') >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.1')) Decimal('0') >>> ExtendedContext.compare(Decimal('2.1'), Decimal('2.10')) Decimal('0') >>> ExtendedContext.compare(Decimal('3'), Decimal('2.1')) Decimal('1') >>> ExtendedContext.compare(Decimal('2.1'), Decimal('-3')) Decimal('1') >>> ExtendedContext.compare(Decimal('-3'), Decimal('2.1')) Decimal('-1') >>> ExtendedContext.compare(1, 2) Decimal('-1') >>> ExtendedContext.compare(Decimal(1), 2) Decimal('-1') >>> ExtendedContext.compare(1, Decimal(2)) Decimal('-1') T)r–)r/)r—r˜)r.rrNr(r(r*r˜[s! zContext.comparecCst|dd�}|j||d�S)aCompares the values of the two operands numerically. It's pretty much like compare(), but all NaNs signal, with signaling NaNs taking precedence over quiet NaNs. >>> c = ExtendedContext >>> c.compare_signal(Decimal('2.1'), Decimal('3')) Decimal('-1') >>> c.compare_signal(Decimal('2.1'), Decimal('2.1')) Decimal('0') >>> c.flags[InvalidOperation] = 0 >>> print(c.flags[InvalidOperation]) 0 >>> c.compare_signal(Decimal('NaN'), Decimal('2.1')) Decimal('NaN') >>> print(c.flags[InvalidOperation]) 1 >>> c.flags[InvalidOperation] = 0 >>> print(c.flags[InvalidOperation]) 0 >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1')) Decimal('NaN') >>> print(c.flags[InvalidOperation]) 1 >>> c.compare_signal(-1, 2) Decimal('-1') >>> c.compare_signal(Decimal(-1), 2) Decimal('-1') >>> c.compare_signal(-1, Decimal(2)) Decimal('-1') T)r–)r/)r—r.)r.rrNr(r(r*r.s zContext.compare_signalcCst|dd�}| |¡S)a+Compares two operands using their abstract representation. This is not like the standard compare, which use their numerical value. Note that a total ordering is defined for all possible abstract representations. >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9')) Decimal('-1') >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12')) Decimal('-1') >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3')) Decimal('-1') >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.30')) Decimal('0') >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('12.300')) Decimal('1') >>> ExtendedContext.compare_total(Decimal('12.3'), Decimal('NaN')) Decimal('-1') >>> ExtendedContext.compare_total(1, 2) Decimal('-1') >>> ExtendedContext.compare_total(Decimal(1), 2) Decimal('-1') >>> ExtendedContext.compare_total(1, Decimal(2)) Decimal('-1') T)r–)r—r))r.rrNr(r(r*r)¢s zContext.compare_totalcCst|dd�}| |¡S)z£Compares two operands using their abstract representation ignoring sign. Like compare_total, but with operand's sign ignored and assumed to be 0. T)r–)r—r2)r.rrNr(r(r*r2¿s zContext.compare_total_magcCst|dd�}| ¡S)aReturns a copy of the operand with the sign set to 0. >>> ExtendedContext.copy_abs(Decimal('2.1')) Decimal('2.1') >>> ExtendedContext.copy_abs(Decimal('-100')) Decimal('100') >>> ExtendedContext.copy_abs(-1) Decimal('1') T)r–)r—r°)r.rr(r(r*r°Çs zContext.copy_abscCst|dd�}t|ƒS)aReturns a copy of the decimal object. >>> ExtendedContext.copy_decimal(Decimal('2.1')) Decimal('2.1') >>> ExtendedContext.copy_decimal(Decimal('-1.00')) Decimal('-1.00') >>> ExtendedContext.copy_decimal(1) Decimal('1') T)r–)r—r)r.rr(r(r*Ú copy_decimalÔs zContext.copy_decimalcCst|dd�}| ¡S)a(Returns a copy of the operand with the sign inverted. >>> ExtendedContext.copy_negate(Decimal('101.5')) Decimal('-101.5') >>> ExtendedContext.copy_negate(Decimal('-101.5')) Decimal('101.5') >>> ExtendedContext.copy_negate(1) Decimal('-1') T)r–)r—r±)r.rr(r(r*r±ás zContext.copy_negatecCst|dd�}| |¡S)aCopies the second operand's sign to the first one. In detail, it returns a copy of the first operand with the sign equal to the sign of the second operand. >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33')) Decimal('1.50') >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33')) Decimal('1.50') >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33')) Decimal('-1.50') >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33')) Decimal('-1.50') >>> ExtendedContext.copy_sign(1, -2) Decimal('-1') >>> ExtendedContext.copy_sign(Decimal(1), -2) Decimal('-1') >>> ExtendedContext.copy_sign(1, Decimal(-2)) Decimal('-1') T)r–)r—r3)r.rrNr(r(r*r3îs zContext.copy_signcCs8t|dd�}|j||d�}|tkr0td|ƒ‚n|SdS)aˆDecimal division in a specified context. >>> ExtendedContext.divide(Decimal('1'), Decimal('3')) Decimal('0.333333333') >>> ExtendedContext.divide(Decimal('2'), Decimal('3')) Decimal('0.666666667') >>> ExtendedContext.divide(Decimal('5'), Decimal('2')) Decimal('2.5') >>> ExtendedContext.divide(Decimal('1'), Decimal('10')) Decimal('0.1') >>> ExtendedContext.divide(Decimal('12'), Decimal('12')) Decimal('1') >>> ExtendedContext.divide(Decimal('8.00'), Decimal('2')) Decimal('4.00') >>> ExtendedContext.divide(Decimal('2.400'), Decimal('2.0')) Decimal('1.20') >>> ExtendedContext.divide(Decimal('1000'), Decimal('100')) Decimal('10') >>> ExtendedContext.divide(Decimal('1000'), Decimal('1')) Decimal('1000') >>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2')) Decimal('1.20E+6') >>> ExtendedContext.divide(5, 5) Decimal('1') >>> ExtendedContext.divide(Decimal(5), 5) Decimal('1') >>> ExtendedContext.divide(5, Decimal(5)) Decimal('1') T)r–)r/zUnable to convert %s to DecimalN)r—rÆr�rm)r.rrNrÉr(r(r*Údivides  zContext.dividecCs8t|dd�}|j||d�}|tkr0td|ƒ‚n|SdS)a/Divides two numbers and returns the integer part of the result. >>> ExtendedContext.divide_int(Decimal('2'), Decimal('3')) Decimal('0') >>> ExtendedContext.divide_int(Decimal('10'), Decimal('3')) Decimal('3') >>> ExtendedContext.divide_int(Decimal('1'), Decimal('0.3')) Decimal('3') >>> ExtendedContext.divide_int(10, 3) Decimal('3') >>> ExtendedContext.divide_int(Decimal(10), 3) Decimal('3') >>> ExtendedContext.divide_int(10, Decimal(3)) Decimal('3') T)r–)r/zUnable to convert %s to DecimalN)r—rÒr�rm)r.rrNrÉr(r(r*Ú divide_int+s  zContext.divide_intcCs8t|dd�}|j||d�}|tkr0td|ƒ‚n|SdS)aÝReturn (a // b, a % b). >>> ExtendedContext.divmod(Decimal(8), Decimal(3)) (Decimal('2'), Decimal('2')) >>> ExtendedContext.divmod(Decimal(8), Decimal(4)) (Decimal('2'), Decimal('0')) >>> ExtendedContext.divmod(8, 4) (Decimal('2'), Decimal('0')) >>> ExtendedContext.divmod(Decimal(8), 4) (Decimal('2'), Decimal('0')) >>> ExtendedContext.divmod(8, Decimal(4)) (Decimal('2'), Decimal('0')) T)r–)r/zUnable to convert %s to DecimalN)r—rÌr�rm)r.rrNrÉr(r(r*rÂBs  zContext.divmodcCst|dd�}|j|d�S)a#Returns e ** a. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.exp(Decimal('-Infinity')) Decimal('0') >>> c.exp(Decimal('-1')) Decimal('0.367879441') >>> c.exp(Decimal('0')) Decimal('1') >>> c.exp(Decimal('1')) Decimal('2.71828183') >>> c.exp(Decimal('0.693147181')) Decimal('2.00000000') >>> c.exp(Decimal('+Infinity')) Decimal('Infinity') >>> c.exp(10) Decimal('22026.4658') T)r–)r/)r—rP)r.rr(r(r*rPWs z Context.expcCst|dd�}|j|||d�S)a Returns a multiplied by b, plus c. The first two operands are multiplied together, using multiply, the third operand is then added to the result of that multiplication, using add, all with only one final rounding. >>> ExtendedContext.fma(Decimal('3'), Decimal('5'), Decimal('7')) Decimal('22') >>> ExtendedContext.fma(Decimal('3'), Decimal('-5'), Decimal('7')) Decimal('-8') >>> ExtendedContext.fma(Decimal('888565290'), Decimal('1557.96930'), Decimal('-86087.7578')) Decimal('1.38435736E+12') >>> ExtendedContext.fma(1, 3, 4) Decimal('7') >>> ExtendedContext.fma(1, Decimal(3), 4) Decimal('7') >>> ExtendedContext.fma(1, 3, Decimal(4)) Decimal('7') T)r–)r/)r—rò)r.rrNr&r(r(r*ròos z Context.fmacCst|tƒstdƒ‚| ¡S)aReturn True if the operand is canonical; otherwise return False. Currently, the encoding of a Decimal instance is always canonical, so this method returns True for any Decimal. >>> ExtendedContext.is_canonical(Decimal('2.50')) True z/is_canonical requires a Decimal as an argument.)rZrrmr6)r.rr(r(r*r6†s zContext.is_canonicalcCst|dd�}| ¡S)a,Return True if the operand is finite; otherwise return False. A Decimal instance is considered finite if it is neither infinite nor a NaN. >>> ExtendedContext.is_finite(Decimal('2.50')) True >>> ExtendedContext.is_finite(Decimal('-0.3')) True >>> ExtendedContext.is_finite(Decimal('0')) True >>> ExtendedContext.is_finite(Decimal('Inf')) False >>> ExtendedContext.is_finite(Decimal('NaN')) False >>> ExtendedContext.is_finite(1) True T)r–)r—r7)r.rr(r(r*r7“s zContext.is_finitecCst|dd�}| ¡S)aUReturn True if the operand is infinite; otherwise return False. >>> ExtendedContext.is_infinite(Decimal('2.50')) False >>> ExtendedContext.is_infinite(Decimal('-Inf')) True >>> ExtendedContext.is_infinite(Decimal('NaN')) False >>> ExtendedContext.is_infinite(1) False T)r–)r—r)r.rr(r(r*r©s zContext.is_infinitecCst|dd�}| ¡S)aOReturn True if the operand is a qNaN or sNaN; otherwise return False. >>> ExtendedContext.is_nan(Decimal('2.50')) False >>> ExtendedContext.is_nan(Decimal('NaN')) True >>> ExtendedContext.is_nan(Decimal('-sNaN')) True >>> ExtendedContext.is_nan(1) False T)r–)r—r›)r.rr(r(r*r›¸s zContext.is_nancCst|dd�}|j|d�S)aïReturn True if the operand is a normal number; otherwise return False. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.is_normal(Decimal('2.50')) True >>> c.is_normal(Decimal('0.1E-999')) False >>> c.is_normal(Decimal('0.00')) False >>> c.is_normal(Decimal('-Inf')) False >>> c.is_normal(Decimal('NaN')) False >>> c.is_normal(1) True T)r–)r/)r—r8)r.rr(r(r*r8Ès zContext.is_normalcCst|dd�}| ¡S)aHReturn True if the operand is a quiet NaN; otherwise return False. >>> ExtendedContext.is_qnan(Decimal('2.50')) False >>> ExtendedContext.is_qnan(Decimal('NaN')) True >>> ExtendedContext.is_qnan(Decimal('sNaN')) False >>> ExtendedContext.is_qnan(1) False T)r–)r—r‡)r.rr(r(r*r‡ßs zContext.is_qnancCst|dd�}| ¡S)a�Return True if the operand is negative; otherwise return False. >>> ExtendedContext.is_signed(Decimal('2.50')) False >>> ExtendedContext.is_signed(Decimal('-12')) True >>> ExtendedContext.is_signed(Decimal('-0')) True >>> ExtendedContext.is_signed(8) False >>> ExtendedContext.is_signed(-8) True T)r–)r—r9)r.rr(r(r*r9îs zContext.is_signedcCst|dd�}| ¡S)aTReturn True if the operand is a signaling NaN; otherwise return False. >>> ExtendedContext.is_snan(Decimal('2.50')) False >>> ExtendedContext.is_snan(Decimal('NaN')) False >>> ExtendedContext.is_snan(Decimal('sNaN')) True >>> ExtendedContext.is_snan(1) False T)r–)r—r†)r.rr(r(r*r†ÿs zContext.is_snancCst|dd�}|j|d�S)aôReturn True if the operand is subnormal; otherwise return False. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.is_subnormal(Decimal('2.50')) False >>> c.is_subnormal(Decimal('0.1E-999')) True >>> c.is_subnormal(Decimal('0.00')) False >>> c.is_subnormal(Decimal('-Inf')) False >>> c.is_subnormal(Decimal('NaN')) False >>> c.is_subnormal(1) False T)r–)r/)r—r:)r.rr(r(r*r:s zContext.is_subnormalcCst|dd�}| ¡S)auReturn True if the operand is a zero; otherwise return False. >>> ExtendedContext.is_zero(Decimal('0')) True >>> ExtendedContext.is_zero(Decimal('2.50')) False >>> ExtendedContext.is_zero(Decimal('-0E+2')) True >>> ExtendedContext.is_zero(1) False >>> ExtendedContext.is_zero(0) True T)r–)r—r;)r.rr(r(r*r;%s zContext.is_zerocCst|dd�}|j|d�S)aþReturns the natural (base e) logarithm of the operand. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.ln(Decimal('0')) Decimal('-Infinity') >>> c.ln(Decimal('1.000')) Decimal('0') >>> c.ln(Decimal('2.71828183')) Decimal('1.00000000') >>> c.ln(Decimal('10')) Decimal('2.30258509') >>> c.ln(Decimal('+Infinity')) Decimal('Infinity') >>> c.ln(1) Decimal('0') T)r–)r/)r—rC)r.rr(r(r*rC6s z Context.lncCst|dd�}|j|d�S)a§Returns the base 10 logarithm of the operand. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.log10(Decimal('0')) Decimal('-Infinity') >>> c.log10(Decimal('0.001')) Decimal('-3') >>> c.log10(Decimal('1.000')) Decimal('0') >>> c.log10(Decimal('2')) Decimal('0.301029996') >>> c.log10(Decimal('10')) Decimal('1') >>> c.log10(Decimal('70')) Decimal('1.84509804') >>> c.log10(Decimal('+Infinity')) Decimal('Infinity') >>> c.log10(0) Decimal('-Infinity') >>> c.log10(1) Decimal('0') T)r–)r/)r—rF)r.rr(r(r*rFLs z Context.log10cCst|dd�}|j|d�S)a4 Returns the exponent of the magnitude of the operand's MSD. The result is the integer which is the exponent of the magnitude of the most significant digit of the operand (as though the operand were truncated to a single digit while maintaining the value of that digit and without limiting the resulting exponent). >>> ExtendedContext.logb(Decimal('250')) Decimal('2') >>> ExtendedContext.logb(Decimal('2.50')) Decimal('0') >>> ExtendedContext.logb(Decimal('0.03')) Decimal('-2') >>> ExtendedContext.logb(Decimal('0')) Decimal('-Infinity') >>> ExtendedContext.logb(1) Decimal('0') >>> ExtendedContext.logb(10) Decimal('1') >>> ExtendedContext.logb(100) Decimal('2') T)r–)r/)r—rG)r.rr(r(r*rGhs z Context.logbcCst|dd�}|j||d�S)a”Applies the logical operation 'and' between each operand's digits. The operands must be both logical numbers. >>> ExtendedContext.logical_and(Decimal('0'), Decimal('0')) Decimal('0') >>> ExtendedContext.logical_and(Decimal('0'), Decimal('1')) Decimal('0') >>> ExtendedContext.logical_and(Decimal('1'), Decimal('0')) Decimal('0') >>> ExtendedContext.logical_and(Decimal('1'), Decimal('1')) Decimal('1') >>> ExtendedContext.logical_and(Decimal('1100'), Decimal('1010')) Decimal('1000') >>> ExtendedContext.logical_and(Decimal('1111'), Decimal('10')) Decimal('10') >>> ExtendedContext.logical_and(110, 1101) Decimal('100') >>> ExtendedContext.logical_and(Decimal(110), 1101) Decimal('100') >>> ExtendedContext.logical_and(110, Decimal(1101)) Decimal('100') T)r–)r/)r—rQ)r.rrNr(r(r*rQ‚s zContext.logical_andcCst|dd�}|j|d�S)a Invert all the digits in the operand. The operand must be a logical number. >>> ExtendedContext.logical_invert(Decimal('0')) Decimal('111111111') >>> ExtendedContext.logical_invert(Decimal('1')) Decimal('111111110') >>> ExtendedContext.logical_invert(Decimal('111111111')) Decimal('0') >>> ExtendedContext.logical_invert(Decimal('101010101')) Decimal('10101010') >>> ExtendedContext.logical_invert(1101) Decimal('111110010') T)r–)r/)r—rS)r.rr(r(r*rS�s zContext.logical_invertcCst|dd�}|j||d�S)a�Applies the logical operation 'or' between each operand's digits. The operands must be both logical numbers. >>> ExtendedContext.logical_or(Decimal('0'), Decimal('0')) Decimal('0') >>> ExtendedContext.logical_or(Decimal('0'), Decimal('1')) Decimal('1') >>> ExtendedContext.logical_or(Decimal('1'), Decimal('0')) Decimal('1') >>> ExtendedContext.logical_or(Decimal('1'), Decimal('1')) Decimal('1') >>> ExtendedContext.logical_or(Decimal('1100'), Decimal('1010')) Decimal('1110') >>> ExtendedContext.logical_or(Decimal('1110'), Decimal('10')) Decimal('1110') >>> ExtendedContext.logical_or(110, 1101) Decimal('1111') >>> ExtendedContext.logical_or(Decimal(110), 1101) Decimal('1111') >>> ExtendedContext.logical_or(110, Decimal(1101)) Decimal('1111') T)r–)r/)r—rT)r.rrNr(r(r*rT°s zContext.logical_orcCst|dd�}|j||d�S)a˜Applies the logical operation 'xor' between each operand's digits. The operands must be both logical numbers. >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('0')) Decimal('0') >>> ExtendedContext.logical_xor(Decimal('0'), Decimal('1')) Decimal('1') >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('0')) Decimal('1') >>> ExtendedContext.logical_xor(Decimal('1'), Decimal('1')) Decimal('0') >>> ExtendedContext.logical_xor(Decimal('1100'), Decimal('1010')) Decimal('110') >>> ExtendedContext.logical_xor(Decimal('1111'), Decimal('10')) Decimal('1101') >>> ExtendedContext.logical_xor(110, 1101) Decimal('1011') >>> ExtendedContext.logical_xor(Decimal(110), 1101) Decimal('1011') >>> ExtendedContext.logical_xor(110, Decimal(1101)) Decimal('1011') T)r–)r/)r—rR)r.rrNr(r(r*rRËs zContext.logical_xorcCst|dd�}|j||d�S)a³max compares two values numerically and returns the maximum. If either operand is a NaN then the general rules apply. Otherwise, the operands are compared as though by the compare operation. If they are numerically equal then the left-hand operand is chosen as the result. Otherwise the maximum (closer to positive infinity) of the two operands is chosen as the result. >>> ExtendedContext.max(Decimal('3'), Decimal('2')) Decimal('3') >>> ExtendedContext.max(Decimal('-10'), Decimal('3')) Decimal('3') >>> ExtendedContext.max(Decimal('1.0'), Decimal('1')) Decimal('1') >>> ExtendedContext.max(Decimal('7'), Decimal('NaN')) Decimal('7') >>> ExtendedContext.max(1, 2) Decimal('2') >>> ExtendedContext.max(Decimal(1), 2) Decimal('2') >>> ExtendedContext.max(1, Decimal(2)) Decimal('2') T)r–)r/)r—r·)r.rrNr(r(r*r·æs z Context.maxcCst|dd�}|j||d�S)aÇCompares the values numerically with their sign ignored. >>> ExtendedContext.max_mag(Decimal('7'), Decimal('NaN')) Decimal('7') >>> ExtendedContext.max_mag(Decimal('7'), Decimal('-10')) Decimal('-10') >>> ExtendedContext.max_mag(1, -2) Decimal('-2') >>> ExtendedContext.max_mag(Decimal(1), -2) Decimal('-2') >>> ExtendedContext.max_mag(1, Decimal(-2)) Decimal('-2') T)r–)r/)r—rU)r.rrNr(r(r*rUs zContext.max_magcCst|dd�}|j||d�S)a¸min compares two values numerically and returns the minimum. If either operand is a NaN then the general rules apply. Otherwise, the operands are compared as though by the compare operation. If they are numerically equal then the left-hand operand is chosen as the result. Otherwise the minimum (closer to negative infinity) of the two operands is chosen as the result. >>> ExtendedContext.min(Decimal('3'), Decimal('2')) Decimal('2') >>> ExtendedContext.min(Decimal('-10'), Decimal('3')) Decimal('-10') >>> ExtendedContext.min(Decimal('1.0'), Decimal('1')) Decimal('1.0') >>> ExtendedContext.min(Decimal('7'), Decimal('NaN')) Decimal('7') >>> ExtendedContext.min(1, 2) Decimal('1') >>> ExtendedContext.min(Decimal(1), 2) Decimal('1') >>> ExtendedContext.min(1, Decimal(29)) Decimal('1') T)r–)r/)r—r¤)r.rrNr(r(r*r¤s z Context.mincCst|dd�}|j||d�S)aÄCompares the values numerically with their sign ignored. >>> ExtendedContext.min_mag(Decimal('3'), Decimal('-2')) Decimal('-2') >>> ExtendedContext.min_mag(Decimal('-3'), Decimal('NaN')) Decimal('-3') >>> ExtendedContext.min_mag(1, -2) Decimal('1') >>> ExtendedContext.min_mag(Decimal(1), -2) Decimal('1') >>> ExtendedContext.min_mag(1, Decimal(-2)) Decimal('1') T)r–)r/)r—rV)r.rrNr(r(r*rV-s zContext.min_magcCst|dd�}|j|d�S)aÎMinus corresponds to unary prefix minus in Python. The operation is evaluated using the same rules as subtract; the operation minus(a) is calculated as subtract('0', a) where the '0' has the same exponent as the operand. >>> ExtendedContext.minus(Decimal('1.3')) Decimal('-1.3') >>> ExtendedContext.minus(Decimal('-1.3')) Decimal('1.3') >>> ExtendedContext.minus(1) Decimal('-1') T)r–)r/)r—r³)r.rr(r(r*Úminus>s z Context.minuscCs8t|dd�}|j||d�}|tkr0td|ƒ‚n|SdS)aàmultiply multiplies two operands. If either operand is a special value then the general rules apply. Otherwise, the operands are multiplied together ('long multiplication'), resulting in a number which may be as long as the sum of the lengths of the two operands. >>> ExtendedContext.multiply(Decimal('1.20'), Decimal('3')) Decimal('3.60') >>> ExtendedContext.multiply(Decimal('7'), Decimal('3')) Decimal('21') >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('0.8')) Decimal('0.72') >>> ExtendedContext.multiply(Decimal('0.9'), Decimal('-0')) Decimal('-0.0') >>> ExtendedContext.multiply(Decimal('654321'), Decimal('654321')) Decimal('4.28135971E+11') >>> ExtendedContext.multiply(7, 7) Decimal('49') >>> ExtendedContext.multiply(Decimal(7), 7) Decimal('49') >>> ExtendedContext.multiply(7, Decimal(7)) Decimal('49') T)r–)r/zUnable to convert %s to DecimalN)r—rÀr�rm)r.rrNrÉr(r(r*ÚmultiplyOs  zContext.multiplycCst|dd�}|j|d�S)a"Returns the largest representable number smaller than a. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> ExtendedContext.next_minus(Decimal('1')) Decimal('0.999999999') >>> c.next_minus(Decimal('1E-1007')) Decimal('0E-1007') >>> ExtendedContext.next_minus(Decimal('-1.00000003')) Decimal('-1.00000004') >>> c.next_minus(Decimal('Infinity')) Decimal('9.99999999E+999') >>> c.next_minus(1) Decimal('0.999999999') T)r–)r/)r—rY)r.rr(r(r*rYos zContext.next_minuscCst|dd�}|j|d�S)aReturns the smallest representable number larger than a. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> ExtendedContext.next_plus(Decimal('1')) Decimal('1.00000001') >>> c.next_plus(Decimal('-1E-1007')) Decimal('-0E-1007') >>> ExtendedContext.next_plus(Decimal('-1.00000003')) Decimal('-1.00000002') >>> c.next_plus(Decimal('-Infinity')) Decimal('-9.99999999E+999') >>> c.next_plus(1) Decimal('1.00000001') T)r–)r/)r—rZ)r.rr(r(r*rZƒs zContext.next_pluscCst|dd�}|j||d�S)a´Returns the number closest to a, in direction towards b. The result is the closest representable number from the first operand (but not the first operand) that is in the direction towards the second operand, unless the operands have the same value. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.next_toward(Decimal('1'), Decimal('2')) Decimal('1.00000001') >>> c.next_toward(Decimal('-1E-1007'), Decimal('1')) Decimal('-0E-1007') >>> c.next_toward(Decimal('-1.00000003'), Decimal('0')) Decimal('-1.00000002') >>> c.next_toward(Decimal('1'), Decimal('0')) Decimal('0.999999999') >>> c.next_toward(Decimal('1E-1007'), Decimal('-100')) Decimal('0E-1007') >>> c.next_toward(Decimal('-1.00000003'), Decimal('-10')) Decimal('-1.00000004') >>> c.next_toward(Decimal('0.00'), Decimal('-0.0000')) Decimal('-0.00') >>> c.next_toward(0, 1) Decimal('1E-1007') >>> c.next_toward(Decimal(0), 1) Decimal('1E-1007') >>> c.next_toward(0, Decimal(1)) Decimal('1E-1007') T)r–)r/)r—r[)r.rrNr(r(r*r[—s zContext.next_towardcCst|dd�}|j|d�S)a³normalize reduces an operand to its simplest form. Essentially a plus operation with all trailing zeros removed from the result. >>> ExtendedContext.normalize(Decimal('2.1')) Decimal('2.1') >>> ExtendedContext.normalize(Decimal('-2.0')) Decimal('-2') >>> ExtendedContext.normalize(Decimal('1.200')) Decimal('1.2') >>> ExtendedContext.normalize(Decimal('-120')) Decimal('-1.2E+2') >>> ExtendedContext.normalize(Decimal('120.00')) Decimal('1.2E+2') >>> ExtendedContext.normalize(Decimal('0.00')) Decimal('0') >>> ExtendedContext.normalize(6) Decimal('6') T)r–)r/)r—r)r.rr(r(r*rºs zContext.normalizecCst|dd�}|j|d�S)aâReturns an indication of the class of the operand. The class is one of the following strings: -sNaN -NaN -Infinity -Normal -Subnormal -Zero +Zero +Subnormal +Normal +Infinity >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.number_class(Decimal('Infinity')) '+Infinity' >>> c.number_class(Decimal('1E-10')) '+Normal' >>> c.number_class(Decimal('2.50')) '+Normal' >>> c.number_class(Decimal('0.1E-999')) '+Subnormal' >>> c.number_class(Decimal('0')) '+Zero' >>> c.number_class(Decimal('-0')) '-Zero' >>> c.number_class(Decimal('-0.1E-999')) '-Subnormal' >>> c.number_class(Decimal('-1E-10')) '-Normal' >>> c.number_class(Decimal('-2.50')) '-Normal' >>> c.number_class(Decimal('-Infinity')) '-Infinity' >>> c.number_class(Decimal('NaN')) 'NaN' >>> c.number_class(Decimal('-NaN')) 'NaN' >>> c.number_class(Decimal('sNaN')) 'sNaN' >>> c.number_class(123) '+Normal' T)r–)r/)r—r])r.rr(r(r*r]Òs/ zContext.number_classcCst|dd�}|j|d�S)a¿Plus corresponds to unary prefix plus in Python. The operation is evaluated using the same rules as add; the operation plus(a) is calculated as add('0', a) where the '0' has the same exponent as the operand. >>> ExtendedContext.plus(Decimal('1.3')) Decimal('1.3') >>> ExtendedContext.plus(Decimal('-1.3')) Decimal('-1.3') >>> ExtendedContext.plus(-1) Decimal('-1') T)r–)r/)r—r´)r.rr(r(r*Úpluss z Context.pluscCs:t|dd�}|j|||d�}|tkr2td|ƒ‚n|SdS)a Raises a to the power of b, to modulo if given. With two arguments, compute a**b. If a is negative then b must be integral. The result will be inexact unless b is integral and the result is finite and can be expressed exactly in 'precision' digits. With three arguments, compute (a**b) % modulo. For the three argument form, the following restrictions on the arguments hold: - all three arguments must be integral - b must be nonnegative - at least one of a or b must be nonzero - modulo must be nonzero and have at most 'precision' digits The result of pow(a, b, modulo) is identical to the result that would be obtained by computing (a**b) % modulo with unbounded precision, but is computed more efficiently. It is always exact. >>> c = ExtendedContext.copy() >>> c.Emin = -999 >>> c.Emax = 999 >>> c.power(Decimal('2'), Decimal('3')) Decimal('8') >>> c.power(Decimal('-2'), Decimal('3')) Decimal('-8') >>> c.power(Decimal('2'), Decimal('-3')) Decimal('0.125') >>> c.power(Decimal('1.7'), Decimal('8')) Decimal('69.7575744') >>> c.power(Decimal('10'), Decimal('0.301029996')) Decimal('2.00000000') >>> c.power(Decimal('Infinity'), Decimal('-1')) Decimal('0') >>> c.power(Decimal('Infinity'), Decimal('0')) Decimal('1') >>> c.power(Decimal('Infinity'), Decimal('1')) Decimal('Infinity') >>> c.power(Decimal('-Infinity'), Decimal('-1')) Decimal('-0') >>> c.power(Decimal('-Infinity'), Decimal('0')) Decimal('1') >>> c.power(Decimal('-Infinity'), Decimal('1')) Decimal('-Infinity') >>> c.power(Decimal('-Infinity'), Decimal('2')) Decimal('Infinity') >>> c.power(Decimal('0'), Decimal('0')) Decimal('NaN') >>> c.power(Decimal('3'), Decimal('7'), Decimal('16')) Decimal('11') >>> c.power(Decimal('-3'), Decimal('7'), Decimal('16')) Decimal('-11') >>> c.power(Decimal('-3'), Decimal('8'), Decimal('16')) Decimal('1') >>> c.power(Decimal('3'), Decimal('7'), Decimal('-16')) Decimal('11') >>> c.power(Decimal('23E12345'), Decimal('67E189'), Decimal('123456789')) Decimal('11729830') >>> c.power(Decimal('-0'), Decimal('17'), Decimal('1729')) Decimal('-0') >>> c.power(Decimal('-23'), Decimal('0'), Decimal('65537')) Decimal('1') >>> ExtendedContext.power(7, 7) Decimal('823543') >>> ExtendedContext.power(Decimal(7), 7) Decimal('823543') >>> ExtendedContext.power(7, Decimal(7), 2) Decimal('1') T)r–)r/zUnable to convert %s to DecimalN)r—rr�rm)r.rrNr÷rÉr(r(r*Úpowers I z Context.powercCst|dd�}|j||d�S)a Returns a value equal to 'a' (rounded), having the exponent of 'b'. The coefficient of the result is derived from that of the left-hand operand. It may be rounded using the current rounding setting (if the exponent is being increased), multiplied by a positive power of ten (if the exponent is being decreased), or is unchanged (if the exponent is already equal to that of the right-hand operand). Unlike other operations, if the length of the coefficient after the quantize operation would be greater than precision then an Invalid operation condition is raised. This guarantees that, unless there is an error condition, the exponent of the result of a quantize is always equal to that of the right-hand operand. Also unlike other operations, quantize will never raise Underflow, even if the result is subnormal and inexact. >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.001')) Decimal('2.170') >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.01')) Decimal('2.17') >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('0.1')) Decimal('2.2') >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+0')) Decimal('2') >>> ExtendedContext.quantize(Decimal('2.17'), Decimal('1e+1')) Decimal('0E+1') >>> ExtendedContext.quantize(Decimal('-Inf'), Decimal('Infinity')) Decimal('-Infinity') >>> ExtendedContext.quantize(Decimal('2'), Decimal('Infinity')) Decimal('NaN') >>> ExtendedContext.quantize(Decimal('-0.1'), Decimal('1')) Decimal('-0') >>> ExtendedContext.quantize(Decimal('-0'), Decimal('1e+5')) Decimal('-0E+5') >>> ExtendedContext.quantize(Decimal('+35236450.6'), Decimal('1e-2')) Decimal('NaN') >>> ExtendedContext.quantize(Decimal('-35236450.6'), Decimal('1e-2')) Decimal('NaN') >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-1')) Decimal('217.0') >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e-0')) Decimal('217') >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+1')) Decimal('2.2E+2') >>> ExtendedContext.quantize(Decimal('217'), Decimal('1e+2')) Decimal('2E+2') >>> ExtendedContext.quantize(1, 2) Decimal('1') >>> ExtendedContext.quantize(Decimal(1), 2) Decimal('1') >>> ExtendedContext.quantize(1, Decimal(2)) Decimal('1') T)r–)r/)r—rí)r.rrNr(r(r*ríes7 zContext.quantizecCstdƒS)zkJust returns 10, as this is Decimal, :) >>> ExtendedContext.radix() Decimal('10') r™)r)r.r(r(r*r^Ÿsz Context.radixcCs8t|dd�}|j||d�}|tkr0td|ƒ‚n|SdS)aReturns the remainder from integer division. The result is the residue of the dividend after the operation of calculating integer division as described for divide-integer, rounded to precision digits if necessary. The sign of the result, if non-zero, is the same as that of the original dividend. This operation will fail under the same conditions as integer division (that is, if integer division on the same two operands would fail, the remainder cannot be calculated). >>> ExtendedContext.remainder(Decimal('2.1'), Decimal('3')) Decimal('2.1') >>> ExtendedContext.remainder(Decimal('10'), Decimal('3')) Decimal('1') >>> ExtendedContext.remainder(Decimal('-10'), Decimal('3')) Decimal('-1') >>> ExtendedContext.remainder(Decimal('10.2'), Decimal('1')) Decimal('0.2') >>> ExtendedContext.remainder(Decimal('10'), Decimal('0.3')) Decimal('0.1') >>> ExtendedContext.remainder(Decimal('3.6'), Decimal('1.3')) Decimal('1.0') >>> ExtendedContext.remainder(22, 6) Decimal('4') >>> ExtendedContext.remainder(Decimal(22), 6) Decimal('4') >>> ExtendedContext.remainder(22, Decimal(6)) Decimal('4') T)r–)r/zUnable to convert %s to DecimalN)r—rÎr�rm)r.rrNrÉr(r(r*rħs  zContext.remaindercCst|dd�}|j||d�S)aGReturns to be "a - b * n", where n is the integer nearest the exact value of "x / b" (if two integers are equally near then the even one is chosen). If the result is equal to 0 then its sign will be the sign of a. This operation will fail under the same conditions as integer division (that is, if integer division on the same two operands would fail, the remainder cannot be calculated). >>> ExtendedContext.remainder_near(Decimal('2.1'), Decimal('3')) Decimal('-0.9') >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('6')) Decimal('-2') >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('3')) Decimal('1') >>> ExtendedContext.remainder_near(Decimal('-10'), Decimal('3')) Decimal('-1') >>> ExtendedContext.remainder_near(Decimal('10.2'), Decimal('1')) Decimal('0.2') >>> ExtendedContext.remainder_near(Decimal('10'), Decimal('0.3')) Decimal('0.1') >>> ExtendedContext.remainder_near(Decimal('3.6'), Decimal('1.3')) Decimal('-0.3') >>> ExtendedContext.remainder_near(3, 11) Decimal('3') >>> ExtendedContext.remainder_near(Decimal(3), 11) Decimal('3') >>> ExtendedContext.remainder_near(3, Decimal(11)) Decimal('3') T)r–)r/)r—rÑ)r.rrNr(r(r*rÑÍs zContext.remainder_nearcCst|dd�}|j||d�S)aNReturns a rotated copy of a, b times. The coefficient of the result is a rotated copy of the digits in the coefficient of the first operand. The number of places of rotation is taken from the absolute value of the second operand, with the rotation being to the left if the second operand is positive or to the right otherwise. >>> ExtendedContext.rotate(Decimal('34'), Decimal('8')) Decimal('400000003') >>> ExtendedContext.rotate(Decimal('12'), Decimal('9')) Decimal('12') >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('-2')) Decimal('891234567') >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('0')) Decimal('123456789') >>> ExtendedContext.rotate(Decimal('123456789'), Decimal('+2')) Decimal('345678912') >>> ExtendedContext.rotate(1333333, 1) Decimal('13333330') >>> ExtendedContext.rotate(Decimal(1333333), 1) Decimal('13333330') >>> ExtendedContext.rotate(1333333, Decimal(1)) Decimal('13333330') T)r–)r/)r—rb)r.rrNr(r(r*rbïs zContext.rotatecCst|dd�}| |¡S)aÝReturns True if the two operands have the same exponent. The result is never affected by either the sign or the coefficient of either operand. >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.001')) False >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('0.01')) True >>> ExtendedContext.same_quantum(Decimal('2.17'), Decimal('1')) False >>> ExtendedContext.same_quantum(Decimal('Inf'), Decimal('-Inf')) True >>> ExtendedContext.same_quantum(10000, -1) True >>> ExtendedContext.same_quantum(Decimal(10000), -1) True >>> ExtendedContext.same_quantum(10000, Decimal(-1)) True T)r–)r—r)r.rrNr(r(r*r s zContext.same_quantumcCst|dd�}|j||d�S)a3Returns the first operand after adding the second value its exp. >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('-2')) Decimal('0.0750') >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('0')) Decimal('7.50') >>> ExtendedContext.scaleb(Decimal('7.50'), Decimal('3')) Decimal('7.50E+3') >>> ExtendedContext.scaleb(1, 4) Decimal('1E+4') >>> ExtendedContext.scaleb(Decimal(1), 4) Decimal('1E+4') >>> ExtendedContext.scaleb(1, Decimal(4)) Decimal('1E+4') T)r–)r/)r—rc)r.rrNr(r(r*rc$s zContext.scalebcCst|dd�}|j||d�S)a{Returns a shifted copy of a, b times. The coefficient of the result is a shifted copy of the digits in the coefficient of the first operand. The number of places to shift is taken from the absolute value of the second operand, with the shift being to the left if the second operand is positive or to the right otherwise. Digits shifted into the coefficient are zeros. >>> ExtendedContext.shift(Decimal('34'), Decimal('8')) Decimal('400000000') >>> ExtendedContext.shift(Decimal('12'), Decimal('9')) Decimal('0') >>> ExtendedContext.shift(Decimal('123456789'), Decimal('-2')) Decimal('1234567') >>> ExtendedContext.shift(Decimal('123456789'), Decimal('0')) Decimal('123456789') >>> ExtendedContext.shift(Decimal('123456789'), Decimal('+2')) Decimal('345678900') >>> ExtendedContext.shift(88888888, 2) Decimal('888888800') >>> ExtendedContext.shift(Decimal(88888888), 2) Decimal('888888800') >>> ExtendedContext.shift(88888888, Decimal(2)) Decimal('888888800') T)r–)r/)r—rÃ)r.rrNr(r(r*rÃ7s z Context.shiftcCst|dd�}|j|d�S)a¦Square root of a non-negative number to context precision. If the result must be inexact, it is rounded using the round-half-even algorithm. >>> ExtendedContext.sqrt(Decimal('0')) Decimal('0') >>> ExtendedContext.sqrt(Decimal('-0')) Decimal('-0') >>> ExtendedContext.sqrt(Decimal('0.39')) Decimal('0.624499800') >>> ExtendedContext.sqrt(Decimal('100')) Decimal('10') >>> ExtendedContext.sqrt(Decimal('1')) Decimal('1') >>> ExtendedContext.sqrt(Decimal('1.0')) Decimal('1.0') >>> ExtendedContext.sqrt(Decimal('1.00')) Decimal('1.0') >>> ExtendedContext.sqrt(Decimal('7')) Decimal('2.64575131') >>> ExtendedContext.sqrt(Decimal('10')) Decimal('3.16227766') >>> ExtendedContext.sqrt(2) Decimal('1.41421356') >>> ExtendedContext.prec 9 T)r–)r/)r—r()r.rr(r(r*r(Us z Context.sqrtcCs8t|dd�}|j||d�}|tkr0td|ƒ‚n|SdS)a&Return the difference between the two operands. >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.07')) Decimal('0.23') >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('1.30')) Decimal('0.00') >>> ExtendedContext.subtract(Decimal('1.3'), Decimal('2.07')) Decimal('-0.77') >>> ExtendedContext.subtract(8, 5) Decimal('3') >>> ExtendedContext.subtract(Decimal(8), 5) Decimal('3') >>> ExtendedContext.subtract(8, Decimal(5)) Decimal('3') T)r–)r/zUnable to convert %s to DecimalN)r—r½r�rm)r.rrNrÉr(r(r*Úsubtractus  zContext.subtractcCst|dd�}|j|d�S)a…Convert to a string, using engineering notation if an exponent is needed. Engineering notation has an exponent which is a multiple of 3. This can leave up to 3 digits to the left of the decimal place and may require the addition of either one or two trailing zeros. The operation is not affected by the context. >>> ExtendedContext.to_eng_string(Decimal('123E+1')) '1.23E+3' >>> ExtendedContext.to_eng_string(Decimal('123E+3')) '123E+3' >>> ExtendedContext.to_eng_string(Decimal('123E-10')) '12.3E-9' >>> ExtendedContext.to_eng_string(Decimal('-123E-12')) '-123E-12' >>> ExtendedContext.to_eng_string(Decimal('7E-7')) '700E-9' >>> ExtendedContext.to_eng_string(Decimal('7E+1')) '70' >>> ExtendedContext.to_eng_string(Decimal('0E+1')) '0.00E+3' T)r–)r/)r—r¯)r.rr(r(r*r¯Œs zContext.to_eng_stringcCst|dd�}|j|d�S)zyConverts a number to a string, using scientific notation. The operation is not affected by the context. T)r–)r/)r—r®)r.rr(r(r*Ú to_sci_string¨s zContext.to_sci_stringcCst|dd�}|j|d�S)akRounds to an integer. When the operand has a negative exponent, the result is the same as using the quantize() operation using the given operand as the left-hand-operand, 1E+0 as the right-hand-operand, and the precision of the operand as the precision setting; Inexact and Rounded flags are allowed in this operation. The rounding mode is taken from the context. >>> ExtendedContext.to_integral_exact(Decimal('2.1')) Decimal('2') >>> ExtendedContext.to_integral_exact(Decimal('100')) Decimal('100') >>> ExtendedContext.to_integral_exact(Decimal('100.0')) Decimal('100') >>> ExtendedContext.to_integral_exact(Decimal('101.5')) Decimal('102') >>> ExtendedContext.to_integral_exact(Decimal('-101.5')) Decimal('-102') >>> ExtendedContext.to_integral_exact(Decimal('10E+5')) Decimal('1.0E+6') >>> ExtendedContext.to_integral_exact(Decimal('7.89E+77')) Decimal('7.89E+77') >>> ExtendedContext.to_integral_exact(Decimal('-Inf')) Decimal('-Infinity') T)r–)r/)r—r")r.rr(r(r*r"°s zContext.to_integral_exactcCst|dd�}|j|d�S)aLRounds to an integer. When the operand has a negative exponent, the result is the same as using the quantize() operation using the given operand as the left-hand-operand, 1E+0 as the right-hand-operand, and the precision of the operand as the precision setting, except that no flags will be set. The rounding mode is taken from the context. >>> ExtendedContext.to_integral_value(Decimal('2.1')) Decimal('2') >>> ExtendedContext.to_integral_value(Decimal('100')) Decimal('100') >>> ExtendedContext.to_integral_value(Decimal('100.0')) Decimal('100') >>> ExtendedContext.to_integral_value(Decimal('101.5')) Decimal('102') >>> ExtendedContext.to_integral_value(Decimal('-101.5')) Decimal('-102') >>> ExtendedContext.to_integral_value(Decimal('10E+5')) Decimal('1.0E+6') >>> ExtendedContext.to_integral_value(Decimal('7.89E+77')) Decimal('7.89E+77') >>> ExtendedContext.to_integral_value(Decimal('-Inf')) Decimal('-Infinity') T)r–)r/)r—rõ)r.rr(r(r*rõÎs zContext.to_integral_value) NNNNNNNNN)N)rK)N)Xr1r2r3r4rrŒr�rŠr’rer¥rGršr#rFrgr_rWržr r¡rÁrÞr$r¡r¢rcr£r¤r-r˜r.r)r2r°r¥r±r3r¦r§rÂrPròr6r7rr›r8r‡r9r†r:r;rCrFrGrQrSrTrRr·rUr¤rVr¨r©rYrZr[rr]rªr«rír^rÄrÑrbrrcrÃr(r¬r¯r­r"rõr}r(r(r(r*rs® "     $#   %  #2 P:&" c@s&eZdZdZddd„Zdd„ZeZdS)rd)r=rOrPNcCsf|dkrd|_d|_d|_nFt|tƒrD|j|_t|jƒ|_|j|_n|d|_|d|_|d|_dS)Nr&r-rV)r=rOrPrZrr7r8rI)r.ror(r(r*rôs     z_WorkRep.__init__cCsd|j|j|jfS)Nz (%r, %r, %r))r=rOrP)r.r(r(r*r¥sz_WorkRep.__repr__)N)r1r2r3rvrr¥r®r(r(r(r*rdîs rdcCsš|j|jkr|}|}n|}|}tt|jƒƒ}tt|jƒƒ}|jtd||dƒ}||jd|krpd|_||_|jd|j|j9_|j|_||fS)zcNormalizes op1, op2 to have the same exp and length of coefficient. Done during addition. rrVr-r™)rPrar[rOr¤)rºr»r@Ztmpr‚Ztmp_lenZ other_lenrPr(r(r*r¹ s r¹cCsb|dkr dS|dkr |d|Stt|ƒƒ}t|ƒt| d¡ƒ}|| krPdS|d| SdS)a Given integers n and e, return n * 10**e if it's an integer, else None. The computation is designed to avoid computing large powers of 10 unnecessarily. >>> _decimal_lshift_exact(3, 4) 30000 >>> _decimal_lshift_exact(300, -999999999) # returns None r&r™rKN)r[rcraÚrstrip)r5r¨Zstr_nZval_nr(r(r*r*s   rcCsF|dks|dkrtdƒ‚d}x$||kr@||| |d?}}qW|S)zóClosest integer to the square root of the positive integer n. a is an initial approximation to the square root. Any positive integer will do for a, but the closer a is to the square root of n the faster convergence will be. r&z3Both arguments to _sqrt_nearest should be positive.r-)rg)r5rrNr(r(r*Ú _sqrt_nearest?s  r¯cCs2d|>||?}}|d||d@|d@|kS)z‰Given an integer x and a nonnegative integer shift, return closest integer to x / 2**shift; use round-to-even in case of a tie. r-rVr()rrÃrNrÈr(r(r*Ú_rshift_nearestNsr°cCs&t||ƒ\}}|d||d@|kS)zaClosest integer to a/b, a and b positive integers; rounds to even in the case of a tie. rVr-)rÂ)rrNrÈrÉr(r(r*Ú _div_nearestVsr±rþc Csî||}d}xn||kr*t|ƒ||>|ksF||krzt|ƒ||?|krzt||d>|t||t||ƒ|ƒƒ}|d7}qWtdtt|ƒƒd|ƒ }t||ƒ}t||ƒ}x0t|dddƒD]}t||ƒt|||ƒ}qÀWt|||ƒS)aÉInteger approximation to M*log(x/M), with absolute error boundable in terms only of x/M. Given positive integers x and M, return an integer approximation to M * log(x/M). For L = 8 and 0.1 <= x/M <= 10 the difference between the approximation and the exact result is at most 22. For L = 8 and 1.0 <= x/M <= 10.0 the difference is at most 15. In both cases these are upper bounds on the error; it will usually be much smaller.r&r-iöÿÿÿrUr)rcr±r¯r°rOrar[rö) rÚMÚLr ÚRÚTZyshiftÚwrzr(r(r*Ú_ilog^s    r·c Cs¶|d7}tt|ƒƒ}||||dk}|dkr”d|}|||}|dkrZ|d|9}nt|d| ƒ}t||ƒ}t|ƒ}t|||ƒ}||} nd}t|d| ƒ} t| |dƒS)z¾Given integers c, e and p with c > 0, p >= 0, compute an integer approximation to 10**p * log10(c*10**e), with an absolute error of at most 1. Assumes that c*10**e is not exactly 1.rVr-r&r™r)rar[r±r·Ú _log10_digits) r&r¨rr'ryr²rzÚlog_dZlog_10Z log_tenpowerr(r(r*rEŽs     rEc CsÎ|d7}tt|ƒƒ}||||dk}|dkrr|||}|dkrR|d|9}nt|d| ƒ}t|d|ƒ}nd}|r¼ttt|ƒƒƒd}||dkr¶t|t||ƒd|ƒ}qÀd}nd}t||dƒS)z´Given integers c, e and p with c > 0, compute an integer approximation to 10**p * log(c*10**e), with an absolute error of at most 1. Assumes that c*10**e is not exactly 1.rVr-r&r™r)rar[r±r·rcr¸) r&r¨rr'ryrzr¹rZ f_log_tenr(r(r*rB°s"   rBc@s eZdZdZdd„Zdd„ZdS)Ú _Log10Memoizez¾Class to compute, store, and allow retrieval of, digits of the constant log(10) = 2.302585.... This constant is needed by Decimal.ln, Decimal.log10, Decimal.exp and Decimal.__pow__.cCs d|_dS)NZ/23025850929940456840179914546843642076011014886)rs)r.r(r(r*ràsz_Log10Memoize.__init__cCsš|dkrtdƒ‚|t|jƒkr„d}xLd||d}tttd||ƒdƒƒ}|| d…d|krdP|d7}q$W| d¡dd …|_t|jd|d …ƒS) ztGiven an integer p >= 0, return floor(10**p)*log(10). For example, self.getdigits(3) returns 2302. r&zp should be nonnegativerUr™rVrNrKrr-)rgrarsr[r±r·r®rO)r.rrr²rsr(r(r*Ú getdigitsãs  z_Log10Memoize.getdigitsN)r1r2r3r4rr»r(r(r(r*rºÜsrºc Cs°t||>|ƒ}tdtt|ƒƒd|ƒ }t||ƒ}||>}x.t|dddƒD]}t|||||ƒ}qRWx6t|dddƒD]"}||d>}t||||ƒ}q‚W||S)zëGiven integers x and M, M > 0, such that x/M is small in absolute value, compute an integer approximation to M*exp(x/M). For 0 <= x/M <= 2.4, the absolute error in the result is bounded by 60 (and is usually much smaller).iöÿÿÿrUr-r&rrV)rrOrar[r±rö) rr²r³r´rµr ZMshiftrúrzr(r(r*Ú_iexps  r¼c Cs–|d7}td|tt|ƒƒdƒ}||}||}|dkrH|d|}n|d| }t|t|ƒƒ\}}t|d|ƒ}tt|d|ƒdƒ||dfS)aÐCompute an approximation to exp(c*10**e), with p decimal places of precision. Returns integers d, f such that: 10**(p-1) <= d <= 10**p, and (d-1)*10**f < exp(c*10**e) < (d+1)*10**f In other words, d*10**f is an approximation to exp(c*10**e) with p digits of precision, and with an error in d of at most 1. This is almost, but not quite, the same as the error being < 1ulp: when d = 10**(p-1) the error could be up to 10 ulp.rVr&r-r™ièrU)r·rar[rÂr¸r±r¼) r&r¨rrrÈrÃZcshiftZquotr r(r(r*r4&sr4c Csèttt|ƒƒƒ|}t||||dƒ}||}|dkrJ||d|}nt||d| ƒ}|dkr´tt|ƒƒ|dk|dkkržd|ddd|} } qàd|d| } } n,t||d |dƒ\} } t| dƒ} | d7} | | fS)a5Given integers xc, xe, yc and ye representing Decimals x = xc*10**xe and y = yc*10**ye, compute x**y. Returns a pair of integers (c, e) such that: 10**(p-1) <= c <= 10**p, and (c-1)*10**e < x**y < (c+1)*10**e in other words, c*10**e is an approximation to x**y with p digits of precision, and with an error in c of at most 1. (This is almost, but not quite, the same as the error being < 1ulp: when c == 10**(p-1) we can only guarantee error < 10ulp.) We assume that: x is positive and not equal to 1, and y is nonzero. r-r&r™)rar[rcrBr±r4) rr r r rrNZlxcrÃZpcr{rPr(r(r*rJs rréFé5é(ér<ér™rt) r¿Ú2Ú3Ú4Ú5Ú6Ú7Ú8r>cCs0|dkrtdƒ‚t|ƒ}dt|ƒ||dS)z@Compute a lower bound for 100*log10(c) for a positive integer c.r&z0The argument to _log10_lb should be nonnegative.r)rgr[ra)r&Z correctionZstr_cr(r(r*rtsrcCsLt|tƒr|St|tƒr t|ƒS|r8t|tƒr8t |¡S|rHtd|ƒ‚tS)zÙConvert other to Decimal. Verifies that it's ok to use in an implicit construction. If allow_float is true, allow conversion from float; this is used in the comparison methods (__eq__ and friends). zUnable to convert %s to Decimal)rZrrOrkrlrmr�)r‚r–Z allow_floatr(r(r*r—s    r—cCs´t|tƒr||fSt|tjƒrR|jsDt|jtt|j ƒ|j ƒ|j ƒ}|t|j ƒfS|rrt|tj ƒrr|jdkrr|j}t|tƒr¬tƒ}|r’d|jt<n | td¡|t |¡fSttfS)zÔGiven a Decimal instance self and a Python object other, return a pair (s, o) of Decimal instances such that "s op o" is equivalent to "self op other" for any of the 6 comparison operators "op". r&r-z;strict semantics for mixing floats and Decimals are enabled)rZrÚ_numbersZRationalrJr6r7r[rOr8Ú denominatorrIÚ numeratorZComplexrÙrØrkrrrr_rlr�)r.r‚rŽr/r(r(r*r�’s$    r�ri?BiÁ½ðÿ)r@r?rrrArrªrÝrW)r@r?rra· # A numeric string consists of: # \s* (?P[-+])? # an optional sign, followed by either... ( (?=\d|\.\d) # ...a number (with at least one digit) (?P\d*) # having a (possibly empty) integer part (\.(?P\d*))? # followed by an optional fractional part (E(?P[-+]?\d+))? # followed by an optional exponent, or... | Inf(inity)? # ...an infinity, or... | (?Ps)? # ...an (optionally signaling) NaN # NaN (?P\d*) # with (possibly empty) diagnostic info. ) # \s* \Z z0*$z50*$zÉ\A (?: (?P.)? (?P[<>=^]) )? (?P[-+ ])? (?P\#)? (?P0)? (?P(?!0)\d+)? (?P,)? (?:\.(?P0|(?!0)\d+))? (?P[eEfFgGn%])? \Z cCs”t |¡}|dkrtd|ƒ‚| ¡}|d}|d}|ddk |d<|drv|dk rbtd|ƒ‚|dk rvtd|ƒ‚|p|d|d<|pˆd |d<|d dkr¢d |d <t|d p®d ƒ|d <|ddk rÒt|dƒ|d<|ddkrþ|ddksö|ddkrþd|d<|ddk�rfd|d<|dk�r&t ¡}|ddk �r@td|ƒ‚|d|d<|d|d<|d|d<n*|ddk�r|d|d<ddg|d<d|d<|S)aÚParse and validate a format specifier. Turns a standard numeric format specifier into a dict, with the following entries: fill: fill character to pad field to minimum width align: alignment type, either '<', '>', '=' or '^' sign: either '+', '-' or ' ' minimumwidth: nonnegative integer giving minimum width zeropad: boolean, indicating whether to pad with zeros thousands_sep: string to use as thousands separator, or '' grouping: grouping for thousands separators, in format used by localeconv decimal_point: string to use for decimal point precision: nonnegative integer giving precision, or None type: one of the characters 'eEfFgG%', or None NzInvalid format specifier: ÚfillÚalignÚzeropadz7Fill character conflicts with '0' in format specifier: z2Alignment conflicts with '0' in format specifier: ú ú>r=rNÚ minimumwidthrKrmr&rfZgGnr-r5rkÚ thousands_sepzJExplicit thousands separator conflicts with 'n' type in format specifier: ÚgroupingÚ decimal_pointrMrUr§)Ú_parse_format_specifier_regexÚmatchrgÚ groupdictrOÚ_localeÚ localeconv)Ú format_specrirpZ format_dictrÌrÍr(r(r*rosN           roc Cs´|d}|d}||t|ƒt|ƒ}|d}|dkrF|||}nj|dkr\|||}nT|dkrr|||}n>|dkr¨t|ƒd}|d |…||||d …}ntd ƒ‚|S) zÜGiven an unpadded, non-aligned numeric string 'body' and sign string 'sign', add padding and alignment conforming to the given format specifier dictionary 'spec' (as produced by parse_format_specifier). rÑrÌrÍúqsh           &     .  ^  0",# %$+   *      P % )