Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

weather: Fix incorrect rounding for negative temperatures #2194

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/components/ble/SimpleWeatherService.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/utility/Math.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#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);

static constexpr auto RoundedDiv(std::integral auto dividend, std::unsigned_integral auto divisor) -> decltype(dividend / divisor) {
return (dividend + (dividend >= 0 ? divisor : -divisor) / 2) / divisor;
}
}
}
Loading