Fixed `changeUIntScale` for linearity when expanding range (#20089)

This commit is contained in:
s-hadinger 2023-11-23 10:57:47 +01:00 committed by GitHub
parent a693c28d07
commit 1cd13d7f66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View File

@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file.
- Matter update hierarchy of plugins (#19915)
- NeoPool ``NPHydrolysis`` percent and unit (#19924)
- Thermostat JSON index from 0 to 1 (#20011)
- Fixed `changeUIntScale` for linearity when expanding range
### Fixed
- Scripter timer issue (#19914)

View File

@ -412,8 +412,13 @@ uint16_t changeUIntScale(uint16_t inum, uint16_t ifrom_min, uint16_t ifrom_max,
uint32_t result;
if ((num - from_min) < 0x8000L) { // no overflow possible
uint32_t numerator = ((num - from_min) * 2 + 1) * (to_max - to_min + 1);
result = numerator / ((from_max - from_min + 1) * 2) + to_min;
if (to_max - to_min > from_max - from_min) {
uint32_t numerator = (num - from_min) * (to_max - to_min) * 2;
result = ((numerator / (from_max - from_min)) + 1 ) / 2 + to_min;
} else {
uint32_t numerator = ((num - from_min) * 2 + 1) * (to_max - to_min + 1);
result = numerator / ((from_max - from_min + 1) * 2) + to_min;
}
} else { // no pre-rounding since it might create an overflow
uint32_t numerator = (num - from_min) * (to_max - to_min + 1);
result = numerator / (from_max - from_min) + to_min;