From 2f249edb8e2627421be87c43167a187e55ed7db7 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Sat, 11 May 2019 16:18:39 +0800 Subject: [PATCH] Use std::abs for a float-compatible abs() function (#2738) * Other Arduino cores uses a macro to redefine libc abs() to take any type, meaning abs(-3.3) == 3.3 not the normal libc result of 3. * 1e4bf14a3 (#1783) replaced similar min, max macros with c++ stdlib. However this change includes after the line which defines the abs() macro. includes which undefines abs() and re-defines it. * This means abs() becomes the plain libc version again which only takes integers, so abs(-3.3) == 3. As reported here: https://github.com/espressif/esp-idf/issues/3405 This fix tries to keep in the spirit of #1783 by using libstdc++. The other option would be to include before defining the abs() macro, so it doesn't get undef-ed again later on. --- cores/esp32/Arduino.h | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index 98e0176f..645b4070 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -68,12 +68,6 @@ #define __STRINGIFY(a) #a #endif -// undefine stdlib's abs if encountered -#ifdef abs -#undef abs -#endif - -#define abs(x) ((x)>0?(x):-(x)) #define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) #define radians(deg) ((deg)*DEG_TO_RAD) #define degrees(rad) ((rad)*RAD_TO_DEG) @@ -160,6 +154,7 @@ void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); #include "HardwareSerial.h" #include "Esp.h" +using std::abs; using std::isinf; using std::isnan; using std::max;