Skip to content

Commit

Permalink
cabana: enhance freq calculation and add zero division protection (#3…
Browse files Browse the repository at this point in the history
…4121)

fix frequency calculation
  • Loading branch information
deanlee authored Nov 27, 2024
1 parent adb9560 commit 8e14e40
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions tools/cabana/streams/abstractstream.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "tools/cabana/streams/abstractstream.h"

#include <limits>
#include <utility>

#include <QApplication>
Expand Down Expand Up @@ -228,11 +229,10 @@ double calc_freq(const MessageId &msg_id, double current_sec) {
auto last = std::upper_bound(first, events.end(), current_mono_time, CompareCanEvent());

int count = std::distance(first, last);
if (count > 1) {
double duration = ((*std::prev(last))->mono_time - (*first)->mono_time) / 1e9;
return count / duration;
}
return 0;
if (count <= 1) return 0.0;

double duration = ((*std::prev(last))->mono_time - (*first)->mono_time) / 1e9;
return duration > std::numeric_limits<double>::epsilon() ? (count - 1) / duration : 0.0;
}

} // namespace
Expand Down

0 comments on commit 8e14e40

Please sign in to comment.