MCPcopy Index your code
hub / github.com/python/cpython / __add__

Method __add__

Lib/_pydecimal.py:1111–1195  ·  view source on GitHub ↗

Returns self + other. -INF + INF (or the reverse) cause InvalidOperation errors.

(self, other, context=None)

Source from the content-addressed store, hash-verified

1109 return ans
1110
1111 def __add__(self, other, context=None):
1112 """Returns self + other.
1113
1114 -INF + INF (or the reverse) cause InvalidOperation errors.
1115 """
1116 other = _convert_other(other)
1117 if other is NotImplemented:
1118 return other
1119
1120 if context is None:
1121 context = getcontext()
1122
1123 if self._is_special or other._is_special:
1124 ans = self._check_nans(other, context)
1125 if ans:
1126 return ans
1127
1128 if self._isinfinity():
1129 # If both INF, same sign => same as both, opposite => error.
1130 if self._sign != other._sign and other._isinfinity():
1131 return context._raise_error(InvalidOperation, '-INF + INF')
1132 return Decimal(self)
1133 if other._isinfinity():
1134 return Decimal(other) # Can't both be infinity here
1135
1136 exp = min(self._exp, other._exp)
1137 negativezero = 0
1138 if context.rounding == ROUND_FLOOR and self._sign != other._sign:
1139 # If the answer is 0, the sign should be negative, in this case.
1140 negativezero = 1
1141
1142 if not self and not other:
1143 sign = min(self._sign, other._sign)
1144 if negativezero:
1145 sign = 1
1146 ans = _dec_from_triple(sign, '0', exp)
1147 ans = ans._fix(context)
1148 return ans
1149 if not self:
1150 exp = max(exp, other._exp - context.prec-1)
1151 ans = other._rescale(exp, context.rounding)
1152 ans = ans._fix(context)
1153 return ans
1154 if not other:
1155 exp = max(exp, self._exp - context.prec-1)
1156 ans = self._rescale(exp, context.rounding)
1157 ans = ans._fix(context)
1158 return ans
1159
1160 op1 = _WorkRep(self)
1161 op2 = _WorkRep(other)
1162 op1, op2 = _normalize(op1, op2, context.prec)
1163
1164 result = _WorkRep()
1165 if op1.sign != op2.sign:
1166 # Equal and opposite
1167 if op1.int == op2.int:
1168 ans = _dec_from_triple(negativezero, '0', exp)

Callers 4

__sub__Method · 0.95
next_plusMethod · 0.95
fmaMethod · 0.45
addMethod · 0.45

Calls 11

_check_nansMethod · 0.95
_isinfinityMethod · 0.95
_fixMethod · 0.95
_rescaleMethod · 0.95
_convert_otherFunction · 0.85
getcontextFunction · 0.85
DecimalClass · 0.85
_dec_from_tripleFunction · 0.85
_WorkRepClass · 0.85
_normalizeFunction · 0.85
_raise_errorMethod · 0.80

Tested by

no test coverage detected