Why does the following code:
from decimal import Decimal
result = Decimal('0') * Decimal('0.8881783462119193534061639577')
print(result)
return 0E-28 ?
I've traced it to the following code in the module:
if not self or not other:
ans = _dec_from_triple(resultsign, '0', resultexp)
# Fixing in case the exponent is out of bounds
ans = ans._fix(context)
return ans
The code appears to follow Decimal Arithmetic Specification, which doesn't explicitly suggest what to do when we multiply by zero, referring to 'special numbers' from another standard, which also doesn't specify what we do when we multiply an integer by zero :) So the decimal library does the thing that is explicitly specified:
- The coefficient of the result, before rounding, is computed by multiplying together the coefficients of the operands.
- The exponent of the result, before rounding, is the sum of the exponents of the two operands.
- The sign of the result is the exclusive or of the signs of the operands.
Question: what is the need to return the coefficient and exponent (i.e, 0E-28) if one of the operands is a zero? We already know what that coefficient is when calling the multiplication function. Why not just return zero?