From 89feacb813f3791ff7cd74498b2b568aa7b7e4ba Mon Sep 17 00:00:00 2001 From: Artur Bieniek Date: Wed, 5 Jun 2019 13:48:13 +0200 Subject: [PATCH] Fix map() division by zero (#2848) --- cores/esp32/WMath.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cores/esp32/WMath.cpp b/cores/esp32/WMath.cpp index 891a737d..abec110b 100644 --- a/cores/esp32/WMath.cpp +++ b/cores/esp32/WMath.cpp @@ -65,9 +65,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) { + 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; } unsigned int makeWord(unsigned int w)