Skip to content

Commit 841a1b8

Browse files
committed
use arduino-api for min/max
1 parent ceccc25 commit 841a1b8

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

cores/arduino/Arduino.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,33 @@ void loop( void ) ;
9797
#undef abs
9898
#endif // abs
9999

100-
#define min(a,b) ((a)<(b)?(a):(b))
101-
#define max(a,b) ((a)>(b)?(a):(b))
100+
#ifdef __cplusplus
101+
template<class T, class L>
102+
auto min(const T& a, const L& b) -> decltype((b < a) ? b : a)
103+
{
104+
return (b < a) ? b : a;
105+
}
106+
107+
template<class T, class L>
108+
auto max(const T& a, const L& b) -> decltype((b < a) ? b : a)
109+
{
110+
return (a < b) ? b : a;
111+
}
112+
#else
113+
#ifndef min
114+
#define min(a,b) \
115+
({ __typeof__ (a) _a = (a); \
116+
__typeof__ (b) _b = (b); \
117+
_a < _b ? _a : _b; })
118+
#endif
119+
#ifndef max
120+
#define max(a,b) \
121+
({ __typeof__ (a) _a = (a); \
122+
__typeof__ (b) _b = (b); \
123+
_a > _b ? _a : _b; })
124+
#endif
125+
#endif
126+
102127
#define abs(x) ((x)>0?(x):-(x))
103128
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
104129
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))

0 commit comments

Comments
 (0)