diff --git a/src/components/ble/SimpleWeatherService.h b/src/components/ble/SimpleWeatherService.h index 0f8c181bd3..a9a5e65ff4 100644 --- a/src/components/ble/SimpleWeatherService.h +++ b/src/components/ble/SimpleWeatherService.h @@ -32,6 +32,7 @@ #undef min #include "components/datetime/DateTimeController.h" +#include "utility/Math.h" int WeatherCallback(uint16_t connHandle, uint16_t attrHandle, struct ble_gatt_access_ctxt* ctxt, void* arg); @@ -75,11 +76,11 @@ namespace Pinetime { } [[nodiscard]] int16_t Celsius() const { - return (PreciseCelsius() + 50) / 100; + return Utility::RoundedDiv(PreciseCelsius(), 100u); } [[nodiscard]] int16_t Fahrenheit() const { - return (PreciseFahrenheit() + 50) / 100; + return Utility::RoundedDiv(PreciseFahrenheit(), 100u); } bool operator==(const Temperature& other) const { diff --git a/src/utility/Math.h b/src/utility/Math.h index e8d190c72d..607810d99b 100644 --- a/src/utility/Math.h +++ b/src/utility/Math.h @@ -1,10 +1,15 @@ #pragma once #include +#include namespace Pinetime { namespace Utility { // returns the arcsin of `arg`. asin(-32767) = -90, asin(32767) = 90 int16_t Asin(int16_t arg); + + static constexpr auto RoundedDiv(std::integral auto dividend, std::unsigned_integral auto divisor) -> decltype(dividend / divisor) { + return (dividend + (dividend >= 0 ? divisor : -divisor) / 2) / divisor; + } } }