Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5be3ff7

Browse filesBrowse files
authored
Unnecessary operation removed from map() in WMath.cpp (espressif#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
Copy full SHA for 5be3ff7

File tree

Expand file treeCollapse file tree

1 file changed

+7
-7
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+7
-7
lines changed

‎cores/esp32/WMath.cpp

Copy file name to clipboardExpand all lines: cores/esp32/WMath.cpp
+7-7Lines changed: 7 additions & 7 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.