From 29908f2d8b2f1b78a054647a780177d69cc1155f Mon Sep 17 00:00:00 2001 From: itsmattkc <34096995+itsmattkc@users.noreply.github.com> Date: Sun, 29 May 2022 11:13:05 -0700 Subject: [PATCH] math: revised timestamp calculations around epsilon --- app/common/timecodefunctions.cpp | 14 ++++++++++++-- app/common/timerange.cpp | 2 +- tests/general/timerange-tests.cpp | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/app/common/timecodefunctions.cpp b/app/common/timecodefunctions.cpp index a0f97efb1..cbd9d4074 100644 --- a/app/common/timecodefunctions.cpp +++ b/app/common/timecodefunctions.cpp @@ -304,14 +304,24 @@ int64_t Timecode::time_to_timestamp(const double &time, const rational &timebase return 0; } + const double eps = 0.000000000001; + switch (floor) { case kRound: default: return qRound64(d); case kFloor: - return qFloor(d); + if (d > qCeil(d)-eps) { + return qCeil(d); + } else { + return qFloor(d); + } case kCeil: - return qCeil(d); + if (d < qFloor(d)+eps) { + return qFloor(d); + } else { + return qCeil(d); + } } } diff --git a/app/common/timerange.cpp b/app/common/timerange.cpp index 6c70ec748..7326794ef 100644 --- a/app/common/timerange.cpp +++ b/app/common/timerange.cpp @@ -368,7 +368,7 @@ void TimeRangeListFrameIterator::UpdateIndexIfNecessary() range_index_++; if (range_index_ < list_.size()) { - current_ = Timecode::snap_time_to_timebase(list_.at(range_index_).in(), timebase_, Timecode::kRound); + current_ = Timecode::snap_time_to_timebase(list_.at(range_index_).in(), timebase_, Timecode::kCeil); } } } diff --git a/tests/general/timerange-tests.cpp b/tests/general/timerange-tests.cpp index fe4d117fa..6ddfe14e1 100644 --- a/tests/general/timerange-tests.cpp +++ b/tests/general/timerange-tests.cpp @@ -105,4 +105,24 @@ OLIVE_ADD_TEST(TimeRangeListFrameIteratorSize) OLIVE_TEST_END; } +OLIVE_ADD_TEST(TimeRangeListFrameIteratorSize2) +{ + const rational timebase(1001, 30000); + + TimeRangeList ranges; + + ranges.insert(TimeRange(rational(247247, 30000), rational(31031, 3750))); // 1 + + TimeRange tr(rational(247247, 30000), rational(31031, 3750)); + + TimeRangeListFrameIterator iterator(ranges, timebase); + + QVector vec = iterator.ToVector(); + + OLIVE_ASSERT_EQUAL(vec.size(), 1); + OLIVE_ASSERT_EQUAL(iterator.size(), vec.size()); + + OLIVE_TEST_END; +} + }