Skip to content

Commit 8f880ee

Browse files
committed
[math] percent_t unit can be float since C++20
1 parent 3811b71 commit 8f880ee

File tree

5 files changed

+23
-26
lines changed

5 files changed

+23
-26
lines changed

src/modm/math/units.hpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Niklas Hauser
2+
* Copyright (c) 2019, 2024, Niklas Hauser
33
*
44
* This file is part of the modm project.
55
*
@@ -36,7 +36,7 @@ namespace modm
3636
template<typename T> constexpr bitrate_t kbps(T value);
3737
template<typename T> constexpr bitrate_t Mbps(T value);
3838

39-
using percent_t = uint16_t;
39+
using percent_t = float;
4040
template<typename T> constexpr percent_t pct(T value);
4141
/// @}
4242

@@ -63,8 +63,7 @@ namespace modm
6363

6464
#else
6565

66-
#define MODM_LITERAL_DEFINITION(type, name, symbol) \
67-
namespace modm { \
66+
#define MODM_UNITS_LITERAL_DEFINITION(type, name, symbol) \
6867
using MODM_CONCAT(name, _t) = type; \
6968
template<typename T> constexpr MODM_CONCAT(name, _t) symbol(T value) { return value; } \
7069
template<typename T> constexpr MODM_CONCAT(name, _t) MODM_CONCAT(k, symbol)(T value) { return value * 1'000ul; } \
@@ -75,16 +74,19 @@ namespace literals { \
7574
constexpr MODM_CONCAT(name, _t) MODM_CONCAT(operator""_k, symbol)(long double value) { return MODM_CONCAT(k, symbol)(value); } \
7675
constexpr MODM_CONCAT(name, _t) MODM_CONCAT(operator""_M, symbol)(unsigned long long int value) { return MODM_CONCAT(M, symbol)(value); } \
7776
constexpr MODM_CONCAT(name, _t) MODM_CONCAT(operator""_M, symbol)(long double value) { return MODM_CONCAT(M, symbol)(value); } \
78-
}}
77+
}
78+
79+
namespace modm
80+
{
7981

80-
MODM_LITERAL_DEFINITION(uint32_t, frequency, Hz)
81-
MODM_LITERAL_DEFINITION(uint32_t, baudrate, Bd)
82-
MODM_LITERAL_DEFINITION(uint32_t, bitrate, bps)
82+
MODM_UNITS_LITERAL_DEFINITION(uint32_t, frequency, Hz)
83+
MODM_UNITS_LITERAL_DEFINITION(uint32_t, baudrate, Bd)
84+
MODM_UNITS_LITERAL_DEFINITION(uint32_t, bitrate, bps)
8385

84-
namespace modm {
85-
enum class percent_t : uint16_t {};
86-
template<typename T> constexpr percent_t pct(T value) { return percent_t(uint16_t(value * 600ul)); }
87-
constexpr float pct2f(percent_t value) { return uint16_t(value) / 60'000.f; }
86+
using percent_t = float;
87+
template<typename T> constexpr percent_t pct(T value) { return value / 100.f; }
88+
// DEPRECATED: 2025q1
89+
modm_deprecated("Access the value directly.") constexpr float pct2f(percent_t value) { return value; }
8890
namespace literals
8991
{
9092
constexpr percent_t operator""_pct(long double value) { return pct(value); }

src/modm/math/units.lb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,20 @@ bitrate_t bitrate = modm::kbps(125);
4545
frequency = 4295_MHz; // OVERFLOW at 2^32 units!
4646
```
4747
48-
## Integral Percentages
4948
50-
Since `float` cannot be used as a non-type template argument, an integer type
51-
for providing tolerances in `percent_t` is available.
52-
Note that `percent_t` is implemented as an enum class, which prevents implicit
53-
conversions, since the base for this is not 1.
54-
You must therefore use the `modm::pct(T value)` or `_pct` constructors.
49+
## Percentages
50+
51+
Percentages are stored as normalized floating point numbers and can be converted
52+
using these convenience constructors:
5553
5654
```cpp
5755
using namespace modm::literals;
5856
5957
percent_t tolerance = modm::pct(10);
6058
tolerance = 10_pct;
61-
62-
// convert back to float. *internal use only*
63-
float percent = modm::pct2f(tolerance);
59+
tolerance = 0.1f;
6460
```
6561
66-
!!! warning "This type is not guaranteed to hold more than 100 percent!"
6762
6863
"""
6964

src/modm/platform/can/common/can_bit_timings.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class CanBitTiming
115115
template<percent_t tolerance>
116116
static constexpr void assertBitrateInTolerance()
117117
{
118-
static_assert(pct2f(tolerance) >= BestConfig.error,
118+
static_assert(tolerance >= BestConfig.error,
119119
"The closest available bitrate exceeds the specified maximum tolerance!");
120120
}
121121

src/modm/platform/i2c/sam_x7x/i2c_master.hpp.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private:
6161
const float freq = 1.f / (tHigh + tLow);
6262
const float error = std::fabs(1.f - freq / baudrate);
6363

64-
return error < pct2f(tolerance);
64+
return error < tolerance;
6565
}
6666

6767
template<class SystemClock, baudrate_t baudrate, percent_t tolerance>

src/modm/platform/i2c/stm32-extended/i2c_timing_calculator.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class I2cTimingCalculator
9797
float speed = calculateSpeed(prescaler, sclLow, sclHigh);
9898
auto error = std::fabs((speed / params.targetSpeed) - 1.0f);
9999
// modm::Tolerance value is in unit [1/1000]
100-
if ((error < pct2f(params.tolerance)) && (error <= lastError) && (prescaler <= bestPrescaler)) {
100+
if ((error < params.tolerance) && (error <= lastError) && (prescaler <= bestPrescaler)) {
101101
lastError = error;
102102
bestPrescaler = prescaler;
103103
bestSclLow = sclLow;
@@ -290,7 +290,7 @@ class I2cTimingCalculator
290290
auto clockPeriod = float(prescaler + 1) / params.peripheralClock;
291291

292292
auto targetSclTime = 1.f / params.targetSpeed;
293-
auto sclTimeMax = targetSclTime * (1.f + pct2f(params.tolerance));
293+
auto sclTimeMax = targetSclTime * (1.f + params.tolerance);
294294
auto maxSclSum = ((sclTimeMax - params.riseTime - params.fallTime - 2*SyncTime)
295295
/ clockPeriod) - 2;
296296

0 commit comments

Comments
 (0)