Skip to content

Commit 2f249ed

Browse files
projectgusme-no-dev
authored andcommitted
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. * 1e4bf14 (#1783) replaced similar min, max macros with c++ stdlib. However this change includes <algorithm> after the line which defines the abs() macro. <algorithm> includes <cstdlib> 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: espressif/esp-idf#3405 This fix tries to keep in the spirit of #1783 by using libstdc++. The other option would be to include <cstdlib> before defining the abs() macro, so it doesn't get undef-ed again later on.
1 parent bd57ff4 commit 2f249ed

File tree

1 file changed

+1
-6
lines changed

1 file changed

+1
-6
lines changed

cores/esp32/Arduino.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@
6868
#define __STRINGIFY(a) #a
6969
#endif
7070

71-
// undefine stdlib's abs if encountered
72-
#ifdef abs
73-
#undef abs
74-
#endif
75-
76-
#define abs(x) ((x)>0?(x):-(x))
7771
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
7872
#define radians(deg) ((deg)*DEG_TO_RAD)
7973
#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);
160154
#include "HardwareSerial.h"
161155
#include "Esp.h"
162156

157+
using std::abs;
163158
using std::isinf;
164159
using std::isnan;
165160
using std::max;

0 commit comments

Comments
 (0)