diff options
Diffstat (limited to 'include/mbgl/math/wrap.hpp')
-rw-r--r-- | include/mbgl/math/wrap.hpp | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/include/mbgl/math/wrap.hpp b/include/mbgl/math/wrap.hpp index 16dc598c22..3076225384 100644 --- a/include/mbgl/math/wrap.hpp +++ b/include/mbgl/math/wrap.hpp @@ -9,8 +9,15 @@ namespace util { // arithmetic. template <typename T> T wrap(T value, T min, T max) { - T d = max - min; - return std::fmod((std::fmod((value - min), d) + d), d) + min; + if (value >= min && value < max) { + return value; + } else if (value == max) { + return min; + } + + const T delta = max - min; + const T wrapped = min + std::fmod(value - min, delta); + return value < min ? wrapped + delta : wrapped; } } // namespace util |