Skip to content

Commit 4a13235

Browse files
committed
utility: Add function for calculating a rounded division
Make weather service use it.
1 parent 5705fad commit 4a13235

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/components/ble/SimpleWeatherService.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#undef min
3333

3434
#include "components/datetime/DateTimeController.h"
35+
#include "utility/Math.h"
3536

3637
int WeatherCallback(uint16_t connHandle, uint16_t attrHandle, struct ble_gatt_access_ctxt* ctxt, void* arg);
3738

@@ -75,13 +76,11 @@ namespace Pinetime {
7576
}
7677

7778
[[nodiscard]] int16_t Celsius() const {
78-
int16_t temp = PreciseCelsius();
79-
return (temp + (temp >= 0 ? 50 : -50)) / 100;
79+
return Utility::RoundedDiv(PreciseCelsius(), 100u);
8080
}
8181

8282
[[nodiscard]] int16_t Fahrenheit() const {
83-
int16_t temp = PreciseFahrenheit();
84-
return (temp + (temp >= 0 ? 50 : -50)) / 100;
83+
return Utility::RoundedDiv(PreciseFahrenheit(), 100u);
8584
}
8685

8786
bool operator==(const Temperature& other) const {

src/utility/Math.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
#pragma once
22

33
#include <cstdint>
4+
#include <concepts>
45

56
namespace Pinetime {
67
namespace Utility {
78
// returns the arcsin of `arg`. asin(-32767) = -90, asin(32767) = 90
89
int16_t Asin(int16_t arg);
10+
11+
static constexpr auto RoundedDiv(std::integral auto dividend, std::unsigned_integral auto divisor) -> decltype(dividend / divisor) {
12+
return (dividend + (dividend >= 0 ? divisor : -divisor) / 2) / divisor;
13+
}
914
}
1015
}

0 commit comments

Comments
 (0)