diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index 4f7d99b1d8..280f9ea006 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -100,24 +100,20 @@ static inline QDate fixedDate(int y, int m, int d) } /* - Until C++11, rounding direction is implementation-defined. + Division, rounding down (rather than towards zero). - For negative operands, implementations may chose to round down instead of - towards zero (truncation). We only actually care about the case a < 0, as all - uses of floordiv have b > 0. In this case, if rounding is down we have a % b - >= 0 and simple division works fine; but a % b = a - (a / b) * b always, so - rounding towards zero gives a % b <= 0; when < 0, we need to adjust. - - Once we assume C++11, we can safely test a < 0 instead of a % b < 0. + From C++11 onwards, integer division is defined to round towards zero, so we + can rely on that when implementing this. This is only used with denominator b + > 0, so we only have to treat negative numerator, a, specially. */ static inline qint64 floordiv(qint64 a, int b) { - return (a - (a % b < 0 ? b - 1 : 0)) / b; + return (a - (a < 0 ? b - 1 : 0)) / b; } static inline int floordiv(int a, int b) { - return (a - (a % b < 0 ? b - 1 : 0)) / b; + return (a - (a < 0 ? b - 1 : 0)) / b; } static inline qint64 julianDayFromDate(int year, int month, int day)