weather: Fix incorrect rounding for negative temperatures

This commit is contained in:
FintasticMan 2024-12-10 00:11:13 +01:00 committed by mark9064
parent 66b5977f39
commit 52baa265fe
2 changed files with 26 additions and 2 deletions

View File

@ -34,6 +34,7 @@
#include "components/datetime/DateTimeController.h"
#include <lvgl/lvgl.h>
#include "displayapp/InfiniTimeTheme.h"
#include "utility/Math.h"
int WeatherCallback(uint16_t connHandle, uint16_t attrHandle, struct ble_gatt_access_ctxt* ctxt, void* arg);
@ -77,11 +78,11 @@ namespace Pinetime {
}
[[nodiscard]] int16_t Celsius() const {
return (PreciseCelsius() + 50) / 100;
return Utility::RoundedDiv(PreciseCelsius(), static_cast<int16_t>(100));
}
[[nodiscard]] int16_t Fahrenheit() const {
return (PreciseFahrenheit() + 50) / 100;
return Utility::RoundedDiv(PreciseFahrenheit(), static_cast<int16_t>(100));
}
[[nodiscard]] lv_color_t Color() const {

View File

@ -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;
}
}
}