Skip to content

Commit

Permalink
Merge pull request #85 from JuliaReach/schillic/tol
Browse files Browse the repository at this point in the history
Make tolerance functions type stable
  • Loading branch information
schillic authored Dec 29, 2024
2 parents 49c1836 + 11c9713 commit 8aa142f
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions src/Comparison/tolerance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,22 @@ set_ztol(::Type{<:Rational}, ε::Rational) = _TOL_RAT.ztol = ε
set_atol(::Type{<:Rational}, ε::Rational) = _TOL_RAT.atol = ε

# global default tolerances for other numeric types
TOL_N = Dict{Type{<:Number},Tolerance}()
_rtol(N::Type{<:Number}) = get!(TOL_N, N, default_tolerance(N)).rtol
_ztol(N::Type{<:Number}) = get!(TOL_N, N, default_tolerance(N)).ztol
_atol(N::Type{<:Number}) = get!(TOL_N, N, default_tolerance(N)).atol

set_rtol(N::Type{NT}, ε::NT) where {NT<:Number} = begin
if N keys(TOL_N)
TOL_N[N] = default_tolerance(N)
end
TOL_N[N].rtol = ε
const TOL_N = Dict{Type{<:Number},Tolerance}()
_rtol(N::Type{<:Number})::N = get!(TOL_N, N, default_tolerance(N)).rtol
_ztol(N::Type{<:Number})::N = get!(TOL_N, N, default_tolerance(N)).ztol
_atol(N::Type{<:Number})::N = get!(TOL_N, N, default_tolerance(N)).atol

function set_rtol(N::Type{NT}, ε::NT) where {NT<:Number}
tol = get!(TOL_N, N, default_tolerance(N))
tol.rtol = ε
end

set_ztol(N::Type{NT}, ε::NT) where {NT<:Number} = begin
if N keys(TOL_N)
TOL_N[N] = default_tolerance(N)
end
TOL_N[N].ztol = ε
function set_ztol(N::Type{NT}, ε::NT) where {NT<:Number}
tol = get!(TOL_N, N, default_tolerance(N))
tol.ztol = ε
end

set_atol(N::Type{NT}, ε::NT) where {NT<:Number} = begin
if N keys(TOL_N)
TOL_N[N] = default_tolerance(N)
end
TOL_N[N].atol = ε
function set_atol(N::Type{NT}, ε::NT) where {NT<:Number}
tol = get!(TOL_N, N, default_tolerance(N))
tol.atol = ε
end

0 comments on commit 8aa142f

Please sign in to comment.