@@ -139,24 +139,6 @@ void yield(void);
139139
140140#endif
141141
142- // undefine stdlib's abs if encountered
143- #ifdef abs
144- #undef abs
145- #endif
146-
147- #define abs (x ) ({ typeof (x) _x = (x); _x > 0 ? _x : -_x; })
148- #define sq (x ) ({ typeof (x) _x = (x); _x * _x; })
149- #define min (a ,b ) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; })
150- #define max (a ,b ) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
151- #define round (x ) ({ typeof (x) _x = (x); _x >= 0 ? (long)(_x + 0.5) : (long)(_x - 0.5); })
152- #define radians (deg ) ((deg) * DEG_TO_RAD)
153- #define degrees (rad ) ((rad) * RAD_TO_DEG)
154- #define constrain (x ,low ,high ) ({ \
155- typeof (x) _x = (x); \
156- typeof (low) _l = (low); \
157- typeof (high) _h = (high); \
158- _x < _l ? _l : _x > _h ? _h : _x; })
159-
160142#define interrupts () sei()
161143#define noInterrupts () cli()
162144
@@ -316,6 +298,66 @@ extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
316298} // extern "C"
317299#endif
318300
301+ // Undefine stdlib's abs if encountered
302+ #ifdef abs
303+ #undef abs
304+ #endif
305+
306+ #ifdef __cplusplus
307+ template <class T >
308+ auto abs (const T& x) -> decltype(x > 0 ? x : -x) {
309+ return x > 0 ? x : -x;
310+ }
311+
312+ template <class T , class L >
313+ auto min (const T& a, const L& b) -> decltype((b < a) ? b : a) {
314+ return (b < a) ? b : a;
315+ }
316+
317+ template <class T , class L >
318+ auto max (const T& a, const L& b) -> decltype((b < a) ? b : a) {
319+ return (a < b) ? b : a;
320+ }
321+
322+ template <class T >
323+ long round (const T& x) {
324+ return (long )(x >= 0 ? (x + 0.5 ) : (x - 0.5 ));
325+ }
326+
327+ template <class T >
328+ auto sq (const T& x) -> decltype(x * x) {
329+ return x * x;
330+ }
331+
332+ template <class T >
333+ auto radians (const T& deg) -> decltype(deg * DEG_TO_RAD) {
334+ return deg * DEG_TO_RAD;
335+ }
336+
337+ template <class T >
338+ auto degrees (const T& rad) -> decltype(rad * RAD_TO_DEG) {
339+ return rad * RAD_TO_DEG;
340+ }
341+
342+ template <class T , class L , class H >
343+ auto constrain (const T& x, const L& l, const H& h) -> decltype((x < l) ? l : (x > h) ? h : x) {
344+ return (x < l) ? l : (x > h) ? h : x;
345+ }
346+
347+ #else
348+ #define abs (x ) ({ typeof (x) _x = (x); _x > 0 ? _x : -_x; })
349+ #define min (a,b ) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a < _b ? _a : _b; })
350+ #define max (a,b ) ({ typeof (a) _a = (a); typeof (b) _b = (b); _a > _b ? _a : _b; })
351+ #define sq (x ) ({ typeof (x) _x = (x); _x * _x; })
352+ #define radians (deg ) ((deg) * DEG_TO_RAD)
353+ #define degrees (rad ) ((rad) * RAD_TO_DEG)
354+ #define constrain (x,low,high ) ({ \
355+ typeof (x) _x = (x); \
356+ typeof (low) _l = (low); \
357+ typeof (high) _h = (high); \
358+ _x < _l ? _l : _x > _h ? _h : _x; })
359+ #endif // __cplusplus
360+
319361#ifdef __cplusplus
320362#include " WCharacter.h"
321363#include " WString.h"
0 commit comments