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

Sign up
Appearance settings

Commit ab23e8a

Browse files
authored
Greatly reduces error rate (half, or 0 zero errors, depends on in/out ranges) for round-trip mapping at the same performance. (#3655)
(Based on "improved_map" from ESP8266's Servo.cpp)
1 parent 5999b7b commit ab23e8a

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

‎cores/esp32/WMath.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ long random(long howsmall, long howbig)
6666
}
6767

6868
long map(long x, long in_min, long in_max, long out_min, long out_max) {
69-
long divisor = (in_max - in_min);
70-
if(divisor == 0){
71-
return -1; //AVR returns -1, SAM returns 0
72-
}
73-
return (x - in_min) * (out_max - out_min) / divisor + out_min;
69+
constlong dividend = out_max - out_min;
70+
constlongdivisor = in_max - in_min;
71+
constlong delta = x - in_min;
72+
73+
return (delta * dividend + (divisor / 2)) / divisor + out_min;
7474
}
7575

7676
unsigned int makeWord(unsigned int w)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /