desc
stringlengths 3
26.7k
| decl
stringlengths 11
7.89k
| bodies
stringlengths 8
553k
|
---|---|---|
'Return self * other.
(+-) INF * 0 (or its reverse) raise InvalidOperation.'
| def __mul__(self, other, context=None):
| other = _convert_other(other)
if (other is NotImplemented):
return other
if (context is None):
context = getcontext()
resultsign = (self._sign ^ other._sign)
if (self._is_special or other._is_special):
ans = self._check_nans(other, context)
if ans:
return ans
if self._isinfinity():
if (not other):
return context._raise_error(InvalidOperation, '(+-)INF * 0')
return _SignedInfinity[resultsign]
if other._isinfinity():
if (not self):
return context._raise_error(InvalidOperation, '0 * (+-)INF')
return _SignedInfinity[resultsign]
resultexp = (self._exp + other._exp)
if ((not self) or (not other)):
ans = _dec_from_triple(resultsign, '0', resultexp)
ans = ans._fix(context)
return ans
if (self._int == '1'):
ans = _dec_from_triple(resultsign, other._int, resultexp)
ans = ans._fix(context)
return ans
if (other._int == '1'):
ans = _dec_from_triple(resultsign, self._int, resultexp)
ans = ans._fix(context)
return ans
op1 = _WorkRep(self)
op2 = _WorkRep(other)
ans = _dec_from_triple(resultsign, str((op1.int * op2.int)), resultexp)
ans = ans._fix(context)
return ans
|
'Return self / other.'
| def __truediv__(self, other, context=None):
| other = _convert_other(other)
if (other is NotImplemented):
return NotImplemented
if (context is None):
context = getcontext()
sign = (self._sign ^ other._sign)
if (self._is_special or other._is_special):
ans = self._check_nans(other, context)
if ans:
return ans
if (self._isinfinity() and other._isinfinity()):
return context._raise_error(InvalidOperation, '(+-)INF/(+-)INF')
if self._isinfinity():
return _SignedInfinity[sign]
if other._isinfinity():
context._raise_error(Clamped, 'Division by infinity')
return _dec_from_triple(sign, '0', context.Etiny())
if (not other):
if (not self):
return context._raise_error(DivisionUndefined, '0 / 0')
return context._raise_error(DivisionByZero, 'x / 0', sign)
if (not self):
exp = (self._exp - other._exp)
coeff = 0
else:
shift = (((len(other._int) - len(self._int)) + context.prec) + 1)
exp = ((self._exp - other._exp) - shift)
op1 = _WorkRep(self)
op2 = _WorkRep(other)
if (shift >= 0):
(coeff, remainder) = divmod((op1.int * (10 ** shift)), op2.int)
else:
(coeff, remainder) = divmod(op1.int, (op2.int * (10 ** (- shift))))
if remainder:
if ((coeff % 5) == 0):
coeff += 1
else:
ideal_exp = (self._exp - other._exp)
while ((exp < ideal_exp) and ((coeff % 10) == 0)):
coeff //= 10
exp += 1
ans = _dec_from_triple(sign, str(coeff), exp)
return ans._fix(context)
|
'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.'
| def _divide(self, other, context):
| sign = (self._sign ^ other._sign)
if other._isinfinity():
ideal_exp = self._exp
else:
ideal_exp = min(self._exp, other._exp)
expdiff = (self.adjusted() - other.adjusted())
if ((not self) or other._isinfinity() or (expdiff <= (-2))):
return (_dec_from_triple(sign, '0', 0), self._rescale(ideal_exp, context.rounding))
if (expdiff <= context.prec):
op1 = _WorkRep(self)
op2 = _WorkRep(other)
if (op1.exp >= op2.exp):
op1.int *= (10 ** (op1.exp - op2.exp))
else:
op2.int *= (10 ** (op2.exp - op1.exp))
(q, r) = divmod(op1.int, op2.int)
if (q < (10 ** context.prec)):
return (_dec_from_triple(sign, str(q), 0), _dec_from_triple(self._sign, str(r), ideal_exp))
ans = context._raise_error(DivisionImpossible, 'quotient too large in //, % or divmod')
return (ans, ans)
|
'Swaps self/other and returns __truediv__.'
| def __rtruediv__(self, other, context=None):
| other = _convert_other(other)
if (other is NotImplemented):
return other
return other.__truediv__(self, context=context)
|
'Return (self // other, self % other)'
| def __divmod__(self, other, context=None):
| other = _convert_other(other)
if (other is NotImplemented):
return other
if (context is None):
context = getcontext()
ans = self._check_nans(other, context)
if ans:
return (ans, ans)
sign = (self._sign ^ other._sign)
if self._isinfinity():
if other._isinfinity():
ans = context._raise_error(InvalidOperation, 'divmod(INF, INF)')
return (ans, ans)
else:
return (_SignedInfinity[sign], context._raise_error(InvalidOperation, 'INF % x'))
if (not other):
if (not self):
ans = context._raise_error(DivisionUndefined, 'divmod(0, 0)')
return (ans, ans)
else:
return (context._raise_error(DivisionByZero, 'x // 0', sign), context._raise_error(InvalidOperation, 'x % 0'))
(quotient, remainder) = self._divide(other, context)
remainder = remainder._fix(context)
return (quotient, remainder)
|
'Swaps self/other and returns __divmod__.'
| def __rdivmod__(self, other, context=None):
| other = _convert_other(other)
if (other is NotImplemented):
return other
return other.__divmod__(self, context=context)
|
'self % other'
| def __mod__(self, other, context=None):
| other = _convert_other(other)
if (other is NotImplemented):
return other
if (context is None):
context = getcontext()
ans = self._check_nans(other, context)
if ans:
return ans
if self._isinfinity():
return context._raise_error(InvalidOperation, 'INF % x')
elif (not other):
if self:
return context._raise_error(InvalidOperation, 'x % 0')
else:
return context._raise_error(DivisionUndefined, '0 % 0')
remainder = self._divide(other, context)[1]
remainder = remainder._fix(context)
return remainder
|
'Swaps self/other and returns __mod__.'
| def __rmod__(self, other, context=None):
| other = _convert_other(other)
if (other is NotImplemented):
return other
return other.__mod__(self, context=context)
|
'Remainder nearest to 0- abs(remainder-near) <= other/2'
| def remainder_near(self, other, context=None):
| if (context is None):
context = getcontext()
other = _convert_other(other, raiseit=True)
ans = self._check_nans(other, context)
if ans:
return ans
if self._isinfinity():
return context._raise_error(InvalidOperation, 'remainder_near(infinity, x)')
if (not other):
if self:
return context._raise_error(InvalidOperation, 'remainder_near(x, 0)')
else:
return context._raise_error(DivisionUndefined, 'remainder_near(0, 0)')
if other._isinfinity():
ans = Decimal(self)
return ans._fix(context)
ideal_exponent = min(self._exp, other._exp)
if (not self):
ans = _dec_from_triple(self._sign, '0', ideal_exponent)
return ans._fix(context)
expdiff = (self.adjusted() - other.adjusted())
if (expdiff >= (context.prec + 1)):
return context._raise_error(DivisionImpossible)
if (expdiff <= (-2)):
ans = self._rescale(ideal_exponent, context.rounding)
return ans._fix(context)
op1 = _WorkRep(self)
op2 = _WorkRep(other)
if (op1.exp >= op2.exp):
op1.int *= (10 ** (op1.exp - op2.exp))
else:
op2.int *= (10 ** (op2.exp - op1.exp))
(q, r) = divmod(op1.int, op2.int)
if (((2 * r) + (q & 1)) > op2.int):
r -= op2.int
q += 1
if (q >= (10 ** context.prec)):
return context._raise_error(DivisionImpossible)
sign = self._sign
if (r < 0):
sign = (1 - sign)
r = (- r)
ans = _dec_from_triple(sign, str(r), ideal_exponent)
return ans._fix(context)
|
'self // other'
| def __floordiv__(self, other, context=None):
| other = _convert_other(other)
if (other is NotImplemented):
return other
if (context is None):
context = getcontext()
ans = self._check_nans(other, context)
if ans:
return ans
if self._isinfinity():
if other._isinfinity():
return context._raise_error(InvalidOperation, 'INF // INF')
else:
return _SignedInfinity[(self._sign ^ other._sign)]
if (not other):
if self:
return context._raise_error(DivisionByZero, 'x // 0', (self._sign ^ other._sign))
else:
return context._raise_error(DivisionUndefined, '0 // 0')
return self._divide(other, context)[0]
|
'Swaps self/other and returns __floordiv__.'
| def __rfloordiv__(self, other, context=None):
| other = _convert_other(other)
if (other is NotImplemented):
return other
return other.__floordiv__(self, context=context)
|
'Float representation.'
| def __float__(self):
| if self._isnan():
if self.is_snan():
raise ValueError('Cannot convert signaling NaN to float')
s = ('-nan' if self._sign else 'nan')
else:
s = str(self)
return float(s)
|
'Converts self to an int, truncating if necessary.'
| def __int__(self):
| if self._is_special:
if self._isnan():
raise ValueError('Cannot convert NaN to integer')
elif self._isinfinity():
raise OverflowError('Cannot convert infinity to integer')
s = ((-1) ** self._sign)
if (self._exp >= 0):
return ((s * int(self._int)) * (10 ** self._exp))
else:
return (s * int((self._int[:self._exp] or '0')))
|
'Converts to a long.
Equivalent to long(int(self))'
| def __long__(self):
| return long(self.__int__())
|
'Decapitate the payload of a NaN to fit the context'
| def _fix_nan(self, context):
| payload = self._int
max_payload_len = (context.prec - context._clamp)
if (len(payload) > max_payload_len):
payload = payload[(len(payload) - max_payload_len):].lstrip('0')
return _dec_from_triple(self._sign, payload, self._exp, True)
return Decimal(self)
|
'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.'
| def _fix(self, context):
| if self._is_special:
if self._isnan():
return self._fix_nan(context)
else:
return Decimal(self)
Etiny = context.Etiny()
Etop = context.Etop()
if (not self):
exp_max = [context.Emax, Etop][context._clamp]
new_exp = min(max(self._exp, Etiny), exp_max)
if (new_exp != self._exp):
context._raise_error(Clamped)
return _dec_from_triple(self._sign, '0', new_exp)
else:
return Decimal(self)
exp_min = ((len(self._int) + self._exp) - context.prec)
if (exp_min > Etop):
ans = context._raise_error(Overflow, 'above Emax', self._sign)
context._raise_error(Inexact)
context._raise_error(Rounded)
return ans
self_is_subnormal = (exp_min < Etiny)
if self_is_subnormal:
exp_min = Etiny
if (self._exp < exp_min):
digits = ((len(self._int) + self._exp) - exp_min)
if (digits < 0):
self = _dec_from_triple(self._sign, '1', (exp_min - 1))
digits = 0
rounding_method = self._pick_rounding_function[context.rounding]
changed = rounding_method(self, digits)
coeff = (self._int[:digits] or '0')
if (changed > 0):
coeff = str((int(coeff) + 1))
if (len(coeff) > context.prec):
coeff = coeff[:(-1)]
exp_min += 1
if (exp_min > Etop):
ans = context._raise_error(Overflow, 'above Emax', self._sign)
else:
ans = _dec_from_triple(self._sign, coeff, exp_min)
if (changed and self_is_subnormal):
context._raise_error(Underflow)
if self_is_subnormal:
context._raise_error(Subnormal)
if changed:
context._raise_error(Inexact)
context._raise_error(Rounded)
if (not ans):
context._raise_error(Clamped)
return ans
if self_is_subnormal:
context._raise_error(Subnormal)
if ((context._clamp == 1) and (self._exp > Etop)):
context._raise_error(Clamped)
self_padded = (self._int + ('0' * (self._exp - Etop)))
return _dec_from_triple(self._sign, self_padded, Etop)
return Decimal(self)
|
'Also known as round-towards-0, truncate.'
| def _round_down(self, prec):
| if _all_zeros(self._int, prec):
return 0
else:
return (-1)
|
'Rounds away from 0.'
| def _round_up(self, prec):
| return (- self._round_down(prec))
|
'Rounds 5 up (away from 0)'
| def _round_half_up(self, prec):
| if (self._int[prec] in '56789'):
return 1
elif _all_zeros(self._int, prec):
return 0
else:
return (-1)
|
'Round 5 down'
| def _round_half_down(self, prec):
| if _exact_half(self._int, prec):
return (-1)
else:
return self._round_half_up(prec)
|
'Round 5 to even, rest to nearest.'
| def _round_half_even(self, prec):
| if (_exact_half(self._int, prec) and ((prec == 0) or (self._int[(prec - 1)] in '02468'))):
return (-1)
else:
return self._round_half_up(prec)
|
'Rounds up (not away from 0 if negative.)'
| def _round_ceiling(self, prec):
| if self._sign:
return self._round_down(prec)
else:
return (- self._round_down(prec))
|
'Rounds down (not towards 0 if negative)'
| def _round_floor(self, prec):
| if (not self._sign):
return self._round_down(prec)
else:
return (- self._round_down(prec))
|
'Round down unless digit prec-1 is 0 or 5.'
| def _round_05up(self, prec):
| if (prec and (self._int[(prec - 1)] not in '05')):
return self._round_down(prec)
else:
return (- self._round_down(prec))
|
'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.'
| def fma(self, other, third, context=None):
| other = _convert_other(other, raiseit=True)
if (self._is_special or other._is_special):
if (context is None):
context = getcontext()
if (self._exp == 'N'):
return context._raise_error(InvalidOperation, 'sNaN', self)
if (other._exp == 'N'):
return context._raise_error(InvalidOperation, 'sNaN', other)
if (self._exp == 'n'):
product = self
elif (other._exp == 'n'):
product = other
elif (self._exp == 'F'):
if (not other):
return context._raise_error(InvalidOperation, 'INF * 0 in fma')
product = _SignedInfinity[(self._sign ^ other._sign)]
elif (other._exp == 'F'):
if (not self):
return context._raise_error(InvalidOperation, '0 * INF in fma')
product = _SignedInfinity[(self._sign ^ other._sign)]
else:
product = _dec_from_triple((self._sign ^ other._sign), str((int(self._int) * int(other._int))), (self._exp + other._exp))
third = _convert_other(third, raiseit=True)
return product.__add__(third, context)
|
'Three argument version of __pow__'
| def _power_modulo(self, other, modulo, context=None):
| other = _convert_other(other, raiseit=True)
modulo = _convert_other(modulo, raiseit=True)
if (context is None):
context = getcontext()
self_is_nan = self._isnan()
other_is_nan = other._isnan()
modulo_is_nan = modulo._isnan()
if (self_is_nan or other_is_nan or modulo_is_nan):
if (self_is_nan == 2):
return context._raise_error(InvalidOperation, 'sNaN', self)
if (other_is_nan == 2):
return context._raise_error(InvalidOperation, 'sNaN', other)
if (modulo_is_nan == 2):
return context._raise_error(InvalidOperation, 'sNaN', modulo)
if self_is_nan:
return self._fix_nan(context)
if other_is_nan:
return other._fix_nan(context)
return modulo._fix_nan(context)
if (not (self._isinteger() and other._isinteger() and modulo._isinteger())):
return context._raise_error(InvalidOperation, 'pow() 3rd argument not allowed unless all arguments are integers')
if (other < 0):
return context._raise_error(InvalidOperation, 'pow() 2nd argument cannot be negative when 3rd argument specified')
if (not modulo):
return context._raise_error(InvalidOperation, 'pow() 3rd argument cannot be 0')
if (modulo.adjusted() >= context.prec):
return context._raise_error(InvalidOperation, 'insufficient precision: pow() 3rd argument must not have more than precision digits')
if ((not other) and (not self)):
return context._raise_error(InvalidOperation, 'at least one of pow() 1st argument and 2nd argument must be nonzero ;0**0 is not defined')
if other._iseven():
sign = 0
else:
sign = self._sign
modulo = abs(int(modulo))
base = _WorkRep(self.to_integral_value())
exponent = _WorkRep(other.to_integral_value())
base = (((base.int % modulo) * pow(10, base.exp, modulo)) % modulo)
for i in xrange(exponent.exp):
base = pow(base, 10, modulo)
base = pow(base, exponent.int, modulo)
return _dec_from_triple(sign, str(base), 0)
|
'Attempt 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.'
| def _power_exact(self, other, p):
| x = _WorkRep(self)
(xc, xe) = (x.int, x.exp)
while ((xc % 10) == 0):
xc //= 10
xe += 1
y = _WorkRep(other)
(yc, ye) = (y.int, y.exp)
while ((yc % 10) == 0):
yc //= 10
ye += 1
if (xc == 1):
xe *= yc
while ((xe % 10) == 0):
xe //= 10
ye += 1
if (ye < 0):
return None
exponent = (xe * (10 ** ye))
if (y.sign == 1):
exponent = (- exponent)
if (other._isinteger() and (other._sign == 0)):
ideal_exponent = (self._exp * int(other))
zeros = min((exponent - ideal_exponent), (p - 1))
else:
zeros = 0
return _dec_from_triple(0, ('1' + ('0' * zeros)), (exponent - zeros))
if (y.sign == 1):
last_digit = (xc % 10)
if (last_digit in (2, 4, 6, 8)):
if ((xc & (- xc)) != xc):
return None
e = (_nbits(xc) - 1)
emax = ((p * 93) // 65)
if (ye >= len(str(emax))):
return None
e = _decimal_lshift_exact((e * yc), ye)
xe = _decimal_lshift_exact((xe * yc), ye)
if ((e is None) or (xe is None)):
return None
if (e > emax):
return None
xc = (5 ** e)
elif (last_digit == 5):
e = ((_nbits(xc) * 28) // 65)
(xc, remainder) = divmod((5 ** e), xc)
if remainder:
return None
while ((xc % 5) == 0):
xc //= 5
e -= 1
emax = ((p * 10) // 3)
if (ye >= len(str(emax))):
return None
e = _decimal_lshift_exact((e * yc), ye)
xe = _decimal_lshift_exact((xe * yc), ye)
if ((e is None) or (xe is None)):
return None
if (e > emax):
return None
xc = (2 ** e)
else:
return None
if (xc >= (10 ** p)):
return None
xe = ((- e) - xe)
return _dec_from_triple(0, str(xc), xe)
if (ye >= 0):
(m, n) = ((yc * (10 ** ye)), 1)
else:
if ((xe != 0) and (len(str(abs((yc * xe)))) <= (- ye))):
return None
xc_bits = _nbits(xc)
if ((xc != 1) and (len(str((abs(yc) * xc_bits))) <= (- ye))):
return None
(m, n) = (yc, (10 ** (- ye)))
while ((m % 2) == (n % 2) == 0):
m //= 2
n //= 2
while ((m % 5) == (n % 5) == 0):
m //= 5
n //= 5
if (n > 1):
if ((xc != 1) and (xc_bits <= n)):
return None
(xe, rem) = divmod(xe, n)
if (rem != 0):
return None
a = (1L << (- ((- _nbits(xc)) // n)))
while True:
(q, r) = divmod(xc, (a ** (n - 1)))
if (a <= q):
break
else:
a = (((a * (n - 1)) + q) // n)
if (not ((a == q) and (r == 0))):
return None
xc = a
if ((xc > 1) and (m > ((p * 100) // _log10_lb(xc)))):
return None
xc = (xc ** m)
xe *= m
if (xc > (10 ** p)):
return None
str_xc = str(xc)
if (other._isinteger() and (other._sign == 0)):
ideal_exponent = (self._exp * int(other))
zeros = min((xe - ideal_exponent), (p - len(str_xc)))
else:
zeros = 0
return _dec_from_triple(0, (str_xc + ('0' * zeros)), (xe - zeros))
|
'Return 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.'
| def __pow__(self, other, modulo=None, context=None):
| if (modulo is not None):
return self._power_modulo(other, modulo, context)
other = _convert_other(other)
if (other is NotImplemented):
return other
if (context is None):
context = getcontext()
ans = self._check_nans(other, context)
if ans:
return ans
if (not other):
if (not self):
return context._raise_error(InvalidOperation, '0 ** 0')
else:
return _One
result_sign = 0
if (self._sign == 1):
if other._isinteger():
if (not other._iseven()):
result_sign = 1
elif self:
return context._raise_error(InvalidOperation, 'x ** y with x negative and y not an integer')
self = self.copy_negate()
if (not self):
if (other._sign == 0):
return _dec_from_triple(result_sign, '0', 0)
else:
return _SignedInfinity[result_sign]
if self._isinfinity():
if (other._sign == 0):
return _SignedInfinity[result_sign]
else:
return _dec_from_triple(result_sign, '0', 0)
if (self == _One):
if other._isinteger():
if (other._sign == 1):
multiplier = 0
elif (other > context.prec):
multiplier = context.prec
else:
multiplier = int(other)
exp = (self._exp * multiplier)
if (exp < (1 - context.prec)):
exp = (1 - context.prec)
context._raise_error(Rounded)
else:
context._raise_error(Inexact)
context._raise_error(Rounded)
exp = (1 - context.prec)
return _dec_from_triple(result_sign, ('1' + ('0' * (- exp))), exp)
self_adj = self.adjusted()
if other._isinfinity():
if ((other._sign == 0) == (self_adj < 0)):
return _dec_from_triple(result_sign, '0', 0)
else:
return _SignedInfinity[result_sign]
ans = None
exact = False
bound = (self._log10_exp_bound() + other.adjusted())
if ((self_adj >= 0) == (other._sign == 0)):
if (bound >= len(str(context.Emax))):
ans = _dec_from_triple(result_sign, '1', (context.Emax + 1))
else:
Etiny = context.Etiny()
if (bound >= len(str((- Etiny)))):
ans = _dec_from_triple(result_sign, '1', (Etiny - 1))
if (ans is None):
ans = self._power_exact(other, (context.prec + 1))
if (ans is not None):
if (result_sign == 1):
ans = _dec_from_triple(1, ans._int, ans._exp)
exact = True
if (ans is None):
p = context.prec
x = _WorkRep(self)
(xc, xe) = (x.int, x.exp)
y = _WorkRep(other)
(yc, ye) = (y.int, y.exp)
if (y.sign == 1):
yc = (- yc)
extra = 3
while True:
(coeff, exp) = _dpower(xc, xe, yc, ye, (p + extra))
if (coeff % (5 * (10 ** ((len(str(coeff)) - p) - 1)))):
break
extra += 3
ans = _dec_from_triple(result_sign, str(coeff), exp)
if (exact and (not other._isinteger())):
if (len(ans._int) <= context.prec):
expdiff = ((context.prec + 1) - len(ans._int))
ans = _dec_from_triple(ans._sign, (ans._int + ('0' * expdiff)), (ans._exp - expdiff))
newcontext = context.copy()
newcontext.clear_flags()
for exception in _signals:
newcontext.traps[exception] = 0
ans = ans._fix(newcontext)
newcontext._raise_error(Inexact)
if newcontext.flags[Subnormal]:
newcontext._raise_error(Underflow)
if newcontext.flags[Overflow]:
context._raise_error(Overflow, 'above Emax', ans._sign)
for exception in (Underflow, Subnormal, Inexact, Rounded, Clamped):
if newcontext.flags[exception]:
context._raise_error(exception)
else:
ans = ans._fix(context)
return ans
|
'Swaps self/other and returns __pow__.'
| def __rpow__(self, other, context=None):
| other = _convert_other(other)
if (other is NotImplemented):
return other
return other.__pow__(self, context=context)
|
'Normalize- strip trailing 0s, change anything equal to 0 to 0e0'
| def normalize(self, context=None):
| if (context is None):
context = getcontext()
if self._is_special:
ans = self._check_nans(context=context)
if ans:
return ans
dup = self._fix(context)
if dup._isinfinity():
return dup
if (not dup):
return _dec_from_triple(dup._sign, '0', 0)
exp_max = [context.Emax, context.Etop()][context._clamp]
end = len(dup._int)
exp = dup._exp
while ((dup._int[(end - 1)] == '0') and (exp < exp_max)):
exp += 1
end -= 1
return _dec_from_triple(dup._sign, dup._int[:end], exp)
|
'Quantize self so its exponent is the same as that of exp.
Similar to self._rescale(exp._exp) but with error checking.'
| def quantize(self, exp, rounding=None, context=None, watchexp=True):
| exp = _convert_other(exp, raiseit=True)
if (context is None):
context = getcontext()
if (rounding is None):
rounding = context.rounding
if (self._is_special or exp._is_special):
ans = self._check_nans(exp, context)
if ans:
return ans
if (exp._isinfinity() or self._isinfinity()):
if (exp._isinfinity() and self._isinfinity()):
return Decimal(self)
return context._raise_error(InvalidOperation, 'quantize with one INF')
if (not watchexp):
ans = self._rescale(exp._exp, rounding)
if (ans._exp > self._exp):
context._raise_error(Rounded)
if (ans != self):
context._raise_error(Inexact)
return ans
if (not (context.Etiny() <= exp._exp <= context.Emax)):
return context._raise_error(InvalidOperation, 'target exponent out of bounds in quantize')
if (not self):
ans = _dec_from_triple(self._sign, '0', exp._exp)
return ans._fix(context)
self_adjusted = self.adjusted()
if (self_adjusted > context.Emax):
return context._raise_error(InvalidOperation, 'exponent of quantize result too large for current context')
if (((self_adjusted - exp._exp) + 1) > context.prec):
return context._raise_error(InvalidOperation, 'quantize result has too many digits for current context')
ans = self._rescale(exp._exp, rounding)
if (ans.adjusted() > context.Emax):
return context._raise_error(InvalidOperation, 'exponent of quantize result too large for current context')
if (len(ans._int) > context.prec):
return context._raise_error(InvalidOperation, 'quantize result has too many digits for current context')
if (ans and (ans.adjusted() < context.Emin)):
context._raise_error(Subnormal)
if (ans._exp > self._exp):
if (ans != self):
context._raise_error(Inexact)
context._raise_error(Rounded)
ans = ans._fix(context)
return ans
|
'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.'
| def same_quantum(self, other):
| other = _convert_other(other, raiseit=True)
if (self._is_special or other._is_special):
return ((self.is_nan() and other.is_nan()) or (self.is_infinite() and other.is_infinite()))
return (self._exp == other._exp)
|
'Rescale 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'
| def _rescale(self, exp, rounding):
| if self._is_special:
return Decimal(self)
if (not self):
return _dec_from_triple(self._sign, '0', exp)
if (self._exp >= exp):
return _dec_from_triple(self._sign, (self._int + ('0' * (self._exp - exp))), exp)
digits = ((len(self._int) + self._exp) - exp)
if (digits < 0):
self = _dec_from_triple(self._sign, '1', (exp - 1))
digits = 0
this_function = self._pick_rounding_function[rounding]
changed = this_function(self, digits)
coeff = (self._int[:digits] or '0')
if (changed == 1):
coeff = str((int(coeff) + 1))
return _dec_from_triple(self._sign, coeff, exp)
|
'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.'
| def _round(self, places, rounding):
| if (places <= 0):
raise ValueError('argument should be at least 1 in _round')
if (self._is_special or (not self)):
return Decimal(self)
ans = self._rescale(((self.adjusted() + 1) - places), rounding)
if (ans.adjusted() != self.adjusted()):
ans = ans._rescale(((ans.adjusted() + 1) - places), rounding)
return ans
|
'Rounds 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.'
| def to_integral_exact(self, rounding=None, context=None):
| if self._is_special:
ans = self._check_nans(context=context)
if ans:
return ans
return Decimal(self)
if (self._exp >= 0):
return Decimal(self)
if (not self):
return _dec_from_triple(self._sign, '0', 0)
if (context is None):
context = getcontext()
if (rounding is None):
rounding = context.rounding
ans = self._rescale(0, rounding)
if (ans != self):
context._raise_error(Inexact)
context._raise_error(Rounded)
return ans
|
'Rounds to the nearest integer, without raising inexact, rounded.'
| def to_integral_value(self, rounding=None, context=None):
| if (context is None):
context = getcontext()
if (rounding is None):
rounding = context.rounding
if self._is_special:
ans = self._check_nans(context=context)
if ans:
return ans
return Decimal(self)
if (self._exp >= 0):
return Decimal(self)
else:
return self._rescale(0, rounding)
|
'Return the square root of self.'
| def sqrt(self, context=None):
| if (context is None):
context = getcontext()
if self._is_special:
ans = self._check_nans(context=context)
if ans:
return ans
if (self._isinfinity() and (self._sign == 0)):
return Decimal(self)
if (not self):
ans = _dec_from_triple(self._sign, '0', (self._exp // 2))
return ans._fix(context)
if (self._sign == 1):
return context._raise_error(InvalidOperation, 'sqrt(-x), x > 0')
prec = (context.prec + 1)
op = _WorkRep(self)
e = (op.exp >> 1)
if (op.exp & 1):
c = (op.int * 10)
l = ((len(self._int) >> 1) + 1)
else:
c = op.int
l = ((len(self._int) + 1) >> 1)
shift = (prec - l)
if (shift >= 0):
c *= (100 ** shift)
exact = True
else:
(c, remainder) = divmod(c, (100 ** (- shift)))
exact = (not remainder)
e -= shift
n = (10 ** prec)
while True:
q = (c // n)
if (n <= q):
break
else:
n = ((n + q) >> 1)
exact = (exact and ((n * n) == c))
if exact:
if (shift >= 0):
n //= (10 ** shift)
else:
n *= (10 ** (- shift))
e += shift
elif ((n % 5) == 0):
n += 1
ans = _dec_from_triple(0, str(n), e)
context = context._shallow_copy()
rounding = context._set_rounding(ROUND_HALF_EVEN)
ans = ans._fix(context)
context.rounding = rounding
return ans
|
'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.'
| def max(self, other, context=None):
| other = _convert_other(other, raiseit=True)
if (context is None):
context = getcontext()
if (self._is_special or other._is_special):
sn = self._isnan()
on = other._isnan()
if (sn or on):
if ((on == 1) and (sn == 0)):
return self._fix(context)
if ((sn == 1) and (on == 0)):
return other._fix(context)
return self._check_nans(other, context)
c = self._cmp(other)
if (c == 0):
c = self.compare_total(other)
if (c == (-1)):
ans = other
else:
ans = self
return ans._fix(context)
|
'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.'
| def min(self, other, context=None):
| other = _convert_other(other, raiseit=True)
if (context is None):
context = getcontext()
if (self._is_special or other._is_special):
sn = self._isnan()
on = other._isnan()
if (sn or on):
if ((on == 1) and (sn == 0)):
return self._fix(context)
if ((sn == 1) and (on == 0)):
return other._fix(context)
return self._check_nans(other, context)
c = self._cmp(other)
if (c == 0):
c = self.compare_total(other)
if (c == (-1)):
ans = self
else:
ans = other
return ans._fix(context)
|
'Returns whether self is an integer'
| def _isinteger(self):
| if self._is_special:
return False
if (self._exp >= 0):
return True
rest = self._int[self._exp:]
return (rest == ('0' * len(rest)))
|
'Returns True if self is even. Assumes self is an integer.'
| def _iseven(self):
| if ((not self) or (self._exp > 0)):
return True
return (self._int[((-1) + self._exp)] in '02468')
|
'Return the adjusted exponent of self'
| def adjusted(self):
| try:
return ((self._exp + len(self._int)) - 1)
except TypeError:
return 0
|
'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.'
| def canonical(self, context=None):
| return self
|
'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.'
| def compare_signal(self, other, context=None):
| other = _convert_other(other, raiseit=True)
ans = self._compare_check_nans(other, context)
if ans:
return ans
return self.compare(other, context=context)
|
'Compares self to other using the abstract representations.
This is not like the standard compare, which use their numerical
value. Note that a total ordering is defined for all possible abstract
representations.'
| def compare_total(self, other):
| other = _convert_other(other, raiseit=True)
if (self._sign and (not other._sign)):
return _NegativeOne
if ((not self._sign) and other._sign):
return _One
sign = self._sign
self_nan = self._isnan()
other_nan = other._isnan()
if (self_nan or other_nan):
if (self_nan == other_nan):
self_key = (len(self._int), self._int)
other_key = (len(other._int), other._int)
if (self_key < other_key):
if sign:
return _One
else:
return _NegativeOne
if (self_key > other_key):
if sign:
return _NegativeOne
else:
return _One
return _Zero
if sign:
if (self_nan == 1):
return _NegativeOne
if (other_nan == 1):
return _One
if (self_nan == 2):
return _NegativeOne
if (other_nan == 2):
return _One
else:
if (self_nan == 1):
return _One
if (other_nan == 1):
return _NegativeOne
if (self_nan == 2):
return _One
if (other_nan == 2):
return _NegativeOne
if (self < other):
return _NegativeOne
if (self > other):
return _One
if (self._exp < other._exp):
if sign:
return _One
else:
return _NegativeOne
if (self._exp > other._exp):
if sign:
return _NegativeOne
else:
return _One
return _Zero
|
'Compares self to other using abstract repr., ignoring sign.
Like compare_total, but with operand\'s sign ignored and assumed to be 0.'
| def compare_total_mag(self, other):
| other = _convert_other(other, raiseit=True)
s = self.copy_abs()
o = other.copy_abs()
return s.compare_total(o)
|
'Returns a copy with the sign set to 0.'
| def copy_abs(self):
| return _dec_from_triple(0, self._int, self._exp, self._is_special)
|
'Returns a copy with the sign inverted.'
| def copy_negate(self):
| if self._sign:
return _dec_from_triple(0, self._int, self._exp, self._is_special)
else:
return _dec_from_triple(1, self._int, self._exp, self._is_special)
|
'Returns self with the sign of other.'
| def copy_sign(self, other):
| other = _convert_other(other, raiseit=True)
return _dec_from_triple(other._sign, self._int, self._exp, self._is_special)
|
'Returns e ** self.'
| def exp(self, context=None):
| if (context is None):
context = getcontext()
ans = self._check_nans(context=context)
if ans:
return ans
if (self._isinfinity() == (-1)):
return _Zero
if (not self):
return _One
if (self._isinfinity() == 1):
return Decimal(self)
p = context.prec
adj = self.adjusted()
if ((self._sign == 0) and (adj > len(str(((context.Emax + 1) * 3))))):
ans = _dec_from_triple(0, '1', (context.Emax + 1))
elif ((self._sign == 1) and (adj > len(str((((- context.Etiny()) + 1) * 3))))):
ans = _dec_from_triple(0, '1', (context.Etiny() - 1))
elif ((self._sign == 0) and (adj < (- p))):
ans = _dec_from_triple(0, (('1' + ('0' * (p - 1))) + '1'), (- p))
elif ((self._sign == 1) and (adj < ((- p) - 1))):
ans = _dec_from_triple(0, ('9' * (p + 1)), ((- p) - 1))
else:
op = _WorkRep(self)
(c, e) = (op.int, op.exp)
if (op.sign == 1):
c = (- c)
extra = 3
while True:
(coeff, exp) = _dexp(c, e, (p + extra))
if (coeff % (5 * (10 ** ((len(str(coeff)) - p) - 1)))):
break
extra += 3
ans = _dec_from_triple(0, str(coeff), exp)
context = context._shallow_copy()
rounding = context._set_rounding(ROUND_HALF_EVEN)
ans = ans._fix(context)
context.rounding = rounding
return ans
|
'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.'
| def is_canonical(self):
| return True
|
'Return True if self is finite; otherwise return False.
A Decimal instance is considered finite if it is neither
infinite nor a NaN.'
| def is_finite(self):
| return (not self._is_special)
|
'Return True if self is infinite; otherwise return False.'
| def is_infinite(self):
| return (self._exp == 'F')
|
'Return True if self is a qNaN or sNaN; otherwise return False.'
| def is_nan(self):
| return (self._exp in ('n', 'N'))
|
'Return True if self is a normal number; otherwise return False.'
| def is_normal(self, context=None):
| if (self._is_special or (not self)):
return False
if (context is None):
context = getcontext()
return (context.Emin <= self.adjusted())
|
'Return True if self is a quiet NaN; otherwise return False.'
| def is_qnan(self):
| return (self._exp == 'n')
|
'Return True if self is negative; otherwise return False.'
| def is_signed(self):
| return (self._sign == 1)
|
'Return True if self is a signaling NaN; otherwise return False.'
| def is_snan(self):
| return (self._exp == 'N')
|
'Return True if self is subnormal; otherwise return False.'
| def is_subnormal(self, context=None):
| if (self._is_special or (not self)):
return False
if (context is None):
context = getcontext()
return (self.adjusted() < context.Emin)
|
'Return True if self is a zero; otherwise return False.'
| def is_zero(self):
| return ((not self._is_special) and (self._int == '0'))
|
'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.'
| def _ln_exp_bound(self):
| adj = ((self._exp + len(self._int)) - 1)
if (adj >= 1):
return (len(str(((adj * 23) // 10))) - 1)
if (adj <= (-2)):
return (len(str(((((-1) - adj) * 23) // 10))) - 1)
op = _WorkRep(self)
(c, e) = (op.int, op.exp)
if (adj == 0):
num = str((c - (10 ** (- e))))
den = str(c)
return ((len(num) - len(den)) - (num < den))
return ((e + len(str(((10 ** (- e)) - c)))) - 1)
|
'Returns the natural (base e) logarithm of self.'
| def ln(self, context=None):
| if (context is None):
context = getcontext()
ans = self._check_nans(context=context)
if ans:
return ans
if (not self):
return _NegativeInfinity
if (self._isinfinity() == 1):
return _Infinity
if (self == _One):
return _Zero
if (self._sign == 1):
return context._raise_error(InvalidOperation, 'ln of a negative value')
op = _WorkRep(self)
(c, e) = (op.int, op.exp)
p = context.prec
places = ((p - self._ln_exp_bound()) + 2)
while True:
coeff = _dlog(c, e, places)
if (coeff % (5 * (10 ** ((len(str(abs(coeff))) - p) - 1)))):
break
places += 3
ans = _dec_from_triple(int((coeff < 0)), str(abs(coeff)), (- places))
context = context._shallow_copy()
rounding = context._set_rounding(ROUND_HALF_EVEN)
ans = ans._fix(context)
context.rounding = rounding
return ans
|
'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.'
| def _log10_exp_bound(self):
| adj = ((self._exp + len(self._int)) - 1)
if (adj >= 1):
return (len(str(adj)) - 1)
if (adj <= (-2)):
return (len(str(((-1) - adj))) - 1)
op = _WorkRep(self)
(c, e) = (op.int, op.exp)
if (adj == 0):
num = str((c - (10 ** (- e))))
den = str((231 * c))
return (((len(num) - len(den)) - (num < den)) + 2)
num = str(((10 ** (- e)) - c))
return (((len(num) + e) - (num < '231')) - 1)
|
'Returns the base 10 logarithm of self.'
| def log10(self, context=None):
| if (context is None):
context = getcontext()
ans = self._check_nans(context=context)
if ans:
return ans
if (not self):
return _NegativeInfinity
if (self._isinfinity() == 1):
return _Infinity
if (self._sign == 1):
return context._raise_error(InvalidOperation, 'log10 of a negative value')
if ((self._int[0] == '1') and (self._int[1:] == ('0' * (len(self._int) - 1)))):
ans = Decimal(((self._exp + len(self._int)) - 1))
else:
op = _WorkRep(self)
(c, e) = (op.int, op.exp)
p = context.prec
places = ((p - self._log10_exp_bound()) + 2)
while True:
coeff = _dlog10(c, e, places)
if (coeff % (5 * (10 ** ((len(str(abs(coeff))) - p) - 1)))):
break
places += 3
ans = _dec_from_triple(int((coeff < 0)), str(abs(coeff)), (- places))
context = context._shallow_copy()
rounding = context._set_rounding(ROUND_HALF_EVEN)
ans = ans._fix(context)
context.rounding = rounding
return ans
|
'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).'
| def logb(self, context=None):
| ans = self._check_nans(context=context)
if ans:
return ans
if (context is None):
context = getcontext()
if self._isinfinity():
return _Infinity
if (not self):
return context._raise_error(DivisionByZero, 'logb(0)', 1)
ans = Decimal(self.adjusted())
return ans._fix(context)
|
'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.'
| def _islogical(self):
| if ((self._sign != 0) or (self._exp != 0)):
return False
for dig in self._int:
if (dig not in '01'):
return False
return True
|
'Applies an \'and\' operation between self and other\'s digits.'
| def logical_and(self, other, context=None):
| if (context is None):
context = getcontext()
other = _convert_other(other, raiseit=True)
if ((not self._islogical()) or (not other._islogical())):
return context._raise_error(InvalidOperation)
(opa, opb) = self._fill_logical(context, self._int, other._int)
result = ''.join([str((int(a) & int(b))) for (a, b) in zip(opa, opb)])
return _dec_from_triple(0, (result.lstrip('0') or '0'), 0)
|
'Invert all its digits.'
| def logical_invert(self, context=None):
| if (context is None):
context = getcontext()
return self.logical_xor(_dec_from_triple(0, ('1' * context.prec), 0), context)
|
'Applies an \'or\' operation between self and other\'s digits.'
| def logical_or(self, other, context=None):
| if (context is None):
context = getcontext()
other = _convert_other(other, raiseit=True)
if ((not self._islogical()) or (not other._islogical())):
return context._raise_error(InvalidOperation)
(opa, opb) = self._fill_logical(context, self._int, other._int)
result = ''.join([str((int(a) | int(b))) for (a, b) in zip(opa, opb)])
return _dec_from_triple(0, (result.lstrip('0') or '0'), 0)
|
'Applies an \'xor\' operation between self and other\'s digits.'
| def logical_xor(self, other, context=None):
| if (context is None):
context = getcontext()
other = _convert_other(other, raiseit=True)
if ((not self._islogical()) or (not other._islogical())):
return context._raise_error(InvalidOperation)
(opa, opb) = self._fill_logical(context, self._int, other._int)
result = ''.join([str((int(a) ^ int(b))) for (a, b) in zip(opa, opb)])
return _dec_from_triple(0, (result.lstrip('0') or '0'), 0)
|
'Compares the values numerically with their sign ignored.'
| def max_mag(self, other, context=None):
| other = _convert_other(other, raiseit=True)
if (context is None):
context = getcontext()
if (self._is_special or other._is_special):
sn = self._isnan()
on = other._isnan()
if (sn or on):
if ((on == 1) and (sn == 0)):
return self._fix(context)
if ((sn == 1) and (on == 0)):
return other._fix(context)
return self._check_nans(other, context)
c = self.copy_abs()._cmp(other.copy_abs())
if (c == 0):
c = self.compare_total(other)
if (c == (-1)):
ans = other
else:
ans = self
return ans._fix(context)
|
'Compares the values numerically with their sign ignored.'
| def min_mag(self, other, context=None):
| other = _convert_other(other, raiseit=True)
if (context is None):
context = getcontext()
if (self._is_special or other._is_special):
sn = self._isnan()
on = other._isnan()
if (sn or on):
if ((on == 1) and (sn == 0)):
return self._fix(context)
if ((sn == 1) and (on == 0)):
return other._fix(context)
return self._check_nans(other, context)
c = self.copy_abs()._cmp(other.copy_abs())
if (c == 0):
c = self.compare_total(other)
if (c == (-1)):
ans = self
else:
ans = other
return ans._fix(context)
|
'Returns the largest representable number smaller than itself.'
| def next_minus(self, context=None):
| if (context is None):
context = getcontext()
ans = self._check_nans(context=context)
if ans:
return ans
if (self._isinfinity() == (-1)):
return _NegativeInfinity
if (self._isinfinity() == 1):
return _dec_from_triple(0, ('9' * context.prec), context.Etop())
context = context.copy()
context._set_rounding(ROUND_FLOOR)
context._ignore_all_flags()
new_self = self._fix(context)
if (new_self != self):
return new_self
return self.__sub__(_dec_from_triple(0, '1', (context.Etiny() - 1)), context)
|
'Returns the smallest representable number larger than itself.'
| def next_plus(self, context=None):
| if (context is None):
context = getcontext()
ans = self._check_nans(context=context)
if ans:
return ans
if (self._isinfinity() == 1):
return _Infinity
if (self._isinfinity() == (-1)):
return _dec_from_triple(1, ('9' * context.prec), context.Etop())
context = context.copy()
context._set_rounding(ROUND_CEILING)
context._ignore_all_flags()
new_self = self._fix(context)
if (new_self != self):
return new_self
return self.__add__(_dec_from_triple(0, '1', (context.Etiny() - 1)), context)
|
'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.'
| def next_toward(self, other, context=None):
| other = _convert_other(other, raiseit=True)
if (context is None):
context = getcontext()
ans = self._check_nans(other, context)
if ans:
return ans
comparison = self._cmp(other)
if (comparison == 0):
return self.copy_sign(other)
if (comparison == (-1)):
ans = self.next_plus(context)
else:
ans = self.next_minus(context)
if ans._isinfinity():
context._raise_error(Overflow, 'Infinite result from next_toward', ans._sign)
context._raise_error(Inexact)
context._raise_error(Rounded)
elif (ans.adjusted() < context.Emin):
context._raise_error(Underflow)
context._raise_error(Subnormal)
context._raise_error(Inexact)
context._raise_error(Rounded)
if (not ans):
context._raise_error(Clamped)
return ans
|
'Returns 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'
| def number_class(self, context=None):
| if self.is_snan():
return 'sNaN'
if self.is_qnan():
return 'NaN'
inf = self._isinfinity()
if (inf == 1):
return '+Infinity'
if (inf == (-1)):
return '-Infinity'
if self.is_zero():
if self._sign:
return '-Zero'
else:
return '+Zero'
if (context is None):
context = getcontext()
if self.is_subnormal(context=context):
if self._sign:
return '-Subnormal'
else:
return '+Subnormal'
if self._sign:
return '-Normal'
else:
return '+Normal'
|
'Just returns 10, as this is Decimal, :)'
| def radix(self):
| return Decimal(10)
|
'Returns a rotated copy of self, value-of-other times.'
| def rotate(self, other, context=None):
| if (context is None):
context = getcontext()
other = _convert_other(other, raiseit=True)
ans = self._check_nans(other, context)
if ans:
return ans
if (other._exp != 0):
return context._raise_error(InvalidOperation)
if (not ((- context.prec) <= int(other) <= context.prec)):
return context._raise_error(InvalidOperation)
if self._isinfinity():
return Decimal(self)
torot = int(other)
rotdig = self._int
topad = (context.prec - len(rotdig))
if (topad > 0):
rotdig = (('0' * topad) + rotdig)
elif (topad < 0):
rotdig = rotdig[(- topad):]
rotated = (rotdig[torot:] + rotdig[:torot])
return _dec_from_triple(self._sign, (rotated.lstrip('0') or '0'), self._exp)
|
'Returns self operand after adding the second value to its exp.'
| def scaleb(self, other, context=None):
| if (context is None):
context = getcontext()
other = _convert_other(other, raiseit=True)
ans = self._check_nans(other, context)
if ans:
return ans
if (other._exp != 0):
return context._raise_error(InvalidOperation)
liminf = ((-2) * (context.Emax + context.prec))
limsup = (2 * (context.Emax + context.prec))
if (not (liminf <= int(other) <= limsup)):
return context._raise_error(InvalidOperation)
if self._isinfinity():
return Decimal(self)
d = _dec_from_triple(self._sign, self._int, (self._exp + int(other)))
d = d._fix(context)
return d
|
'Returns a shifted copy of self, value-of-other times.'
| def shift(self, other, context=None):
| if (context is None):
context = getcontext()
other = _convert_other(other, raiseit=True)
ans = self._check_nans(other, context)
if ans:
return ans
if (other._exp != 0):
return context._raise_error(InvalidOperation)
if (not ((- context.prec) <= int(other) <= context.prec)):
return context._raise_error(InvalidOperation)
if self._isinfinity():
return Decimal(self)
torot = int(other)
rotdig = self._int
topad = (context.prec - len(rotdig))
if (topad > 0):
rotdig = (('0' * topad) + rotdig)
elif (topad < 0):
rotdig = rotdig[(- topad):]
if (torot < 0):
shifted = rotdig[:torot]
else:
shifted = (rotdig + ('0' * torot))
shifted = shifted[(- context.prec):]
return _dec_from_triple(self._sign, (shifted.lstrip('0') or '0'), self._exp)
|
'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.'
| def __format__(self, specifier, context=None, _localeconv=None):
| if (context is None):
context = getcontext()
spec = _parse_format_specifier(specifier, _localeconv=_localeconv)
if self._is_special:
sign = _format_sign(self._sign, spec)
body = str(self.copy_abs())
if (spec['type'] == '%'):
body += '%'
return _format_align(sign, body, spec)
if (spec['type'] is None):
spec['type'] = ['g', 'G'][context.capitals]
if (spec['type'] == '%'):
self = _dec_from_triple(self._sign, self._int, (self._exp + 2))
rounding = context.rounding
precision = spec['precision']
if (precision is not None):
if (spec['type'] in 'eE'):
self = self._round((precision + 1), rounding)
elif (spec['type'] in 'fF%'):
self = self._rescale((- precision), rounding)
elif ((spec['type'] in 'gG') and (len(self._int) > precision)):
self = self._round(precision, rounding)
if ((not self) and (self._exp > 0) and (spec['type'] in 'fF%')):
self = self._rescale(0, rounding)
leftdigits = (self._exp + len(self._int))
if (spec['type'] in 'eE'):
if ((not self) and (precision is not None)):
dotplace = (1 - precision)
else:
dotplace = 1
elif (spec['type'] in 'fF%'):
dotplace = leftdigits
elif (spec['type'] in 'gG'):
if ((self._exp <= 0) and (leftdigits > (-6))):
dotplace = leftdigits
else:
dotplace = 1
if (dotplace < 0):
intpart = '0'
fracpart = (('0' * (- dotplace)) + self._int)
elif (dotplace > len(self._int)):
intpart = (self._int + ('0' * (dotplace - len(self._int))))
fracpart = ''
else:
intpart = (self._int[:dotplace] or '0')
fracpart = self._int[dotplace:]
exp = (leftdigits - dotplace)
return _format_number(self._sign, intpart, fracpart, exp, spec)
|
'Show the current context.'
| def __repr__(self):
| s = []
s.append(('Context(prec=%(prec)d, rounding=%(rounding)s, Emin=%(Emin)d, Emax=%(Emax)d, capitals=%(capitals)d' % vars(self)))
names = [f.__name__ for (f, v) in self.flags.items() if v]
s.append((('flags=[' + ', '.join(names)) + ']'))
names = [t.__name__ for (t, v) in self.traps.items() if v]
s.append((('traps=[' + ', '.join(names)) + ']'))
return (', '.join(s) + ')')
|
'Reset all flags to zero'
| def clear_flags(self):
| for flag in self.flags:
self.flags[flag] = 0
|
'Returns a shallow copy from self.'
| def _shallow_copy(self):
| nc = Context(self.prec, self.rounding, self.traps, self.flags, self.Emin, self.Emax, self.capitals, self._clamp, self._ignored_flags)
return nc
|
'Returns a deep copy from self.'
| def copy(self):
| nc = Context(self.prec, self.rounding, self.traps.copy(), self.flags.copy(), self.Emin, self.Emax, self.capitals, self._clamp, self._ignored_flags)
return nc
|
'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.'
| def _raise_error(self, condition, explanation=None, *args):
| error = _condition_map.get(condition, condition)
if (error in self._ignored_flags):
return error().handle(self, *args)
self.flags[error] = 1
if (not self.traps[error]):
return condition().handle(self, *args)
raise error(explanation)
|
'Ignore all flags, if they are raised'
| def _ignore_all_flags(self):
| return self._ignore_flags(*_signals)
|
'Ignore the flags, if they are raised'
| def _ignore_flags(self, *flags):
| self._ignored_flags = (self._ignored_flags + list(flags))
return list(flags)
|
'Stop ignoring the flags, if they are raised'
| def _regard_flags(self, *flags):
| if (flags and isinstance(flags[0], (tuple, list))):
flags = flags[0]
for flag in flags:
self._ignored_flags.remove(flag)
|
'Returns Etiny (= Emin - prec + 1)'
| def Etiny(self):
| return int(((self.Emin - self.prec) + 1))
|
'Returns maximum exponent (= Emax - prec + 1)'
| def Etop(self):
| return int(((self.Emax - self.prec) + 1))
|
'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.'
| def _set_rounding(self, type):
| rounding = self.rounding
self.rounding = type
return rounding
|
'Creates a new Decimal instance but using self as context.
This method implements the to-number operation of the
IBM Decimal specification.'
| def create_decimal(self, num='0'):
| if (isinstance(num, basestring) and (num != num.strip())):
return self._raise_error(ConversionSyntax, 'no trailing or leading whitespace is permitted.')
d = Decimal(num, context=self)
if (d._isnan() and (len(d._int) > (self.prec - self._clamp))):
return self._raise_error(ConversionSyntax, 'diagnostic info too long in NaN')
return d._fix(self)
|
'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):
Inexact: None'
| def create_decimal_from_float(self, f):
| d = Decimal.from_float(f)
return d._fix(self)
|
'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\')'
| def abs(self, a):
| a = _convert_other(a, raiseit=True)
return a.__abs__(context=self)
|
'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\')'
| def add(self, a, b):
| a = _convert_other(a, raiseit=True)
r = a.__add__(b, context=self)
if (r is NotImplemented):
raise TypeError(('Unable to convert %s to Decimal' % b))
else:
return r
|
'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\')'
| def canonical(self, a):
| return a.canonical(context=self)
|
'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\')'
| def compare(self, a, b):
| a = _convert_other(a, raiseit=True)
return a.compare(b, context=self)
|
'Compares 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\')'
| def compare_signal(self, a, b):
| a = _convert_other(a, raiseit=True)
return a.compare_signal(b, context=self)
|
'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\')'
| def compare_total(self, a, b):
| a = _convert_other(a, raiseit=True)
return a.compare_total(b)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.