Skip to content

Commit

Permalink
Use === and !== for comparisons with nothing (JuliaMath#243)
Browse files Browse the repository at this point in the history
* Use `===` and `!==` for comparisons with `nothing`

* Fix two additional comparisons
  • Loading branch information
devmotion authored Sep 14, 2021
1 parent a77396c commit 0b3e8fe
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
13 changes: 6 additions & 7 deletions src/find_zero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Tracks(s::AbstractUnivariateZeroState{T,S}) where {T, S} = Tracks(T,S)
Tracks(verbose, tracks, state) = (verbose && isa(tracks, NullTracks)) ? Tracks(state) : tracks

function log_step(l::Tracks, M::Any, state, init=nothing)
if init != nothing
if init !== nothing
x₀, fx₀ = state.xn0, state.fxn0
push!(l.xs, x₀)
push!(l.fs, fx₀)
Expand All @@ -194,7 +194,7 @@ function log_step(l::Tracks, M::Any, state, init=nothing)
push!(l.xs, x₁)
push!(l.fs, fx₁)

init == nothing && log_steps(l, 1)
init === nothing && log_steps(l, 1)
nothing
end

Expand All @@ -217,8 +217,7 @@ function show_tracks(io::IO, s::Tracks, M::AbstractUnivariateZeroMethod)
end

function show_trace(io::IO, method, N, state, tracks)

if state == nothing
if state === nothing
print(io, "Algorithm has not been run")
return nothing
end
Expand All @@ -228,7 +227,7 @@ function show_trace(io::IO, method, N, state, tracks)
println(io, "Results of univariate zero finding:\n")
if converged
println(io, "* Converged to: $(state.xn1)")
if N == nothing || isa(method, AbstractBracketing)
if N === nothing || method isa AbstractBracketing
println(io, "* Algorithm: $(method)")
else
println(io, "* Algorithm: $(method), with possible bracketing with $N")
Expand Down Expand Up @@ -783,7 +782,7 @@ end
# This should be deprecated
function Base.iterate(P::ZeroProblemIterator, st=nothing)
## st = (val, (state, ctr, val))
if st == nothing
if st === nothing
state = P.state
ctr = 1
else
Expand Down Expand Up @@ -902,7 +901,7 @@ julia> function order0(f, x)
fx = ZeroProblem(f, x)
p = init(fx, Roots.Secant())
xᵢ,st = ϕ = iterate(p)
while ϕ != nothing
while ϕ !== nothing
xᵢ, st = ϕ
state, ctr = st
fᵢ₋₁, fᵢ = state.fxn0, state.fxn1
Expand Down
8 changes: 4 additions & 4 deletions src/simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ function bisection(f, a::Number, b::Number; xatol=nothing, xrtol=nothing)
T = eltype(x1)


atol = xatol == nothing ? zero(T) : abs(xatol)
rtol = xrtol == nothing ? zero(one(T)) : abs(xrtol)
atol = xatol === nothing ? zero(T) : abs(xatol)
rtol = xrtol === nothing ? zero(one(T)) : abs(xrtol)
CT = iszero(atol) && iszero(rtol) ? Val(:exact) : Val(:inexact)

x1, x2 = float(x1), float(x2)
Expand Down Expand Up @@ -313,8 +313,8 @@ function newton(f, x0; xatol=nothing, xrtol=nothing, maxevals = 100)

x = float(x0)
T = typeof(x)
atol = xatol != nothing ? xatol : oneunit(T) * (eps(one(T)))^(4/5)
rtol = xrtol != nothing ? xrtol : eps(one(T))^(4/5)
atol = xatol !== nothing ? xatol : oneunit(T) * (eps(one(T)))^(4/5)
rtol = xrtol !== nothing ? xrtol : eps(one(T))^(4/5)


xo = Inf
Expand Down
2 changes: 1 addition & 1 deletion test/test_derivative_free_interactive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ function read_in(fname)
E["residuals"] = Dict()
for (k,v) in D["residuals"]
for vi in v
vi[vi .== nothing] .= NaN
vi[vi .=== nothing] .= NaN
end
E["residuals"][k] = vvta1(v, Float64)
end
Expand Down
4 changes: 2 additions & 2 deletions test/test_find_zero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ end
options = Roots.init_options(M, state, xatol=1/2)
ZPI = init(M,G1,state,options)
ϕ = iterate(ZPI)
while ϕ != nothing
while ϕ !== nothing
val, st = ϕ
state, ctr = st
ϕ = iterate(ZPI, st)
Expand Down Expand Up @@ -490,7 +490,7 @@ end
ZPI = init(ZeroProblem(F, x0), M; kwargs...)
x = NaN * float(x0)
ϕ = iterate(ZPI)
while ϕ != nothing
while ϕ !== nothing
x, st = ϕ
F.cnt.contents >= maxfnevals && return NaN*float(x0)
ϕ = iterate(ZPI, st)
Expand Down

0 comments on commit 0b3e8fe

Please sign in to comment.