1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-06 05:21:22 +03:00

Merge pull request #7027 from dok-net/wmath_map

Fix WMath's map() implementation for inverse/round-trip mapping
This commit is contained in:
Earle F. Philhower, III 2020-02-08 20:00:42 -08:00 committed by GitHub
commit 56b90a2abb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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) {