Go Down

Topic: abs bug (Read 921 times) previous topic - next topic

Mike

I think the abs function does not work in arduino since I still get negative values.   :o

mellis

Can you post an example of code that has problems?

mellis

You're right, abs has a bug, as do min, max, and constrain.  They'll be fixed in Arduino 0004.  In the meantime, you can put the following at the very top of your sketch (before the variable definitions).

Code: [Select]

#undef min
#undef max
#undef abs
#undef constrain
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))

Go Up