diff --git a/CHANGELOG.md b/CHANGELOG.md index a05a2dad9..32293cd29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/tasmota/tasmota_support/support_float.ino b/tasmota/tasmota_support/support_float.ino index dcef29671..c72978a5c 100644 --- a/tasmota/tasmota_support/support_float.ino +++ b/tasmota/tasmota_support/support_float.ino @@ -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;