Skip to content

Commit

Permalink
Avoid rare NaN in calc_check_iterations (#210)
Browse files Browse the repository at this point in the history
* avoid NaN in calc_check_iterations

* add test for #209

* fix test

* more fix
  • Loading branch information
IanButterworth authored Jun 1, 2021
1 parent c2280b6 commit 5f11b32
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/ProgressMeter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ running_ijulia_kernel() = isdefined(Main, :IJulia) && Main.IJulia.inited
clear_ijulia() = (IJULIABEHAVIOR[] != IJuliaAppend) && running_ijulia_kernel()

function calc_check_iterations(p, t)
if t == p.tlast
# avoid a NaN which could happen because the print time compensation makes an assumption about how long printing
# takes, therefore it's possible (but rare) for `t == p.tlast`
return p.check_iterations
end
# Adjust the number of iterations that skips time check based on how accurate the last number was
iterations_per_dt = (p.check_iterations / (t - p.tlast)) * p.dt
return round(Int, clamp(iterations_per_dt, 1, p.check_iterations * 10))
Expand Down
8 changes: 8 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@ if get(ENV, "GITHUB_ACTIONS", "false") != "true" # CI environment is too unrelia
@time noprog_perf(10^7)
@test @elapsed(prog_perf(10^7)) < 9*@elapsed(noprog_perf(10^7))
end

# Avoid a NaN due to the estimated print time compensation
# https://github.com/timholy/ProgressMeter.jl/issues/209
prog = Progress(10)
prog.check_iterations = 999
t = time()
prog.tlast = t
@test ProgressMeter.calc_check_iterations(prog, t) == 999

0 comments on commit 5f11b32

Please sign in to comment.