Does mod() work? Or am I high?

That seems dicey to me. If by “approximately zero” you mean abs(mod(t, dt)) < ε for some ε, then if you make ε too small you risk outputting very infrequently (because your code only gets called at a discrete set of times t, which may not often be close to multiples of dt), and if you make ε too large then you risk outputting multiple times in the same dt time interval.

Why not do something like:

t = time()
if t >= last_output_time + dt
    ...output...
    last_output_time = t
end

That way it will call the output routine as close as possible to intervals of dt, limited by the frequency of the times t where you perform this check.

3 Likes