Skip to content

Commit 53670e8

Browse files
authored
Avoid type promotions due to hard-coded tolerances (#438)
* Avoid type promotions due to hard-coded tolerances ...in the `AlefieldPotraShi` solver. * Fix a unit handling problem
1 parent 5794200 commit 53670e8

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/Bracketing/alefeld_potra_shi.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ function update_state(
9292
options,
9393
l=NullTracks(),
9494
) where {T,S}
95-
μ, λ = 0.5, 0.7
9695
atol, rtol = options.xabstol, options.xreltol
96+
μ, λ = oftype(rtol, 0.5), oftype(rtol, 0.7)
9797
tols = (; λ=λ, atol=atol, rtol=rtol)
9898

9999
a::T, b::T, d::T, ee::T = o.xn0, o.xn1, o.d, o.ee
@@ -187,7 +187,7 @@ struct A2425{K} <: AbstractAlefeldPotraShi end
187187
function calculateΔ(::A2425{K}, F::Callable_Function, c₀::T, ps) where {K,T}
188188
a, b, d, ee = ps.a, ps.b, ps.d, ps.ee
189189
fa, fb, fd, fee = ps.fa, ps.fb, ps.fd, ps.fee
190-
tols ==0.7, atol=ps.atol, rtol=ps.rtol)
190+
tols ==oftype(ps.rtol, 0.7), atol=ps.atol, rtol=ps.rtol)
191191

192192
c = a
193193
for k in 1:K
@@ -236,7 +236,7 @@ fncalls_per_step(::A57{K}) where {K} = K - 1
236236
function calculateΔ(::A57{K}, F::Callable_Function, c₀::T, ps) where {K,T}
237237
a, b, d, ee = ps.a, ps.b, ps.d, ps.ee
238238
fa, fb, fd, fee = ps.fa, ps.fb, ps.fd, ps.fee
239-
tols ==0.7, atol=ps.atol, rtol=ps.rtol)
239+
tols ==oftype(ps.rtol, 0.7), atol=ps.atol, rtol=ps.rtol)
240240
c, fc = a, fa
241241

242242
for k in 1:K

src/convergence.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ function is_small_Δx(
205205
state::AbstractUnivariateZeroState,
206206
options,
207207
)
208-
δ = abs(state.xn1 - state.xn0)
208+
δ = _unitless(abs(state.xn1 - state.xn0))
209209
δₐ, δᵣ = options.xabstol, options.xreltol
210210
Δₓ = max(_unitless(δₐ), _unitless(abs(state.xn1)) * δᵣ)
211211
Δₓ = sqrt(sqrt(sqrt((abs(_unitless(Δₓ)))))) # faster than x^(1/8)

test/test_composable.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ using ForwardDiff
2424
@testset "find zero(s) with Unitful" begin
2525
s = u"s"
2626
m = u"m"
27-
g = 9.8 * m / s^2
27+
g = (9 + 8//10) * m / s^2
2828
v0 = 10m / s
2929
y0 = 16m
3030
y(t) = -g * t^2 + v0 * t + y0
3131

3232
for order in orders
3333
@test find_zero(y, 1.8s, order) 1.886053370668014s
34+
@test find_zero(y, 1.8f0s, order) isa typeof(1.88f0s)
3435
end
3536

3637
for M in [Roots.Bisection(), Roots.A42(), Roots.AlefeldPotraShi()]
3738
@test find_zero(y, (1.8s, 1.9s), M) 1.886053370668014s
39+
@test find_zero(y, (1.8f0s, 1.9f0s), M) isa typeof(1.88f0s)
3840
end
3941

4042
xrts = find_zeros(y, 0s, 10s)
@@ -44,6 +46,10 @@ using ForwardDiff
4446
# issue #434
4547
xzs1 = find_zeros(x -> cos(x / 1u"m"), -1.6u"m", 2u"m")
4648
@test length(xzs1) == 2 && maximum(xzs1) 1.5707963267948966 * u"m"
49+
50+
FX = ZeroProblem(y, (0f0s, 2f0s))
51+
prob = Roots.init(FX, Roots.AlefeldPotraShi())
52+
@test Roots.is_small_Δx(prob.M, prob.state, prob.options) isa Bool # does not throw
4753
end
4854

4955
# Polynomials

0 commit comments

Comments
 (0)