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

fix frequency calculation
pull/34116/head^2
Dean Lee 9 months ago committed by GitHub
parent adb9560870
commit 8e14e400ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 10
      tools/cabana/streams/abstractstream.cc

@ -1,5 +1,6 @@
#include "tools/cabana/streams/abstractstream.h"
#include <limits>
#include <utility>
#include <QApplication>
@ -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

Loading…
Cancel
Save