"min" function with multiple numbers.

G'Day All,
I'm trying to take the smallest of 3 numbers using the "min" function, using as a reference 'The ardunio cookbook' page 75.

CWsens = analogRead ( CWsensorPIN );
delay(5);
int CWsens1 = analogRead ( CWsensorPIN );
delay(5);
int CWsens2 = analogRead ( CWsensorPIN );
delay(5);
CWsens = min ( CWsens, min(CWsens1), CWsens2 ) ;

I've tried a few variations but it comes up with the error "min was not declared in this scope".
Can someone put me out of my misery ?

Cheers ....... Mike B

min is for two values.
Change this:

               CWsens = min ( CWsens, min(CWsens1), CWsens2 ) ;

to this:

               CWsens = min ( CWsens, min(CWsens1, CWsens2) ) ;

--- bill

CWsens = min ( CWsens, min(CWsens1, CWsens2 )); // OK
or
CWsens = min ( CWsens, (min(CWsens1, CWsens2 ))); // to make sure - no offence , OK
Vaclav

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.

--- bill

Vaclav:
CWsens = min ( CWsens, min(CWsens1, CWsens2 )); // OK
or
CWsens = min ( CWsens, (min(CWsens1, CWsens2 ))); // to make sure - no offence , OK
Vaclav

To "make sure" of what, exactly?

The parentheses were missed / misplaced on OP, so I provided TWO LOGICALLY SAME answers to illustrate it.
And it was missed again. O well.

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.

Thanks to All,

On the subject of the brackets, the first solution didn't solve it but Vaclav's extra brackets did .

Cheers ............... Mike B

Froggins:
Thanks to All,

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.

Froggins:
Thanks to All,

On the subject of the brackets, the first solution didn't solve it but Vaclav's extra brackets did .

Cheers ............... Mike B

The question is about parens not brackets, but

Are you saying this:

CWsens = min ( CWsens, min(CWsens1, CWsens2) ) ;

didn't work?

--- bill

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.

Let's make really sure:

  CWsens = ((min ( (CWsens), (min((CWsens1), (CWsens2)))))) ;

You're not helping, Nick.

Sorry. I was just reinforcing your point.

pico:
To "make sure" of what, exactly?

I know. But my feeling is that it's still not helping!

if you want the minimum of multiple (>2) values you should consider using an array

int CWsens[3];

for (int i = 0; i< 3; i++)
{
    CWsens[i] = analogRead ( CWsensorPIN );
    delay(5);
}

int minimum = 9999;
for (int i = 0; i< 3; i++)
{
    minimum  = min(minimum , CWsens[i]);
}

AND/OR considering updating the minimum after each read.

int CWsens[3];
int minimum = 9999;
for (int i = 0; i< 3; i++)
{
    CWsens[i] = analogRead ( CWsensorPIN );
     minimum  = min(minimum , CWsens[i]);
    delay(5);  
}

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!

Cheers
Vaclav

I do not get how we got into explaining why manipulation of the macro parameters is a bad idea. That was not an issue.

Nerds get distracted by the all details, I know from experience :wink:

Vaclav:
Third – making triple sure by adding additional and unnecessary parenthesis just defeated my “making sure “ post. IMHO it did not help.

Sorry, some times my sense of humour runs away with me. :wink:

Here is what I told some other poster with such sense of humor:

Beauty is in the eye of the beholder.

No problema.
Cheers
Vaclav