Skip to content

Commit

Permalink
Greatly reduces error rate (half, or 0 zero errors, depends on in/out…
Browse files Browse the repository at this point in the history
… ranges) for round-trip mapping at the same performance.

(Based on "improved_map" from ESP8266's Servo.cpp)
  • Loading branch information
dok-net committed Jan 21, 2020
1 parent 0e7fae8 commit e6be389
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions cores/arduino/WMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ long random(long howsmall, long howbig)
return random(diff) + howsmall;
}

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
long map(long x, long in_min, long in_max, long out_min, long out_max) {
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) { return w; }
Expand Down

0 comments on commit e6be389

Please sign in to comment.