[core] util::wrap(): std::fmod is not lossless
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user