I want to do move through a loop and save a value at particular time intervals. I was thinking of something with this form:
t <- 0
t_end <- 10
time_step <- 0.01
record_interval <- 1
while (t <= t_end){
if (t %% record_interval == 0) print(t)
t <- t + time_step
}
But of course doing it this way fails due to machine precision issues.
I can do:
if (isTRUE(all.equal(t %% record_interval,0, tolerance = time_step*0.1))) { ...
But even this fails because sometimes it gives a modulo of record_interval rather than 0.
So, I can run my code with an or statement, using the above and also checking if the modulo is equal to record_interval, but that's incredibly ugly. I am sure there is an obvious, easy way to solve this problem but I am at a loss. I don't want to introduce an unnecessary integer counter (the all.equal part works, it's just not pretty, so that seems less bad), and I want to be able to change time_step and record_interval depending upon needs.
t_rolling <- t_rolling + time_step; if (t_rolling > record_interval) { print(t); t_rolling <- 0 }