From c868a3190d780701e319ff5a155aa0fe4de60d05 Mon Sep 17 00:00:00 2001 From: FintasticMan Date: Tue, 10 Dec 2024 00:11:13 +0100 Subject: [PATCH 1/2] weather: Fix incorrect rounding for negative temperatures --- src/components/ble/SimpleWeatherService.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/components/ble/SimpleWeatherService.h b/src/components/ble/SimpleWeatherService.h index 0f8c181bd3..501d434dcc 100644 --- a/src/components/ble/SimpleWeatherService.h +++ b/src/components/ble/SimpleWeatherService.h @@ -75,11 +75,13 @@ namespace Pinetime { } [[nodiscard]] int16_t Celsius() const { - return (PreciseCelsius() + 50) / 100; + int16_t temp = PreciseCelsius(); + return (temp + (temp >= 0 ? 50 : -50)) / 100; } [[nodiscard]] int16_t Fahrenheit() const { - return (PreciseFahrenheit() + 50) / 100; + int16_t temp = PreciseFahrenheit(); + return (temp + (temp >= 0 ? 50 : -50)) / 100; } bool operator==(const Temperature& other) const { From 635aa0c817d93855f5c3200f53e40052074782cc Mon Sep 17 00:00:00 2001 From: FintasticMan Date: Tue, 10 Dec 2024 03:12:36 +0100 Subject: [PATCH 2/2] utility: Add function for calculating a rounded division Make weather service use it. --- src/components/ble/SimpleWeatherService.h | 7 +++---- src/utility/Math.h | 5 +++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/components/ble/SimpleWeatherService.h b/src/components/ble/SimpleWeatherService.h index 501d434dcc..60aea5fd2b 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,13 +76,11 @@ namespace Pinetime { } [[nodiscard]] int16_t Celsius() const { - int16_t temp = PreciseCelsius(); - return (temp + (temp >= 0 ? 50 : -50)) / 100; + return Utility::RoundedDiv(PreciseCelsius(), 100); } [[nodiscard]] int16_t Fahrenheit() const { - int16_t temp = PreciseFahrenheit(); - return (temp + (temp >= 0 ? 50 : -50)) / 100; + return Utility::RoundedDiv(PreciseFahrenheit(), 100); } bool operator==(const Temperature& other) const { diff --git a/src/utility/Math.h b/src/utility/Math.h index e8d190c72d..3c4dc7da3d 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::integral auto divisor) -> decltype(dividend / divisor) { + return (dividend + (dividend >= 0 ? divisor : -divisor) / 2) / divisor; + } } }