Skip to content

Commit 5882902

Browse files
committed
Fix "improved_map" from Servo.cpp and use this is in core implementation.
Greatly reduces error rate (half, or 0 zero errors, depends on in/out ranges) for round-trip mapping at the same performance.
1 parent 14f6272 commit 5882902

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

cores/esp8266/WMath.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ long secureRandom(long howsmall, long howbig) {
7070
}
7171

7272
long map(long x, long in_min, long in_max, long out_min, long out_max) {
73-
long divisor = (in_max - in_min);
74-
if(divisor == 0){
75-
return -1; //AVR returns -1, SAM returns 0
76-
}
77-
return (x - in_min) * (out_max - out_min) / divisor + out_min;
73+
const long dividend = out_max - out_min;
74+
const long divisor = in_max - in_min;
75+
const long delta = x - in_min;
76+
77+
return (delta * dividend + (divisor / 2)) / divisor + out_min;
7878
}
7979

8080
unsigned int makeWord(unsigned int w) {

0 commit comments

Comments
 (0)