Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cabana: enhance freq calculation and add zero division protection #34121

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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