Skip to content

Commit

Permalink
Fix "improved_map" from Servo.cpp and use this is in core implementat…
Browse files Browse the repository at this point in the history
…ion.

Greatly reduces error rate (half, or 0 zero errors, depends on in/out ranges) for round-trip mapping at the same performance.
  • Loading branch information
dok-net committed Feb 5, 2020
1 parent 14f6272 commit 5882902
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions cores/esp8266/WMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ long secureRandom(long howsmall, long howbig) {
}

long map(long x, long in_min, long in_max, long out_min, long out_max) {
long divisor = (in_max - in_min);
if(divisor == 0){
return -1; //AVR returns -1, SAM returns 0
}
return (x - in_min) * (out_max - out_min) / divisor + out_min;
const long dividend = out_max - out_min;
const long divisor = in_max - in_min;
const long delta = x - in_min;

return (delta * dividend + (divisor / 2)) / divisor + out_min;
}

unsigned int makeWord(unsigned int w) {
Expand Down

0 comments on commit 5882902

Please sign in to comment.