Skip to content

Commit

Permalink
detect division by zero in map() to prevent exceptions (esp8266#2397)
Browse files Browse the repository at this point in the history
Will return -1 like AVR would
  • Loading branch information
me-no-dev authored Aug 18, 2016
1 parent 7746288 commit fb00e64
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cores/esp8266/WMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit fb00e64

Please sign in to comment.