Skip to content

fix: handle Infinity/NaN in Power evaluation (Von autonomous AI patch) - #2

Open
wowo515151 wants to merge 1 commit into
mainfrom
von-patch-1-infinity-nan-fix
Open

fix: handle Infinity/NaN in Power evaluation (Von autonomous AI patch)#2
wowo515151 wants to merge 1 commit into
mainfrom
von-patch-1-infinity-nan-fix

Conversation

@wowo515151

Copy link
Copy Markdown
Owner

⚠️ This PR was submitted by Von, an autonomous AI program.

Summary

Fixes a critical bug in NumericEvaluator.EvaluateRecursive() where the Power evaluation guard check for Infinity/NaN results was self-defeating — it detected the problematic condition but then called SafeToDecimal() on the offending value, which itself throws an exception.

The Bug

File: src/SymSolvers/NumericEvaluator.cs (line 308-310)

When Math.Pow() produced Infinity or NaN, the code did:

if (double.IsInfinity(resPow) || double.IsNaN(resPow))
{
    return SafeToDecimal(resPow);  // BUG: SafeToDecimal throws on Infinity/NaN!
}

SafeToDecimal() (in src/SymCore/NumericConvert.cs) explicitly throws:

  • InvalidOperationException for NaN values
  • OverflowException for Infinity values

The Fix

Replace the problematic return SafeToDecimal(resPow) with throw new OverflowException(...), which is properly caught by the existing catch block below that handles overflow gracefully:

if (double.IsInfinity(resPow) || double.IsNaN(resPow))
{
    throw new OverflowException("Power result is infinity or NaN and cannot be represented as decimal.");
}

The existing catch block then:

  1. Logs the overflow event
  2. Re-evaluates and checks if the result is outside decimal range
  3. Either re-throws with context or falls back to double precision

Impact

  • Fixes the TryEvaluate() error-handling contract — exceptions no longer leak unhandled
  • Prevents crashes in the solver pipeline during numeric evaluation of power expressions
  • Affects expressions like 0^(-2), (-1)^0.5, and very large base^exponent

Files Changed

  • src/SymSolvers/NumericEvaluator.cs — 1 line changed (guard throw instead of SafeToDecimal call)

Testing

GitHub Actions CI will run the full test suite including SymSolvers.Tests which covers NumericEvaluator.


Fixes #1

…ption

Previously, when Math.Pow() produced Infinity or NaN, the guard check
called SafeToDecimal() which itself throws. Now it throws OverflowException
directly, caught by the existing catch block for proper handling.

Fixes #1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] NumericEvaluator.EvaluateRecursive crashes on Power producing Infinity/NaN due to SafeToDecimal guard paradox

1 participant