Returns self + other. -INF + INF (or the reverse) cause InvalidOperation errors.
(self, other, context=None)
| 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) |
no test coverage detected