From 0114345483f793d69dd2127f3037ff7488c952e3 Mon Sep 17 00:00:00 2001 From: IanSC <8453489+IanSC@users.noreply.github.com> Date: Tue, 1 Feb 2022 05:57:39 +0800 Subject: [PATCH 1/2] Unneccesary Operation Removed (A) extra operation not needed and incorrect: wrong by 0.5 but happens to be thrown out ( delta * dividend + (divisor / 2) ) / divisor delta * dividend divisor = ---------------- + ----------- divisor 2 * divisor = delta * dividend / divisor + 1/2 (B) check first before doing other computations (C) changed to rise/run, easier for future maintainer since it's closer to equation of a line (D) before: mult, shift, add, div, add now: mult, div, add (E) error message easier to trace where thrown --- cores/esp32/WMath.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cores/esp32/WMath.cpp b/cores/esp32/WMath.cpp index bb75fc8a2f6..aefefc4f617 100644 --- a/cores/esp32/WMath.cpp +++ b/cores/esp32/WMath.cpp @@ -67,14 +67,14 @@ long random(long howsmall, long howbig) } long map(long x, long in_min, long in_max, long out_min, long out_max) { - const long dividend = out_max - out_min; - const long divisor = in_max - in_min; - const long delta = x - in_min; + const long run = in_max - in_min; if(divisor == 0){ - log_e("Invalid map input range, min == max"); - return -1; //AVR returns -1, SAM returns 0 + log_e("map(): Invalid input range, min == max"); + return -1; // AVR returns -1, SAM returns 0 } - return (delta * dividend + (divisor / 2)) / divisor + out_min; + const long rise = out_max - out_min; + const long delta = x - in_min; + return (delta * rise) / run + out_min; } uint16_t makeWord(uint16_t w) From 65decc4ca0978c1d552d4b1deaa51f72b5d1fdb8 Mon Sep 17 00:00:00 2001 From: IanSC <8453489+IanSC@users.noreply.github.com> Date: Tue, 1 Feb 2022 08:17:32 +0800 Subject: [PATCH 2/2] Update WMath.cpp forgot to change variable name --- cores/esp32/WMath.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/WMath.cpp b/cores/esp32/WMath.cpp index aefefc4f617..931ac96637b 100644 --- a/cores/esp32/WMath.cpp +++ b/cores/esp32/WMath.cpp @@ -68,7 +68,7 @@ long random(long howsmall, long howbig) long map(long x, long in_min, long in_max, long out_min, long out_max) { const long run = in_max - in_min; - if(divisor == 0){ + if(run == 0){ log_e("map(): Invalid input range, min == max"); return -1; // AVR returns -1, SAM returns 0 }