Skip to content

Commit

Permalink
When using floating-point timestamps, use a double instead of a float.
Browse files Browse the repository at this point in the history
The vast majority of timestamps are held as a pair of 64-bit integers
(numerator and denominator). For the few cases where floating point
math is used, use a double for the numerator instead of a float (the
denominator is always an int64_t.) This will provide more precision.
  • Loading branch information
linuxdude42 committed Mar 7, 2021
1 parent fecb776 commit 49ea6b5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions mythtv/libs/libmythbase/housekeeper.cpp
Expand Up @@ -386,23 +386,23 @@ bool PeriodicHouseKeeperTask::DoCheckRun(const QDateTime& now)

// calculate probability that task should not have yet run
// it's backwards, but it makes the math simplier
float prob = 1.0F - (duration_cast<floatsecs>(elapsed - m_windowElapsed.first) /
double prob = 1.0 - (duration_cast<floatsecs>(elapsed - m_windowElapsed.first) /
duration_cast<floatsecs>(m_windowElapsed.second - m_windowElapsed.first));
if (m_currentProb < prob)
// more bad stuff
return false;

// calculate current probability to achieve overall probability
// this should be nearly one
float prob2 = prob/m_currentProb;
double prob2 = prob/m_currentProb;
// so rand() should have to return nearly RAND_MAX to get a positive
// remember, this is computing the probability that up to this point, one
// of these tests has returned positive, so each individual test has
// a necessarily low probability
//
// Pseudo-random is good enough. Don't need a true random.
// NOLINTNEXTLINE(cert-msc30-c,cert-msc50-cpp)
bool res = (rand() > (int)(prob2 * static_cast<float>(RAND_MAX)));
bool res = (rand() > (int)(prob2 * static_cast<double>(RAND_MAX)));
m_currentProb = prob;
// if (res)
// LOG(VB_GENERAL, LOG_DEBUG, QString("%1 will run: this=%2; total=%3")
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythbase/housekeeper.h
Expand Up @@ -102,7 +102,7 @@ class MBASE_PUBLIC PeriodicHouseKeeperTask : public HouseKeeperTask
std::chrono::seconds m_retry;
QPair<float,float> m_windowPercent;
QPair<std::chrono::seconds,std::chrono::seconds> m_windowElapsed;
float m_currentProb;
double m_currentProb;
};

class MBASE_PUBLIC DailyHouseKeeperTask : public PeriodicHouseKeeperTask
Expand Down
10 changes: 5 additions & 5 deletions mythtv/libs/libmythbase/mythchrono.h
Expand Up @@ -36,9 +36,9 @@ namespace std::chrono // NOLINT(cert-dcl58-cpp)

// There are a number of places that hold/manipulate time in the form
// of a floating point number. Create unique types for these.
using floatsecs = std::chrono::duration<float>;
using floatmsecs = std::chrono::duration<float, std::milli>;
using floatusecs = std::chrono::duration<float, std::micro>;
using floatsecs = std::chrono::duration<double>;
using floatmsecs = std::chrono::duration<double, std::milli>;
using floatusecs = std::chrono::duration<double, std::micro>;

// There are a handful of places that hold a time value in units of
// AV_TIME_BASE. Create a unique type for this.
Expand Down Expand Up @@ -196,13 +196,13 @@ T nowAsDuration (bool adjustForTZ = false)

/// \brief Multiply a duration by a float, returning a duration.
template <typename T>
static constexpr T chronomult(T duration, float f)
static constexpr T chronomult(T duration, double f)
{
return T(std::llround(duration.count() * f));
}
/// \brief Divide a duration by a float, returning a duration.
template <typename T>
static constexpr T chronodivide(T duration, float f)
static constexpr T chronodivide(T duration, double f)
{
return T(std::llround(duration.count() / f));
}
Expand Down

0 comments on commit 49ea6b5

Please sign in to comment.