You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
1e2000000000 + 1e-2000000000 throws ArgumentOutOfRangeException ("exponent must be non-negative"): exponent alignment wraps int #135
CommonizeSignificands subtracts the two exponents in unchecked int. With operands at opposite extremes, the difference wraps negative and is passed to Pow10, which rejects it with an unrelated ArgumentOutOfRangeException.
Where
PreciseNumber/PreciseNumber.cs:923-935 (CommonizeSignificands, which computes leftExponent - rightExponent), used by Add/Subtract (around line 1500) and Mod (line 1711).
Failure scenario
Confirmed with a scratch app:
vara=PreciseNumber.Parse("1e2000000000");varb=PreciseNumber.Parse("1e-2000000000");_=a+b;// ArgumentOutOfRangeException: exponent must be a non-negative value (Pow10(-294967296))
Multiply and Divide report the same out-of-range condition as OverflowException (fixed in #109), so callers that catch OverflowException around arithmetic miss this one.
Suggested fix
Compute the exponent difference as long (or checked).
When it cannot be represented, either throw OverflowException, consistent with Multiply/Divide, or, better for Add, short-circuit. When the difference exceeds the working precision, the smaller operand cannot affect the result's significant digits.
Add tests for Add, Subtract and Mod with exponents ±2e9.
Priority: Low. It needs exponents about 2e9 apart to trigger, and the outcome is an exception rather than a wrong result. The real defect is the exception type: ArgumentOutOfRangeException escapes where Multiply/Divide throw OverflowException.
Area / suggested assignee:PreciseNumber/PreciseNumber.csCommonizeSignificands (used by Add, Subtract and Mod). @matt-edmondson.
Notes: The short-circuit for Add/Subtract (the smaller operand is below working precision) is a correctness win on its own, not only an overflow guard.
Activity
matt-edmondson commentedon Sep 28, 2026
Triage
ArgumentOutOfRangeExceptionescapes whereMultiply/DividethrowOverflowException.PreciseNumber/PreciseNumber.csCommonizeSignificands(used byAdd,SubtractandMod). @matt-edmondson.Multiply/Divide) and Pow(-1, n) throws OverflowException for any integer |n| ≥ 2^31, although the result is ±1 #128 (Pow(-1, n)overflow).Add/Subtract(the smaller operand is below working precision) is a correctness win on its own, not only an overflow guard.Generated by Claude Code