From 1af74f73985f2a9bbcbf63953ecaa6d89bb7edea Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Sat, 13 Feb 2021 21:57:00 -0700 Subject: [PATCH] libtemporal: correct the definition of std::numeric_limits::max(). Comment in code contains details. --- libs/temporal/temporal/beats.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/libs/temporal/temporal/beats.h b/libs/temporal/temporal/beats.h index c090c77de5..f031c36711 100644 --- a/libs/temporal/temporal/beats.h +++ b/libs/temporal/temporal/beats.h @@ -324,12 +324,22 @@ namespace std { return Temporal::Beats(std::numeric_limits::min(), std::numeric_limits::min()); } - /* We don't define min() since this has different behaviour for integral and floating point types, - but Beats is used as both. Better to avoid providing a min at all - than a confusing one. */ + /* We don't define min() since this has different behaviour for + integral and floating point types, but Beats is used as both + an integral and "fractional" value, so the semantics of + min() would be unclear. + + Better to avoid providing a min at all than a confusing one. + */ + + /* We must make the number of beats be 1 less than INT32_MAX, + * because otherwise adding the PPQN-1 ticks would cause + * overflow (the value would be INT32_MAX+((PPQN-1)/PPQN) which + * exceeds INT32_MAX. + */ static Temporal::Beats max() { - return Temporal::Beats(std::numeric_limits::max(), Temporal::Beats::PPQN-1); + return Temporal::Beats(std::numeric_limits::max() - 1, Temporal::Beats::PPQN - 1); } };