operator / method
Divides this decimal by other. Division by zero returns a non-finite
numeric value so calculated field validation can report the error instead
of failing during calculation.
Implementation
dynamic operator /(Object other) {
final right = _coerceOperand(other, operation: '/');
if (right.scaledValue == BigInt.zero) {
if (scaledValue == BigInt.zero) {
return double.nan;
}
return scaledValue.isNegative ? double.negativeInfinity : double.infinity;
}
final resultScale = _max3(scale, right.scale, 12);
final numerator = scaledValue * _pow10(right.scale + resultScale);
final denominator = right.scaledValue * _pow10(scale);
final negative =
(numerator.isNegative && !denominator.isNegative) ||
(!numerator.isNegative && denominator.isNegative);
final absNumerator = numerator.abs();
final absDenominator = denominator.abs();
final quotient = absNumerator ~/ absDenominator;
final remainder = absNumerator % absDenominator;
final rounded = remainder * BigInt.from(2) >= absDenominator
? quotient + BigInt.one
: quotient;
return FdcDecimal.fromScaled(
negative ? -rounded : rounded,
scale: resultScale,
);
}