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

Duration: Use double instead of quint64 to store nanoseconds internally #4147

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
42 changes: 21 additions & 21 deletions src/util/duration.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,43 @@ class DurationBase {

// Returns the duration as an integer number of seconds (rounded-down).
constexpr qint64 toIntegerSeconds() const {
return m_durationNanos / kNanosPerSecond;
return static_cast<qint64>(toDoubleSeconds());
}

// Returns the duration as a floating point number of seconds.
constexpr double toDoubleSeconds() const {
return static_cast<double>(m_durationNanos) / kNanosPerSecond;
return m_durationNanos / kNanosPerSecond;
}

// Returns the duration as an integer number of milliseconds (rounded-down).
constexpr qint64 toIntegerMillis() const {
return m_durationNanos / kNanosPerMilli;
return static_cast<qint64>(toDoubleMillis());
}

// Returns the duration as a floating point number of milliseconds.
constexpr double toDoubleMillis() const {
return static_cast<double>(m_durationNanos) / kNanosPerMilli;
return m_durationNanos / kNanosPerMilli;
}

// Returns the duration as an integer number of microseconds (rounded-down).
constexpr qint64 toIntegerMicros() const {
return m_durationNanos / kNanosPerMicro;
return static_cast<qint64>(toDoubleMicros());
}

// Returns the duration as a floating point number of microseconds.
constexpr double toDoubleMicros() const {
return static_cast<double>(m_durationNanos) / kNanosPerMicro;
return m_durationNanos / kNanosPerMicro;
}

// Returns the duration as an integer number of nanoseconds. The duration is
// represented internally as nanoseconds so no rounding occurs.
constexpr qint64 toIntegerNanos() const {
return m_durationNanos;
return static_cast<qint64>(toDoubleNanos());
}

// Returns the duration as an integer number of nanoseconds.
constexpr double toDoubleNanos() const {
return static_cast<double>(m_durationNanos);
return m_durationNanos;
}

enum class Precision {
Expand All @@ -84,22 +84,22 @@ class DurationBase {
double dSeconds,
Precision precision = Precision::SECONDS);

static constexpr qint64 kMillisPerSecond = 1000;
static constexpr qint64 kMicrosPerSecond = kMillisPerSecond * 1000;
static constexpr qint64 kNanosPerSecond = kMicrosPerSecond * 1000;
static constexpr qint64 kNanosPerMilli = kNanosPerSecond / 1000;
static constexpr qint64 kNanosPerMicro = kNanosPerMilli / 1000;
static constexpr double kMillisPerSecond = 1000;
static constexpr double kMicrosPerSecond = kMillisPerSecond * 1000;
static constexpr double kNanosPerSecond = kMicrosPerSecond * 1000;
static constexpr double kNanosPerMilli = kNanosPerSecond / 1000;
static constexpr double kNanosPerMicro = kNanosPerMilli / 1000;
static const QString kInvalidDurationString;
static QChar kKiloGroupSeparator;
static QChar kHectoGroupSeparator;
static QChar kDecimalSeparator;

protected:
explicit constexpr DurationBase(qint64 durationNanos)
: m_durationNanos(durationNanos) {
explicit constexpr DurationBase(double durationNanos)
: m_durationNanos(durationNanos) {
uklotzde marked this conversation as resolved.
Show resolved Hide resolved
}

qint64 m_durationNanos;
double m_durationNanos;
};

class DurationDebug : public DurationBase {
Expand Down Expand Up @@ -136,21 +136,21 @@ class Duration : public DurationBase {
// Returns a Duration object representing a duration of 'seconds'.
template<typename T>
static constexpr Duration fromSeconds(T seconds) {
return Duration(static_cast<qint64>(seconds * kNanosPerSecond));
return Duration(static_cast<double>(seconds * kNanosPerSecond));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seconds should be casted to a double first.
Do we still need a template? I think If we make the seconds a double we can even remove the cast.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kNanosPerSecond should be also a double to avoid loss of precision warnings.

Copy link
Member Author

@Holzhaus Holzhaus Jul 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made kNanosPerSecond a double. I'll check if removing the template breaks anything.

}

// Returns a Duration object representing a duration of 'millis'.
static constexpr Duration fromMillis(qint64 millis) {
static constexpr Duration fromMillis(double millis) {
return Duration(millis * kNanosPerMilli);
}

// Returns a Duration object representing a duration of 'micros'.
static constexpr Duration fromMicros(qint64 micros) {
static constexpr Duration fromMicros(double micros) {
return Duration(micros * kNanosPerMicro);
}

// Returns a Duration object representing a duration of 'nanos'.
static constexpr Duration fromNanos(qint64 nanos) {
static constexpr Duration fromNanos(double nanos) {
return Duration(nanos);
}

Expand Down Expand Up @@ -264,7 +264,7 @@ class Duration : public DurationBase {
}

private:
explicit constexpr Duration(qint64 durationNanos)
explicit constexpr Duration(double durationNanos)
: DurationBase(durationNanos) {
}
};
Expand Down