diff options
| author | FintasticMan <finlay.neon.kid@gmail.com> | 2024-12-10 00:11:13 +0100 |
|---|---|---|
| committer | mark9064 <30447455+mark9064@users.noreply.github.com> | 2025-12-21 20:18:04 +0000 |
| commit | 52baa265feecbb83dcf0419cfaddcd75dde9ccba (patch) | |
| tree | 02be0db6a29f91010ea4838ab50d925e2d95417c /src/utility | |
| parent | 66b5977f39bf22e0641be43766439a3ed025d604 (diff) | |
weather: Fix incorrect rounding for negative temperatures
Diffstat (limited to 'src/utility')
| -rw-r--r-- | src/utility/Math.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/utility/Math.h b/src/utility/Math.h index e8d190c7..314e4e37 100644 --- a/src/utility/Math.h +++ b/src/utility/Math.h @@ -1,10 +1,33 @@ #pragma once #include <cstdint> +#include <concepts> namespace Pinetime { namespace Utility { // returns the arcsin of `arg`. asin(-32767) = -90, asin(32767) = 90 int16_t Asin(int16_t arg); + + // Round half away from zero integer division + // If T signed, divisor cannot be std::numeric_limits<T>::min() + // Adapted from https://github.com/lucianpls/rounding_integer_division + // Under the MIT license + template <std::integral T> + constexpr T RoundedDiv(T dividend, T divisor) { + bool neg = divisor < 0; + if (neg) { + // overflows if divisor is minimum value for T + divisor = -divisor; + } + + T m = dividend % divisor; + T h = divisor / 2 + divisor % 2; + T res = (dividend / divisor) + (!(dividend < 0) & (m >= h)) - ((dividend < 0) & ((m + h) <= 0)); + + if (neg) { + res = -res; + } + return res; + } } } |
