mirror of
https://github.com/esphome/esphome.git
synced 2026-10-01 08:50:22 +00:00
[sensor] Replace powf with pow10_int in sensor filters
Add shared pow10_int() helper to helpers.h that computes 10^exp using iterative multiplication/division instead of powf. Replace powf(10, exp) calls in: - sensor/filter.cpp: ValueListFilter and RoundFilter - helpers.cpp: normalize_accuracy_decimals (refactored to use pow10_int for the general case, keeping -1/-2 fast paths) Eliminates powf/__ieee754_powf from builds where sensor filters are the only remaining powf call site.
This commit is contained in:
@@ -468,20 +468,15 @@ ParseOnOffState parse_on_off(const char *str, const char *on, const char *off) {
|
||||
|
||||
static inline void normalize_accuracy_decimals(float &value, int8_t &accuracy_decimals) {
|
||||
if (accuracy_decimals < 0) {
|
||||
// Clamp to -9 to keep divisor within uint32_t range (max 10^9 = 1,000,000,000)
|
||||
int8_t dec = accuracy_decimals < -9 ? (int8_t) -9 : accuracy_decimals;
|
||||
uint32_t divisor;
|
||||
if (dec == -1) {
|
||||
divisor = 10;
|
||||
} else if (dec == -2) {
|
||||
divisor = 100;
|
||||
float divisor;
|
||||
if (accuracy_decimals == -1) {
|
||||
divisor = 10.0f;
|
||||
} else if (accuracy_decimals == -2) {
|
||||
divisor = 100.0f;
|
||||
} else {
|
||||
divisor = 1000;
|
||||
for (int8_t i = dec + 3; i < 0; i++)
|
||||
divisor *= 10;
|
||||
divisor = pow10_int(-accuracy_decimals);
|
||||
}
|
||||
auto divisor_f = static_cast<float>(divisor);
|
||||
value = roundf(value / divisor_f) * divisor_f;
|
||||
value = roundf(value / divisor) * divisor;
|
||||
accuracy_decimals = 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user