Lines Matching refs:other
728 def _check_nans(self, other=None, context=None):
731 if self, other are sNaN, signal
732 if self, other are NaN return nan
739 if other is None:
742 other_is_nan = other._isnan()
753 other)
757 return other._fix_nan(context)
760 def _compare_check_nans(self, other, context):
764 Signal InvalidOperation if either self or other is a (quiet
774 if self._is_special or other._is_special:
779 elif other.is_snan():
782 other)
787 elif other.is_qnan():
790 other)
800 def _cmp(self, other):
801 """Compare the two non-NaN decimal instances self and other.
803 Returns -1 if self < other, 0 if self == other and 1
804 if self > other. This routine is for internal use only."""
806 if self._is_special or other._is_special:
808 other_inf = other._isinfinity()
818 if not other:
821 return -((-1)**other._sign)
822 if not other:
826 if other._sign < self._sign:
828 if self._sign < other._sign:
832 other_adjusted = other.adjusted()
834 self_padded = self._int + '0'*(self._exp - other._exp)
835 other_padded = other._int + '0'*(other._exp - self._exp)
864 def __eq__(self, other, context=None):
865 other = _convert_other(other, allow_float=True)
866 if other is NotImplemented:
867 return other
868 if self._check_nans(other, context):
870 return self._cmp(other) == 0
872 def __ne__(self, other, context=None):
873 other = _convert_other(other, allow_float=True)
874 if other is NotImplemented:
875 return other
876 if self._check_nans(other, context):
878 return self._cmp(other) != 0
880 def __lt__(self, other, context=None):
881 other = _convert_other(other, allow_float=True)
882 if other is NotImplemented:
883 return other
884 ans = self._compare_check_nans(other, context)
887 return self._cmp(other) < 0
889 def __le__(self, other, context=None):
890 other = _convert_other(other, allow_float=True)
891 if other is NotImplemented:
892 return other
893 ans = self._compare_check_nans(other, context)
896 return self._cmp(other) <= 0
898 def __gt__(self, other, context=None):
899 other = _convert_other(other, allow_float=True)
900 if other is NotImplemented:
901 return other
902 ans = self._compare_check_nans(other, context)
905 return self._cmp(other) > 0
907 def __ge__(self, other, context=None):
908 other = _convert_other(other, allow_float=True)
909 if other is NotImplemented:
910 return other
911 ans = self._compare_check_nans(other, context)
914 return self._cmp(other) >= 0
916 def compare(self, other, context=None):
925 other = _convert_other(other, raiseit=True)
928 if (self._is_special or other and other._is_special):
929 ans = self._check_nans(other, context)
933 return Decimal(self._cmp(other))
1126 def __add__(self, other, context=None):
1127 """Returns self + other.
1131 other = _convert_other(other)
1132 if other is NotImplemented:
1133 return other
1138 if self._is_special or other._is_special:
1139 ans = self._check_nans(other, context)
1145 if self._sign != other._sign and other._isinfinity():
1148 if other._isinfinity():
1149 return Decimal(other) # Can't both be infinity here
1151 exp = min(self._exp, other._exp)
1153 if context.rounding == ROUND_FLOOR and self._sign != other._sign:
1157 if not self and not other:
1158 sign = min(self._sign, other._sign)
1165 exp = max(exp, other._exp - context.prec-1)
1166 ans = other._rescale(exp, context.rounding)
1169 if not other:
1176 op2 = _WorkRep(other)
1214 def __sub__(self, other, context=None):
1215 """Return self - other"""
1216 other = _convert_other(other)
1217 if other is NotImplemented:
1218 return other
1220 if self._is_special or other._is_special:
1221 ans = self._check_nans(other, context=context)
1225 # self - other is computed as self + other.copy_negate()
1226 return self.__add__(other.copy_negate(), context=context)
1228 def __rsub__(self, other, context=None):
1229 """Return other - self"""
1230 other = _convert_other(other)
1231 if other is NotImplemented:
1232 return other
1234 return other.__sub__(self, context=context)
1236 def __mul__(self, other, context=None):
1237 """Return self * other.
1241 other = _convert_other(other)
1242 if other is NotImplemented:
1243 return other
1248 resultsign = self._sign ^ other._sign
1250 if self._is_special or other._is_special:
1251 ans = self._check_nans(other, context)
1256 if not other:
1260 if other._isinfinity():
1265 resultexp = self._exp + other._exp
1268 if not self or not other:
1276 ans = _dec_from_triple(resultsign, other._int, resultexp)
1279 if other._int == '1':
1285 op2 = _WorkRep(other)
1293 def __truediv__(self, other, context=None):
1294 """Return self / other."""
1295 other = _convert_other(other)
1296 if other is NotImplemented:
1302 sign = self._sign ^ other._sign
1304 if self._is_special or other._is_special:
1305 ans = self._check_nans(other, context)
1309 if self._isinfinity() and other._isinfinity():
1315 if other._isinfinity():
1320 if not other:
1326 exp = self._exp - other._exp
1330 shift = len(other._int) - len(self._int) + context.prec + 1
1331 exp = self._exp - other._exp - shift
1333 op2 = _WorkRep(other)
1344 ideal_exp = self._exp - other._exp
1352 def _divide(self, other, context):
1353 """Return (self // other, self % other), to context.prec precision.
1355 Assumes that neither self nor other is a NaN, that self is not
1356 infinite and that other is nonzero.
1358 sign = self._sign ^ other._sign
1359 if other._isinfinity():
1362 ideal_exp = min(self._exp, other._exp)
1364 expdiff = self.adjusted() - other.adjusted()
1365 if not self or other._isinfinity() or expdiff <= -2:
1370 op2 = _WorkRep(other)
1385 def __rtruediv__(self, other, context=None):
1386 """Swaps self/other and returns __truediv__."""
1387 other = _convert_other(other)
1388 if other is NotImplemented:
1389 return other
1390 return other.__truediv__(self, context=context)
1395 def __divmod__(self, other, context=None):
1397 Return (self // other, self % other)
1399 other = _convert_other(other)
1400 if other is NotImplemented:
1401 return other
1406 ans = self._check_nans(other, context)
1410 sign = self._sign ^ other._sign
1412 if other._isinfinity():
1419 if not other:
1427 quotient, remainder = self._divide(other, context)
1431 def __rdivmod__(self, other, context=None):
1432 """Swaps self/other and returns __divmod__."""
1433 other = _convert_other(other)
1434 if other is NotImplemented:
1435 return other
1436 return other.__divmod__(self, context=context)
1438 def __mod__(self, other, context=None):
1440 self % other
1442 other = _convert_other(other)
1443 if other is NotImplemented:
1444 return other
1449 ans = self._check_nans(other, context)
1455 elif not other:
1461 remainder = self._divide(other, context)[1]
1465 def __rmod__(self, other, context=None):
1466 """Swaps self/other and returns __mod__."""
1467 other = _convert_other(other)
1468 if other is NotImplemented:
1469 return other
1470 return other.__mod__(self, context=context)
1472 def remainder_near(self, other, context=None):
1474 Remainder nearest to 0- abs(remainder-near) <= other/2
1479 other = _convert_other(other, raiseit=True)
1481 ans = self._check_nans(other, context)
1490 # other == 0 -> either InvalidOperation or DivisionUndefined
1491 if not other:
1499 # other = +/-infinity -> remainder = self
1500 if other._isinfinity():
1505 ideal_exponent = min(self._exp, other._exp)
1511 expdiff = self.adjusted() - other.adjusted()
1513 # expdiff >= prec+1 => abs(self/other) > 10**prec
1516 # expdiff <= -2 => abs(self/other) < 0.1
1522 op2 = _WorkRep(other)
1528 # remainder is r*10**ideal_exponent; other is +/-op2.int *
1530 # abs(remainder) <= abs(other)/2
1547 def __floordiv__(self, other, context=None):
1548 """self // other"""
1549 other = _convert_other(other)
1550 if other is NotImplemented:
1551 return other
1556 ans = self._check_nans(other, context)
1561 if other._isinfinity():
1564 return _SignedInfinity[self._sign ^ other._sign]
1566 if not other:
1569 self._sign ^ other._sign)
1573 return self._divide(other, context)[0]
1575 def __rfloordiv__(self, other, context=None):
1576 """Swaps self/other and returns __floordiv__."""
1577 other = _convert_other(other)
1578 if other is NotImplemented:
1579 return other
1580 return other.__floordiv__(self, context=context)
1809 def fma(self, other, third, context=None):
1812 Returns self*other+third with no rounding of the intermediate
1813 product self*other.
1815 self and other are multiplied together, with no rounding of
1820 other = _convert_other(other, raiseit=True)
1824 if self._is_special or other._is_special:
1829 if other._exp == 'N':
1830 return context._raise_error(InvalidOperation, 'sNaN', other)
1833 elif other._exp == 'n':
1834 product = other
1836 if not other:
1839 product = _SignedInfinity[self._sign ^ other._sign]
1840 elif other._exp == 'F':
1844 product = _SignedInfinity[self._sign ^ other._sign]
1846 product = _dec_from_triple(self._sign ^ other._sign,
1847 str(int(self._int) * int(other._int)),
1848 self._exp + other._exp)
1853 def _power_modulo(self, other, modulo, context=None):
1856 # if can't convert other and modulo to Decimal, raise
1859 other = _convert_other(other, raiseit=True)
1868 other_is_nan = other._isnan()
1876 other)
1883 return other._fix_nan(context)
1888 other._isinteger() and
1893 if other < 0:
1911 if not other and not self:
1918 if other._iseven():
1923 # convert modulo to a Python integer, and self and other to
1927 exponent = _WorkRep(other.to_integral_value())
1937 def _power_exact(self, other, p):
1938 """Attempt to compute self**other exactly.
1940 Given Decimals self and other and an integer p, attempt to
1941 compute an exact result for the power self**other, with p
1942 digits of precision. Return None if self**other is not
1946 performed: self and other must both be nonspecial; self must
1947 be positive and not numerically equal to 1; other must be
1948 nonzero. For efficiency, other._exp should not be too large,
1949 so that 10**abs(other._exp) is a feasible calculation."""
1952 # value of other. Write x = xc*10**xe and abs(y) = yc*10**ye, with xc
2001 y = _WorkRep(other)
2020 # if other is a nonnegative integer, use ideal exponent
2021 if other._isinteger() and other._sign == 0:
2022 ideal_exponent = self._exp*int(other)
2167 if other._isinteger() and other._sign == 0:
2168 ideal_exponent = self._exp*int(other)
2174 def __pow__(self, other, modulo=None, context=None):
2175 """Return self ** other [ % modulo].
2177 With two arguments, compute self**other.
2179 With three arguments, compute (self**other) % modulo. For the
2184 - other must be nonnegative
2185 - either self or other (or both) must be nonzero
2192 The result of pow(self, other, modulo) is identical to the
2193 result that would be obtained by computing (self**other) %
2199 return self._power_modulo(other, modulo, context)
2201 other = _convert_other(other)
2202 if other is NotImplemented:
2203 return other
2209 ans = self._check_nans(other, context)
2214 if not other:
2220 # result has sign 1 iff self._sign is 1 and other is an odd integer
2223 if other._isinteger():
2224 if not other._iseven():
2237 if other._sign == 0:
2244 if other._sign == 0:
2249 # 1**other = 1, but the choice of exponent and the flags
2250 # depend on the exponent of self, and on whether other is a
2253 if other._isinteger():
2254 # exp = max(self._exp*max(int(other), 0),
2255 # 1-context.prec) but evaluating int(other) directly
2256 # is dangerous until we know other is small (other
2258 if other._sign == 1:
2260 elif other > context.prec:
2263 multiplier = int(other)
2281 other._isinfinity():
2282 if (other._sign == 0) == (self_adj < 0):
2293 # log10(self)*other >= 10**bound and bound >= len(str(Emax))
2295 # self**other >= 10**(Emax+1), so overflow occurs. The test
2297 bound = self._log10_exp_bound() + other.adjusted()
2298 if (self_adj >= 0) == (other._sign == 0):
2299 # self > 1 and other +ve, or self < 1 and other -ve
2304 # self > 1 and other -ve, or self < 1 and other +ve
2312 ans = self._power_exact(other, context.prec + 1)
2323 y = _WorkRep(other)
2342 # There's a difficulty here when 'other' is not an integer and
2352 if exact and not other._isinteger():
2390 def __rpow__(self, other, context=None):
2391 """Swaps self/other and returns __pow__."""
2392 other = _convert_other(other)
2393 if other is NotImplemented:
2394 return other
2395 return other.__pow__(self, context=context)
2493 def same_quantum(self, other):
2494 """Return True if self and other have the same exponent; otherwise
2502 other = _convert_other(other, raiseit=True)
2503 if self._is_special or other._is_special:
2504 return (self.is_nan() and other.is_nan() or
2505 self.is_infinite() and other.is_infinite())
2506 return self._exp == other._exp
2712 def max(self, other, context=None):
2715 Like max(self, other) except if one is not a number, returns
2718 other = _convert_other(other, raiseit=True)
2723 if self._is_special or other._is_special:
2724 # If one operand is a quiet NaN and the other is number, then the
2727 on = other._isnan()
2732 return other._fix(context)
2733 return self._check_nans(other, context)
2735 c = self._cmp(other)
2745 c = self.compare_total(other)
2748 ans = other
2754 def min(self, other, context=None):
2757 Like min(self, other) except if one is not a number, returns
2760 other = _convert_other(other, raiseit=True)
2765 if self._is_special or other._is_special:
2766 # If one operand is a quiet NaN and the other is number, then the
2769 on = other._isnan()
2774 return other._fix(context)
2775 return self._check_nans(other, context)
2777 c = self._cmp(other)
2779 c = self.compare_total(other)
2784 ans = other
2819 def compare_signal(self, other, context=None):
2820 """Compares self to the other operand numerically.
2825 other = _convert_other(other, raiseit = True)
2826 ans = self._compare_check_nans(other, context)
2829 return self.compare(other, context=context)
2831 def compare_total(self, other):
2832 """Compares self to other using the abstract representations.
2838 other = _convert_other(other, raiseit=True)
2840 # if one is negative and the other is positive, it's easy
2841 if self._sign and not other._sign:
2843 if not self._sign and other._sign:
2849 other_nan = other._isnan()
2854 other_key = len(other._int), other._int
2886 if self < other:
2888 if self > other:
2891 if self._exp < other._exp:
2896 if self._exp > other._exp:
2904 def compare_total_mag(self, other):
2905 """Compares self to other using abstract repr., ignoring sign.
2909 other = _convert_other(other, raiseit=True)
2912 o = other.copy_abs()
2926 def copy_sign(self, other):
2927 """Returns self with the sign of other."""
2928 other = _convert_other(other, raiseit=True)
2929 return _dec_from_triple(other._sign, self._int,
3065 In other words, compute r such that self.ln() >= 10**r. Assumes
3140 In other words, find r such that self.log10() >= 10**r.
3276 def logical_and(self, other, context=None):
3277 """Applies an 'and' operation between self and other's digits."""
3281 other = _convert_other(other, raiseit=True)
3283 if not self._islogical() or not other._islogical():
3287 (opa, opb) = self._fill_logical(context, self._int, other._int)
3300 def logical_or(self, other, context=None):
3301 """Applies an 'or' operation between self and other's digits."""
3305 other = _convert_other(other, raiseit=True)
3307 if not self._islogical() or not other._islogical():
3311 (opa, opb) = self._fill_logical(context, self._int, other._int)
3317 def logical_xor(self, other, context=None):
3318 """Applies an 'xor' operation between self and other's digits."""
3322 other = _convert_other(other, raiseit=True)
3324 if not self._islogical() or not other._islogical():
3328 (opa, opb) = self._fill_logical(context, self._int, other._int)
3334 def max_mag(self, other, context=None):
3336 other = _convert_other(other, raiseit=True)
3341 if self._is_special or other._is_special:
3342 # If one operand is a quiet NaN and the other is number, then the
3345 on = other._isnan()
3350 return other._fix(context)
3351 return self._check_nans(other, context)
3353 c = self.copy_abs()._cmp(other.copy_abs())
3355 c = self.compare_total(other)
3358 ans = other
3364 def min_mag(self, other, context=None):
3366 other = _convert_other(other, raiseit=True)
3371 if self._is_special or other._is_special:
3372 # If one operand is a quiet NaN and the other is number, then the
3375 on = other._isnan()
3380 return other._fix(context)
3381 return self._check_nans(other, context)
3383 c = self.copy_abs()._cmp(other.copy_abs())
3385 c = self.compare_total(other)
3390 ans = other
3440 def next_toward(self, other, context=None):
3441 """Returns the number closest to self, in the direction towards other.
3444 (excluding self) that is in the direction towards other,
3447 sign set to be the same as the sign of other.
3449 other = _convert_other(other, raiseit=True)
3454 ans = self._check_nans(other, context)
3458 comparison = self._cmp(other)
3460 return self.copy_sign(other)
3532 def rotate(self, other, context=None):
3533 """Returns a rotated copy of self, value-of-other times."""
3537 other = _convert_other(other, raiseit=True)
3539 ans = self._check_nans(other, context)
3543 if other._exp != 0:
3545 if not (-context.prec <= int(other) <= context.prec):
3552 torot = int(other)
3565 def scaleb(self, other, context=None):
3570 other = _convert_other(other, raiseit=True)
3572 ans = self._check_nans(other, context)
3576 if other._exp != 0:
3580 if not (liminf <= int(other) <= limsup):
3586 d = _dec_from_triple(self._sign, self._int, self._exp + int(other))
3590 def shift(self, other, context=None):
3591 """Returns a shifted copy of self, value-of-other times."""
3595 other = _convert_other(other, raiseit=True)
3597 ans = self._check_nans(other, context)
3601 if other._exp != 0:
3603 if not (-context.prec <= int(other) <= context.prec):
3610 torot = int(other)
3913 val = self.__sub__(other, context=context)
5054 Unlike other operations, if the length of the coefficient after the
5060 Also unlike other
5453 other = op1
5456 other = op2
5461 # for subtraction. So if other is smaller than 10**exp we replace
5462 # it with 10**exp. This avoids tmp.exp - other.exp getting too large.
5464 other_len = len(str(other.int))
5466 if other_len + other.exp - 1 < exp:
5467 other.int = 1
5468 other.exp = exp
5470 tmp.int *= 10 ** (tmp.exp - other.exp)
5471 tmp.exp = other.exp
5755 In other words, d*10**f is an approximation to exp(c*10**e) with p
5789 in other words, c*10**e is an approximation to x**y with p digits
5835 def _convert_other(other, raiseit=False, allow_float=False):
5836 """Convert other to Decimal.
5843 if isinstance(other, Decimal):
5844 return other
5845 if isinstance(other, (int, long)):
5846 return Decimal(other)
5847 if allow_float and isinstance(other, float):
5848 return Decimal.from_float(other)
5851 raise TypeError("Unable to convert %s to Decimal" % other)
5870 # contexts and be able to reproduce results from other implementations
5925 # class, and could potentially be reused or adapted for other pure