MCPcopy Create free account
hub / github.com/RustPython/RustPython / __add__

Method __add__

Lib/_pydecimal.py:1108–1192  ·  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

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

Callers 4

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

Calls 13

_check_nansMethod · 0.95
_isinfinityMethod · 0.95
_fixMethod · 0.95
_rescaleMethod · 0.95
_convert_otherFunction · 0.85
getcontextFunction · 0.85
DecimalClass · 0.85
minFunction · 0.85
_dec_from_tripleFunction · 0.85
maxFunction · 0.85
_WorkRepClass · 0.85
_normalizeFunction · 0.85

Tested by

no test coverage detected