Vaclav:
CWsens = min ( CWsens, min(CWsens1, CWsens2 )); // OK
or
CWsens = min ( CWsens, (min(CWsens1, CWsens2 ))); // to make sure - no offence , OK
Vaclav
The extra parentheses are not needed.
min() is normally a function and for functions the extra parentheses are not needed.
Arduino and other enviroments override the min() function with a macro.
Here is the min() macro definition from Arduino.h
#define min(a,b) ((a)<(b)?(a):(b))
The extra parentheses around the parameters and the and final expression result
are there to make the result from the ternary
behave like the return from a function, where no extra parentheses are needed.
The biggest danger with replacing the min() function with a min() macro
the way the Arduino code does, isn't a danger of needing extra parentheses to gauard
the evaluated expression, but in the macro definition itself.
The min() defintion references the arguments more than once so if you try to do something like this:
minval = min(a++, min(b++, c++));
It will not work the same with their simplistic macro definition vs a function.
There are still ways to do it, with either a smarter macro or with an inline function,
but as a simplist macro as they have done, the paramers will be used more than once which breaks
things like post operations.
And there is no way to fix that with parentheses.
Vaclav:
CWsens = min ( CWsens, min(CWsens1, CWsens2 )); // OK
or
CWsens = min ( CWsens, (min(CWsens1, CWsens2 ))); // to make sure - no offence , OK
Vaclav
Vaclav:
The parentheses were missed / misplaced on OP, so I provided TWO LOGICALLY SAME answers to illustrate it.
And it was missed again. O well.
Yes, bperrybap spotted that and addressed the original issue.
You replied to bperrybap's answer with what you seemed to believe was an "improved" answer. I was curious about what exactly it was you thought you were "making sure" of. I suspect whatever it is you think you were "making sure" of rests on a misconception of some sort on your part.
On the subject of the brackets, the first solution didn't solve it but Vaclav's extra brackets did .
Cheers ............... Mike B
I can assure you this isn't the case. If you believe the first placement of the parenthesis provided by bperrybap didn't work, you must have mistyped something.
OK guys,
I am still trying to figure out how to reply to specific post in thread, so don't take this personally.
I have noted few things in this one.
The OP was a syntax error – missing parenthesis. I think we all agree on that.
I do not get how we got into explaining why manipulation of the macro parameters is a bad idea. That was not an issue.
Second - since the parenthesis were missed ,my lucky shot – been there done that, I provided an optional code to make it more visible. But I did not explained it well, my mistake.
Third – making triple sure by adding additional and unnecessary parenthesis just defeated my “making sure “ post. IMHO it did not help.
The moral of the story – in programming you need to sweat the small stuff!