Skip to content

BUG: Make _max_abs_diff NaN-safe to prevent false convergence#96

Merged
oyamad merged 1 commit into
mainfrom
fix-nan-convergence
Jul 4, 2026
Merged

BUG: Make _max_abs_diff NaN-safe to prevent false convergence#96
oyamad merged 1 commit into
mainfrom
fix-nan-convergence

Conversation

@oyamad

@oyamad oyamad commented Jul 4, 2026

Copy link
Copy Markdown
Member

Summary

Fix a regression introduced in #95: _max_abs_diff accumulated via d > err && (err = d), and since NaN > err is false, an iteration that diverged to non-finite coefficients (Inf - Inf = NaN) produced err = 0.0 and was reported as converged = true. The previous criterion maximum(abs, C - C_old) propagated NaN, so divergence correctly failed with a max_iter warning.

The accumulation now uses max(err, abs(x[i] - y[i])), which propagates NaN exactly like maximum, restoring the pre-#95 semantics (still allocation-free).

Discovered while investigating a VFI divergence: with the buggy criterion the solver returned converged = true on coefficients that were all NaN.

Verification

  • New unit tests for _max_abs_diff (NaN/Inf propagation) and an end-to-end test that operator_iteration! does not declare convergence for an operator diverging to NaN.
  • Full test suite passes.

🤖 Generated with Claude Code (Claude Fable 5)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a regression in the solver’s convergence criterion by making _max_abs_diff NaN-safe again, preventing false convergence when coefficient updates produce non-finite values (e.g., Inf - Inf = NaN). This restores the pre-#95 behavior where divergence propagates through the error metric and cannot be misreported as converged.

Changes:

  • Update _max_abs_diff to accumulate with max(err, abs(x[i] - y[i])), ensuring NaN propagates like the prior maximum(abs, x - y) approach.
  • Add unit tests covering NaN/Inf propagation behavior in _max_abs_diff.
  • Add an end-to-end test ensuring operator_iteration! does not declare convergence for an operator that drives coefficients to NaN.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/cdp.jl Updates _max_abs_diff to propagate NaN during convergence checking, preventing false convergence.
test/test_workspace.jl Adds tests for _max_abs_diff non-finite propagation and an end-to-end divergence-to-NaN convergence check.

@oyamad oyamad merged commit 6ec9917 into main Jul 4, 2026
6 checks passed
@oyamad oyamad deleted the fix-nan-convergence branch July 4, 2026 14:17
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.

2 participants