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

Method __truediv__

Lib/_pydecimal.py:1278–1335  ·  view source on GitHub ↗

Return self / other.

(self, other, context=None)

Source from the content-addressed store, hash-verified

1276 __rmul__ = __mul__
1277
1278 def __truediv__(self, other, context=None):
1279 """Return self / other."""
1280 other = _convert_other(other)
1281 if other is NotImplemented:
1282 return NotImplemented
1283
1284 if context is None:
1285 context = getcontext()
1286
1287 sign = self._sign ^ other._sign
1288
1289 if self._is_special or other._is_special:
1290 ans = self._check_nans(other, context)
1291 if ans:
1292 return ans
1293
1294 if self._isinfinity() and other._isinfinity():
1295 return context._raise_error(InvalidOperation, '(+-)INF/(+-)INF')
1296
1297 if self._isinfinity():
1298 return _SignedInfinity[sign]
1299
1300 if other._isinfinity():
1301 context._raise_error(Clamped, 'Division by infinity')
1302 return _dec_from_triple(sign, '0', context.Etiny())
1303
1304 # Special cases for zeroes
1305 if not other:
1306 if not self:
1307 return context._raise_error(DivisionUndefined, '0 / 0')
1308 return context._raise_error(DivisionByZero, 'x / 0', sign)
1309
1310 if not self:
1311 exp = self._exp - other._exp
1312 coeff = 0
1313 else:
1314 # OK, so neither = 0, INF or NaN
1315 shift = len(other._int) - len(self._int) + context.prec + 1
1316 exp = self._exp - other._exp - shift
1317 op1 = _WorkRep(self)
1318 op2 = _WorkRep(other)
1319 if shift >= 0:
1320 coeff, remainder = divmod(op1.int * 10**shift, op2.int)
1321 else:
1322 coeff, remainder = divmod(op1.int, op2.int * 10**-shift)
1323 if remainder:
1324 # result is not exact; adjust to ensure correct rounding
1325 if coeff % 5 == 0:
1326 coeff += 1
1327 else:
1328 # result is exact; get as close to ideal exponent as possible
1329 ideal_exp = self._exp - other._exp
1330 while exp < ideal_exp and coeff % 10 == 0:
1331 coeff //= 10
1332 exp += 1
1333
1334 ans = _dec_from_triple(sign, str(coeff), exp)
1335 return ans._fix(context)

Callers 2

__rtruediv__Method · 0.45
divideMethod · 0.45

Calls 10

_check_nansMethod · 0.95
_isinfinityMethod · 0.95
_convert_otherFunction · 0.85
getcontextFunction · 0.85
_dec_from_tripleFunction · 0.85
_WorkRepClass · 0.85
strFunction · 0.85
_raise_errorMethod · 0.80
EtinyMethod · 0.80
_fixMethod · 0.80

Tested by

no test coverage detected