From e6653029d17d11cc02b619b920114d53886194d1 Mon Sep 17 00:00:00 2001 From: ParadaCarleton <71727937+ParadaCarleton@users.noreply.github.com> Date: Tue, 27 Oct 2026 08:18:46 -0700 Subject: [PATCH] BackTracking: skip a NaN interpolated step instead of taking it --- src/backtracking.jl | 4 ++-- test/root_finding_tests.jl | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/backtracking.jl b/src/backtracking.jl index fff1440..797f3b0 100644 --- a/src/backtracking.jl +++ b/src/backtracking.jl @@ -110,7 +110,7 @@ function CommonSolve.solve!( return solution_at!(ev, u, du, α₂, ReturnCode.Success) α_tmp = -(dϕ₀ * α₂^2) / (2 * (ϕx₁ - ϕ₀ - dϕ₀ * α₂)) α₁ = α₂ - α_tmp = min(α_tmp, α₂ * T(cache.alg.ρ_hi)) + α_tmp = isnan(α_tmp) ? α₂ * T(cache.alg.ρ_hi) : min(α_tmp, α₂ * T(cache.alg.ρ_hi)) α₂ = max(α_tmp, α₂ * T(cache.alg.ρ_lo)) ϕx₀, ϕx₁ = ϕx₁, ϕ(α₂) @@ -121,7 +121,7 @@ function CommonSolve.solve!( α_tmp = compute_alpha_backtracking(cache.alg.order, T, dϕ₀, ϕ₀, ϕx₀, ϕx₁, α₁, α₂) α₁ = α₂ - α_tmp = min(α_tmp, α₂ * T(cache.alg.ρ_hi)) + α_tmp = isnan(α_tmp) ? α₂ * T(cache.alg.ρ_hi) : min(α_tmp, α₂ * T(cache.alg.ρ_hi)) α₂ = max(α_tmp, α₂ * T(cache.alg.ρ_lo)) ϕx₀, ϕx₁ = ϕx₁, ϕ(α₂) diff --git a/test/root_finding_tests.jl b/test/root_finding_tests.jl index eb20230..f0a2417 100644 --- a/test/root_finding_tests.jl +++ b/test/root_finding_tests.jl @@ -210,6 +210,21 @@ end end end +@testset "BackTracking step stays finite once Float32 interpolation underflows" begin + using CommonSolve: init, solve! + using SciMLBase: NonlinearProblem + using ADTypes: AutoForwardDiff + import ForwardDiff + + # The residual jumps from 1 to 2 past u = 0, so no positive step passes Armijo. The shrinking + # trial steps underflow the cubic's divisor, and its NaN minimizer must not become the step. + r(u, p) = ifelse.(u .> 0, 2.0f0, 1.0f0) + u0 = Float32[0] + cache = init(NonlinearProblem(r, u0), BackTracking(; autodiff = AutoForwardDiff()), r(u0, nothing), u0) + sol = solve!(cache, u0, Float32[1]; ϕ0 = 0.5f0, dϕ0 = 0.0f0) + @test isfinite(sol.step_size) +end + @testset "Static Strong Wolfe residual mode" begin using CommonSolve: init, solve! using SciMLBase: NonlinearProblem, ReturnCode