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

detect division by zero in map() to prevent exceptions (#2397)

Will return -1 like AVR would
This commit is contained in:
Me No Dev 2016-08-18 10:50:59 +03:00 committed by GitHub
parent 7746288b41
commit fb00e64a6f

View File

@ -70,7 +70,11 @@ long secureRandom(long howsmall, long howbig) {
}
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 divisor = (in_max - in_min) + out_min;
if(divisor == 0){
return -1; //AVR returns -1, SAM returns 0
}
return (x - in_min) * (out_max - out_min) / divisor;
}
unsigned int makeWord(unsigned int w) {