desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'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()) 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)
'Compares two operands using their abstract representation ignoring sign. Like compare_total, but with operand\'s sign ignored and assumed to be 0.'
def compare_total_mag(self, a, b):
a = _convert_other(a, raiseit=True) return a.compare_total_mag(b)
'Returns 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\')'
def copy_abs(self, a):
a = _convert_other(a, raiseit=True) return a.copy_abs()
'Returns 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\')'
def copy_decimal(self, a):
a = _convert_other(a, raiseit=True) return Decimal(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\')'
def copy_negate(self, a):
a = _convert_other(a, raiseit=True) return a.copy_negate()
'Copies 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\')'
def copy_sign(self, a, b):
a = _convert_other(a, raiseit=True) return a.copy_sign(b)
'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\')'
def divide(self, a, b):
a = _convert_other(a, raiseit=True) r = a.__div__(b, context=self) if (r is NotImplemented): raise TypeError(('Unable to convert %s to Decimal' % b)) else: return r
'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\')'
def divide_int(self, a, b):
a = _convert_other(a, raiseit=True) r = a.__floordiv__(b, context=self) if (r is NotImplemented): raise TypeError(('Unable to convert %s to Decimal' % b)) else: return r
'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\'))'
def divmod(self, a, b):
a = _convert_other(a, raiseit=True) r = a.__divmod__(b, context=self) if (r is NotImplemented): raise TypeError(('Unable to convert %s to Decimal' % b)) else: return r
'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\')'
def exp(self, a):
a = _convert_other(a, raiseit=True) return a.exp(context=self)
'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\')'
def fma(self, a, b, c):
a = _convert_other(a, raiseit=True) return a.fma(b, c, context=self)
'Return 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'
def is_canonical(self, a):
return a.is_canonical()
'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'
def is_finite(self, a):
a = _convert_other(a, raiseit=True) return a.is_finite()
'Return 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'
def is_infinite(self, a):
a = _convert_other(a, raiseit=True) return a.is_infinite()
'Return 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'
def is_nan(self, a):
a = _convert_other(a, raiseit=True) return a.is_nan()
'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'
def is_normal(self, a):
a = _convert_other(a, raiseit=True) return a.is_normal(context=self)
'Return 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'
def is_qnan(self, a):
a = _convert_other(a, raiseit=True) return a.is_qnan()
'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'
def is_signed(self, a):
a = _convert_other(a, raiseit=True) return a.is_signed()
'Return 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'
def is_snan(self, a):
a = _convert_other(a, raiseit=True) return a.is_snan()
'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'
def is_subnormal(self, a):
a = _convert_other(a, raiseit=True) return a.is_subnormal(context=self)
'Return 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'
def is_zero(self, a):
a = _convert_other(a, raiseit=True) return a.is_zero()
'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\')'
def ln(self, a):
a = _convert_other(a, raiseit=True) return a.ln(context=self)
'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\')'
def log10(self, a):
a = _convert_other(a, raiseit=True) return a.log10(context=self)
'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\')'
def logb(self, a):
a = _convert_other(a, raiseit=True) return a.logb(context=self)
'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\')'
def logical_and(self, a, b):
a = _convert_other(a, raiseit=True) return a.logical_and(b, context=self)
'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\')'
def logical_invert(self, a):
a = _convert_other(a, raiseit=True) return a.logical_invert(context=self)
'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\')'
def logical_or(self, a, b):
a = _convert_other(a, raiseit=True) return a.logical_or(b, context=self)
'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\')'
def logical_xor(self, a, b):
a = _convert_other(a, raiseit=True) return a.logical_xor(b, context=self)
'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\')'
def max(self, a, b):
a = _convert_other(a, raiseit=True) return a.max(b, context=self)
'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\')'
def max_mag(self, a, b):
a = _convert_other(a, raiseit=True) return a.max_mag(b, context=self)
'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\')'
def min(self, a, b):
a = _convert_other(a, raiseit=True) return a.min(b, context=self)
'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\')'
def min_mag(self, a, b):
a = _convert_other(a, raiseit=True) return a.min_mag(b, context=self)
'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\')'
def minus(self, a):
a = _convert_other(a, raiseit=True) return a.__neg__(context=self)
'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\')'
def multiply(self, a, b):
a = _convert_other(a, raiseit=True) r = a.__mul__(b, context=self) if (r is NotImplemented): raise TypeError(('Unable to convert %s to Decimal' % b)) else: return r
'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\')'
def next_minus(self, a):
a = _convert_other(a, raiseit=True) return a.next_minus(context=self)
'Returns 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\')'
def next_plus(self, a):
a = _convert_other(a, raiseit=True) return a.next_plus(context=self)
'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\')'
def next_toward(self, a, b):
a = _convert_other(a, raiseit=True) return a.next_toward(b, context=self)
'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\')'
def normalize(self, a):
a = _convert_other(a, raiseit=True) return a.normalize(context=self)
'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 = Context(ExtendedContext) >>> 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\''
def number_class(self, a):
a = _convert_other(a, raiseit=True) return a.number_class(context=self)