Skip to content

Commit 5be3ff7

Browse files
authored
Unnecessary operation removed from map() in WMath.cpp (#6218)
* 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 * Update WMath.cpp forgot to change variable name
1 parent dafdc05 commit 5be3ff7

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Diff for: cores/esp32/WMath.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ long random(long howsmall, long howbig)
6767
}
6868

6969
long map(long x, long in_min, long in_max, long out_min, long out_max) {
70-
const long dividend = out_max - out_min;
71-
const long divisor = in_max - in_min;
72-
const long delta = x - in_min;
73-
if(divisor == 0){
74-
log_e("Invalid map input range, min == max");
75-
return -1; //AVR returns -1, SAM returns 0
70+
const long run = in_max - in_min;
71+
if(run == 0){
72+
log_e("map(): Invalid input range, min == max");
73+
return -1; // AVR returns -1, SAM returns 0
7674
}
77-
return (delta * dividend + (divisor / 2)) / divisor + out_min;
75+
const long rise = out_max - out_min;
76+
const long delta = x - in_min;
77+
return (delta * rise) / run + out_min;
7878
}
7979

8080
uint16_t makeWord(uint16_t w)

0 commit comments

Comments
 (0)