P: n/a

I need to find a minimum of three float values.. what would be the most
efficient way of doing this? Can someone please share a #define macro or
something with me for doing this? Thanks
Sona  
Share this Question
P: n/a

"Sona" <so**********@nospam.com> wrote in message
news:3f********@clarion.carno.net.au... I need to find a minimum of three float values.. what would be the most efficient way of doing this? Can someone please share a #define macro or something with me for doing this? Thanks
Sona
x = min( a, min( b, c ) );
Howard  
P: n/a

Sona wrote: I need to find a minimum of three float values.. what would be the most efficient way of doing this? Can someone please share a #define macro or something with me for doing this? Thanks
In C++
#include <algoritm>
double min3(double a, double b, double c) {
return std::min(a, std::min(b, c));
}  
P: n/a

> #include <algoritm> double min3(double a, double b, double c) { return std::min(a, std::min(b, c)); }
To keep the templated aspect of min, you could even write
namespace
{
template<class T>
T& min(T const& a, T const& b, T const& c)
{
return min(a, min(b,c));
}
}
Like that std::min works with any type, and with 2 or 3 arguments  
P: n/a

Sona wrote: I need to find a minimum of three float values.. what would be the most efficient way of doing this? Can someone please share a #define macro or something with me for doing this? Thanks
Sona
Sona,
Thank you for your post.
Steve
#include <stdio.h>
float min(float h, float j);
int main()
{
float a, b, c;
printf("Enter three float values, separated by spaces: ");
scanf("%f%f%f", &a, &b, &c);
/*
* NOTE: I had to round the floats off to two decimal places
* in the following printf statement, because I was getting
* weird errors otherwise.
*/
printf("The minimum of these three is %.2f\n", min(a, min(b, c)));
return 0;
}
float min(float h, float j)
{
if (h < j)
return h;
else
return j;
}  
P: n/a

[crossposting to c.l.c++ fixed]
Steve Zimmerman <st******@sonic.net> wrote: Sona wrote: I need to find a minimum of three float values [...]
<SNIP>
Why not make use of the standard C library?
Code modified:
#include <stdio.h>
#include <math.h> /* for fminf() */
int main( void )
{
float a, b, c;
printf("Enter three float values, separated by spaces: ");
scanf("%f%f%f", &a, &b, &c);
printf("The minimum is: %f\n", fminf(a, fminf(b, c)) );
return 0;
}
<SNIP>

Close your eyes and press escape three times.  
P: n/a

"Howard" <al*****@hotmail.com> wrote in message news:<bj********@dispatch.concentric.net>... "Sona" <so**********@nospam.com> wrote in message news:3f********@clarion.carno.net.au... I need to find a minimum of three float values.. what would be the most efficient way of doing this? Can someone please share a #define macro or something with me for doing this? Thanks
Sona
x = min( a, min( b, c ) );
Given a typical definition of min(), that will not produce an efficient result.
#define min3(a,b,c) \
( (a) < (b) ? min(a,c) : min(b,c) )

Peter  
P: n/a

Peter Nilsson wrote: "Howard" <al*****@hotmail.com> wrote in message news:<bj********@dispatch.concentric.net>... x = min( a, min( b, c ) );
Given a typical definition of min(), that will not produce an efficient result.
#define min3(a,b,c) \ ( (a) < (b) ? min(a,c) : min(b,c) )
Have you counted the number of evaluations of a or b this involves? There
is a good reason for preferring functions over defines.

Martin Ambuhl  
P: n/a

Irrwahn Grausewitz wrote: Steve Zimmerman <st******@sonic.net> wrote:Sona wrote:
I need to find a minimum of three float values [...]
Why not make use of the standard C library? Code modified:
#include <stdio.h> #include <math.h> /* for fminf() */
Is this in C89, too? If not, the standard C library provides qsort.
It is not *that* efficient for 3 values, but in return it's scalable.
#include <stdio.h>
#include <stdlib.h>
static int cmp(const void *a, const void *b)
{
return *(float *)a < *(float *)b ? 1 : 1; /* Is 0 really required? */
}
int main( void )
{
float v[] = {2.3, 2.4, 2.299};
qsort(v, sizeof v / sizeof *v, sizeof *v, cmp);
printf("The minimum is: %f\n", *v);
return 0;
}
Jirka  
P: n/a

Martin Ambuhl <ma*****@earthlink.net> wrote: Peter Nilsson wrote: #define min3(a,b,c) \ ( (a) < (b) ? min(a,c) : min(b,c) )
Have you counted the number of evaluations of a or b this involves? There is a good reason for preferring functions over defines.
Not to mention possible side effects; consider:
float m3, x, y, z;
m3 = min3( x *= 3.142, y /= 2.718, z += 1.111 );
Irrwahn

Computer: a million morons working at the speed of light.  
P: n/a

"Sona" <so**********@nospam.com> wrote in message
news:3f********@clarion.carno.net.au... I need to find a minimum of three float values.. what would be the most efficient way of doing this? Can someone please share a #define macro or something with me for doing this? Thanks
Sona
You want a macro. Here you go:
#define min3(x, y, z) \
((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) < (z))
? (x) : (((y) < (z)) ? (z) : (y)))
You can't beat that for efficiency. Of course it is totally unreadable. But
you asked for it :)
Carsten Hansen  
P: n/a

Carsten Hansen wrote: "Sona" <so**********@nospam.com> wrote in message news:3f********@clarion.carno.net.au...
I need to find a minimum of three float values.. what would be the most efficient way of doing this? Can someone please share a #define macro or something with me for doing this? Thanks
You want a macro. Here you go: #define min3(x, y, z) \ ((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) < (z)) ? (x) : (((y) < (z)) ? (z) : (y)))
You can't beat that for efficiency.
It is not even correct.
float min3(float x, float y, float z)
{
if (x < y)
if (x < z) return x;
if (y < z) return y;
return z;
}
Jirka  
P: n/a

"Carsten Hansen" <ha******@worldnet.att.net> wrote: You want a macro. Here you go: #define min3(x, y, z) \ ((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) < (z)) ? (x) : (((y) < (z)) ? (z) : (y)))
*urk*

Computer: a million morons working at the speed of light.  
P: n/a

"Jirka Klaue" <jk****@ee.tuberlin.de> wrote in message
news:bj**********@mamenchi.zrz.TUBerlin.DE... Carsten Hansen wrote: "Sona" <so**********@nospam.com> wrote in message news:3f********@clarion.carno.net.au...
I need to find a minimum of three float values.. what would be the most efficient way of doing this? Can someone please share a #define macro or something with me for doing this? Thanks
You want a macro. Here you go: #define min3(x, y, z) \ ((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) <
(z)) ? (x) : (((y) < (z)) ? (z) : (y)))
You can't beat that for efficiency.
It is not even correct.
float min3(float x, float y, float z) { if (x < y) if (x < z) return x; if (y < z) return y; return z; }
Jirka
Give an example where it fails.
Carsten Hansen  
P: n/a

"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f***********************@news.newshosting.co m... Sona wrote: I need to find a minimum of three float values.. what would be the most efficient way of doing this? Can someone please share a #define macro or something with me for doing this? Thanks
In C++
#include <algoritm>
double min3(double a, double b, double c) { return std::min(a, std::min(b, c)); }
Please do not post offtopic things here. It waste the bandwidth and time.

Jeff  
P: n/a

"Jeff" <no*****@notexist.com> wrote: "Ron Natalie" <ro*@sensor.com> wrote in message news:3f***********************@news.newshosting.c om... In C++
#include <algoritm>
double min3(double a, double b, double c) { return std::min(a, std::min(b, c)); }
Please do not post offtopic things here. It waste the bandwidth and time.
Erm, (almost) the whole thread is crossposted between c.l.c and c.l.c++

Computer: a million morons working at the speed of light.  
P: n/a

Carsten Hansen wrote: "Jirka Klaue" wrote:Carsten Hansen wrote:"Sona" wrote:
I need to find a minimum of three float values.. what would be the most efficient way of doing this? Can someone please share a #define macro or something with me for doing this? Thanks
You want a macro. Here you go: #define min3(x, y, z) \ ((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) < (z)) ? (x) : (((y) < (z)) ? (z) : (y)))
You can't beat that for efficiency.
It is not even correct.
Give an example where it fails.
#include <stdio.h>
#define MIN3(x, y, z) \
((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) < (z)) ? (x) : (((y) < (z)) ? (z) : (y)))
int main(void)
{
float a = 1.6, b = 1.7, c = 1.8;
printf("%f\n", MIN3(a, b, c));
a = 1.9, b = 1.7, c = 1.8;
printf("%f\n", MIN3(a, b, c));
a = 1.6, b = 1.7, c = 1.5;
printf("%f\n", MIN3(a, b, c));
return 0;
}
1.700000
1.800000
1.600000
Now, give an example where it works.
And even when you fix it, it probably is not the most efficient method.
Jirka  
P: n/a

Carsten Hansen wrote: "Jirka Klaue" <jk****@ee.tuberlin.de> wrote in message news:bj**********@mamenchi.zrz.TUBerlin.DE...
Carsten Hansen wrote:
"Sona" <so**********@nospam.com> wrote in message news:3f********@clarion.carno.net.au...
I need to find a minimum of three float values.. what would be the most efficient way of doing this? Can someone please share a #define macro or something with me for doing this? Thanks
You want a macro. Here you go: #define min3(x, y, z) \ ((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) < (z)) ? (x) : (((y) < (z)) ? (z) : (y)))
You can't beat that for efficiency. It is not even correct.
float min3(float x, float y, float z) { if (x < y) if (x < z) return x; if (y < z) return y; return z; }
Jirka
Give an example where it fails.
Carsten Hansen
Carsten,
Thank you for your post.
I don't intend this post as meanspirited; I cannot write such
a macro.
#include <stdio.h>
#define min3(x, y, z) \
((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : \
(((x) < (z)) ? (x) : (((y) < (z)) ? (z) : (y)))
int main()
{
float x, y, z;
printf("Enter three float values, separated by spaces: ");
scanf("%f%f%f", &x, &y, &z);
printf("The least of these is %f\n", min3(x, y, z));
return 0;
}
Input from keyboard: 12345 44444 55555
Output to screen: The least of these is 44444  
P: n/a

On Wed, 10 Sep 2003 04:34:29 +1000, Sona <so**********@nospam.com> wrote: I need to find a minimum of three float values.. what would be the most efficient way of doing this? Can someone please share a #define macro or something with me for doing this? Thanks
First off, DO NOT, I repeat for your convenience, DO NOT, crosspost
general questions to both [comp.lang.c] and [comp.lang.c++].
Those are different languages.
Now _the_ (one and only) answer to your question is simple: the _most
efficient_ way of finding the minimum of three 'double' values is to
leverage contextual information and language and implementation quirks.
That depends very much on the problem to be solved, your current code,
the language you're using, and even the compiler. Post this information,
except the compiler, and you may receive more specific answers.  
P: n/a

"Jirka Klaue" <jk****@ee.tuberlin.de> wrote in message
news:bj**********@mamenchi.zrz.TUBerlin.DE... Carsten Hansen wrote:"Jirka Klaue" wrote:Carsten Hansen wrote: "Sona" wrote:
>I need to find a minimum of three float values.. what would be the
most>efficient way of doing this? Can someone please share a #define macro
or>something with me for doing this? Thanks
You want a macro. Here you go: #define min3(x, y, z) \ ((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) < (z))
? (x) : (((y) < (z)) ? (z) : (y)))
You can't beat that for efficiency.
It is not even correct.
Give an example where it fails.
#include <stdio.h>
#define MIN3(x, y, z) \ ((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) <
(z)) ? (x) : (((y) < (z)) ? (z) : (y))) int main(void) { float a = 1.6, b = 1.7, c = 1.8; printf("%f\n", MIN3(a, b, c));
a = 1.9, b = 1.7, c = 1.8; printf("%f\n", MIN3(a, b, c));
a = 1.6, b = 1.7, c = 1.5; printf("%f\n", MIN3(a, b, c));
return 0; }
1.700000 1.800000 1.600000
Now, give an example where it works. And even when you fix it, it probably is not the most efficient method.
Jirka
Sorry. It is me being stupid. The macro I posted returns the median of the
three, not the minimum.
This one is much simpler.
#define min3(x, y, z) \
(((x) < (y)) ? (((z) < (x)) ? (z) : (x)) : (((z) < (y)) ? (z) : (y)))
Carsten Hansen  
P: n/a

Carsten Hansen wrote:
.... >#define min3(x, y, z) \ >((x) < (y)) ? (((y) < (z)) ? (y) : (((x) < (z)) ? (z) : (x))) : (((x) < (z)) ? (x) : (((y) < (z)) ? (z) : (y)))
.... Sorry. It is me being stupid. The macro I posted returns the median of the three, not the minimum. This one is much simpler.
#define min3(x, y, z) \ (((x) < (y)) ? (((z) < (x)) ? (z) : (x)) : (((z) < (y)) ? (z) : (y)))
I see. Seems to be pretty efficient now, too. :)
Jirka  
P: n/a

Martin Ambuhl <ma*****@earthlink.net> wrote in message news:<Sj****************@newsread2.news.atl.earthl ink.net>... Peter Nilsson wrote: "Howard" <al*****@hotmail.com> wrote in message news:<bj********@dispatch.concentric.net>...x = min( a, min( b, c ) );
Given a typical definition of min(), that will not produce an efficient result.
#define min3(a,b,c) \ ( (a) < (b) ? min(a,c) : min(b,c) )
Have you counted the number of evaluations of a or b this involves? There is a good reason for preferring functions over defines.
Inline functions yes; but functions, in C, not always.
My problem was not realising that the OP cross posted. My Bad.
I was reading it in clc and concentrated on "Can someone please share
a #define macro or something..." in the OP's post.
[I really don't know why people using C++ also ask C groups for
solutions when the answers are highly likely to involve different
paradigms and/or language constructs.]
Anyway, my mistake...
FWIW, C99 has its own version of inline functions too.

Peter  
P: n/a

"Peter Nilsson" <ai***@acay.com.au> wrote in message news:63**************************@posting.google.c om... x = min( a, min( b, c ) );
Given a typical definition of min(), that will not produce an efficient result.
#define min3(a,b,c) \ ( (a) < (b) ? min(a,c) : min(b,c) )
The typical min is inlined to something like
a < b ? a : b
There's always two comparisons. Tests show no speed difference
between the two on my machine.  
P: n/a

On Wed, 10 Sep 2003, Ron Natalie wrote: "Peter Nilsson" <ai***@acay.com.au> wrote... x = min( a, min( b, c ) );
Given a typical definition of min(), that will not produce an efficient result.
#define min3(a,b,c) \ ( (a) < (b) ? min(a,c) : min(b,c) )
The typical min is inlined to something like a < b ? a : b
There's always two comparisons. Tests show no speed difference between the two on my machine.
Are you using functions, or macros? I'm sure that Peter
was referring to the use of macros, in C  *not* C++, for
the benefit of those reading in c.l.c++.
So we have, without the usual parentheses since they'd just
clutter the example:
#define min(a,b) ((a < b)? a: b)
#define min3_inefficient(a,b,c) min(a, min(b, c))
#define min3_better(a,b,c) (a < b)? min(a, c): min(b, c)
min3_inefficient(X, Y, Z) becomes
min(X, ((Y < Z)? Y: Z))
((X < ((Y < Z)? Y: Z))? X: ((Y < Z)? Y: Z)))
min3_better(X, Y, Z) becomes
((X < Y)? ((X < Z)? X: Z): (Y < Z)? Y: Z)
In this case, even though both expansions contain the same
number of < and ? characters, the "inefficient" version has
poorer control flow  three conditionals are evaluated
fully 2/3 of the time, whereas with the "better" version
only two conditionals are ever executed.
<C++ only>
With C++ template functions, of course, it doesn't matter
so much. But if you're interested in efficiency, why are
you using functions? ;)
</C++>
Arthur  
P: n/a

Irrwahn Grausewitz <ir*****@freenet.de> writes: "Jeff" <no*****@notexist.com> wrote:Please do not post offtopic things here. It waste the bandwidth and time.
Erm, (almost) the whole thread is crossposted between c.l.c and c.l.c++
So any proposed solutions should work in both C and C++.

"...deficient support can be a virtue.
It keeps the amateurs off."
Bjarne Stroustrup  
P: n/a

Ben Pfaff <bl*@cs.stanford.edu> wrote: Irrwahn Grausewitz <ir*****@freenet.de> writes:
"Jeff" <no*****@notexist.com> wrote: >Please do not post offtopic things here. It waste the bandwidth and time.
Erm, (almost) the whole thread is crossposted between c.l.c and c.l.c++
So any proposed solutions should work in both C and C++.
And one who is crossposting complaints about offtopicality of
crossposted replies to crossposted questions should make clear
what the word "here" is referring to in this context.

What does this red button do?  
P: n/a

"Arthur J. O'Dwyer" <aj*@andrew.cmu.edu> wrote in message news:Pine.LNX.4.55L Are you using functions, or macros? I'm sure that Peter was referring to the use of macros, in C  *not* C++, for the benefit of those reading in c.l.c++.
I used inline functions. It's even worse if he used macros as
the expressions passed are possibly evaluated more than once.
In this case, even though both expansions contain the same number of < and ? characters, the "inefficient" version has poorer control flow  three conditionals are evaluated fully 2/3 of the time, whereas with the "better" version only two conditionals are ever executed.
It's beyond me what you mean by that. Both times execute
the comparison twice, as a result the select. You can make
up your own idea of the cost of the flow between the two, but
as I said, it;s negligable. Both functions on the Sun compiler
take the same amount of time.
<C++ only> With C++ template functions, of course, it doesn't matter so much. But if you're interested in efficiency, why are you using functions? ;) </C++>
Templates have no bearing on this situation at all. You could
as easily code this as functions taking doubles without templates
and the answer would be the same.  
P: n/a

"Arthur J. O'Dwyer" <aj*@andrew.cmu.edu> wrote in message
news:Pi************************************@unix49 .andrew.cmu.edu...
(snip) Are you using functions, or macros? I'm sure that Peter was referring to the use of macros, in C  *not* C++, for the benefit of those reading in c.l.c++.
So we have, without the usual parentheses since they'd just clutter the example:
#define min(a,b) ((a < b)? a: b)
#define min3_inefficient(a,b,c) min(a, min(b, c))
#define min3_better(a,b,c) (a < b)? min(a, c): min(b, c)
min3_inefficient(X, Y, Z) becomes
min(X, ((Y < Z)? Y: Z))
((X < ((Y < Z)? Y: Z))? X: ((Y < Z)? Y: Z)))
min3_better(X, Y, Z) becomes
((X < Y)? ((X < Z)? X: Z): (Y < Z)? Y: Z)
In this case, even though both expansions contain the same number of < and ? characters, the "inefficient" version has poorer control flow  three conditionals are evaluated fully 2/3 of the time, whereas with the "better" version only two conditionals are ever executed.
Good compilers do common subexpression elimination, and hopefully will
notice that (Y<Z) occurs twice.
In any hand optimization problem, the question is always what will the
compiler find at want won't it find.
 glen  
P: n/a

On Wed, 10 Sep 2003, Ron Natalie wrote: "Arthur J. O'Dwyer" <aj*@andrew.cmu.edu> wrote... Are you using functions, or macros? I'm sure that Peter was referring to the use of macros, in C  *not* C++, for the benefit of those reading in c.l.c++. I used inline functions. It's even worse if he used macros as the expressions passed are possibly evaluated more than once.
True; however, he did mention the *usual* definition of 'min',
which IME is more often than not as a macro. In this case, even though both expansions contain the same number of < and ? characters, the "inefficient" version has poorer control flow  three conditionals are evaluated fully 2/3 of the time, whereas with the "better" version only two conditionals are ever executed.
It's beyond me what you mean by that. Both times execute the comparison twice, as a result the select.
Brain fart? :)
Unsnipped from my previous post:
min3_inefficient(X, Y, Z) becomes
min(X, ((Y < Z)? Y: Z))
((X < ((Y < Z)? Y: Z))? X: ((Y < Z)? Y: Z)))
min3_better(X, Y, Z) becomes
((X < Y)? ((X < Z)? X: Z): (Y < Z)? Y: Z)
Suppose X is the least of the three... then
min3_inefficient compares Y to Z
then compares X to min(Y,Z)
min3_better compares X to Y
then compares X to Z
Suppose Y is the least of the three... then
min3_inefficient compares Y to Z
then compares X to Y
then compares Y to Z
min3_better compares X to Y
then compares Y to Z
Suppose Z is the least of the three... then
min3_inefficient compares Y to Z
then compares X to Z
then compares Y to Z
min3_better compares X to Y
then compares min(X,Y) to Z
See? min3_better makes exactly 2 comparisons in each case,
while min3_inefficient makes 3 comparisons fully 2/3 of the
time, as I said. Thus min3_inefficient *is* less efficient,
because it computes useless comparisons.
You can make up your own idea of the cost of the flow between the two, but as I said, it;s negligable. Both functions on the Sun compiler take the same amount of time.
Try running them more than once.
(But before doing that, take a look at the generated machine
code. I would not be surprised if the GCC folks have optimized
this particular computation, so the Sun folks might have too.) <C++ only> With C++ template functions, of course, it doesn't matter so much. But if you're interested in efficiency, why are you using functions? ;) </C++>
Templates have no bearing on this situation at all. You could as easily code this as functions taking doubles without templates and the answer would be the same.
....except in the case where you wanted to compare pointers.
Or C++ std::strings. Or 'long longs'. Or practically anything
that wasn't a double.
(Which, incidentally, is why the most common implementation of
'min' is as a macro  because C macros can do things that C
functions can't.)
Arthur  
P: n/a

"Arthur J. O'Dwyer" <aj*@andrew.cmu.edu> wrote in message news:Pi***********************************@unix45. andrew.cmu.edu... On Wed, 10 Sep 2003, Ron Natalie wrote: "Arthur J. O'Dwyer" <aj*@andrew.cmu.edu> wrote... Are you using functions, or macros? I'm sure that Peter was referring to the use of macros, in C  *not* C++, for the benefit of those reading in c.l.c++. I used inline functions. It's even worse if he used macros as the expressions passed are possibly evaluated more than once.
True; however, he did mention the *usual* definition of 'min', which IME is more often than not as a macro.
Maybe it is in C, but it certainly IS NOT in C++. See? min3_better makes exactly 2 comparisons in each case, while min3_inefficient makes 3 comparisons fully 2/3 of the time, as I said. Thus min3_inefficient *is* less efficient, because it computes useless comparisons.
It is for the ugly macros, as I was saying, for inline functions it does
not need to generate a bogus third test.  
P: n/a

Ron Natalie wrote: "Arthur J. O'Dwyer" <aj*@andrew.cmu.edu> wrote in message news:Pi***********************************@unix45. andrew.cmu.edu... On Wed, 10 Sep 2003, Ron Natalie wrote: "Arthur J. O'Dwyer" <aj*@andrew.cmu.edu> wrote... > Are you using functions, or macros? I'm sure that Peter > was referring to the use of macros, in C  *not* C++, for > the benefit of those reading in c.l.c++.
I used inline functions. It's even worse if he used macros as the expressions passed are possibly evaluated more than once.
True; however, he did mention the *usual* definition of 'min', which IME is more often than not as a macro.
Maybe it is in C, but it certainly IS NOT in C++.
The following macro is a paradigm in C:
#define max(a, b) ((a) > (b) ? (a) : (b))

pete  
P: n/a

Arthur J. O'Dwyer wrote: On Wed, 10 Sep 2003, Ron Natalie wrote: "Peter Nilsson" <ai***@acay.com.au> wrote... > x = min( a, min( b, c ) );
Given a typical definition of min(), that will not produce an efficient result.
#define min3(a,b,c) \ ( (a) < (b) ? min(a,c) : min(b,c) )
The typical min is inlined to something like a < b ? a : b
There's always two comparisons. Tests show no speed difference between the two on my machine.
Are you using functions, or macros? I'm sure that Peter was referring to the use of macros, in C  *not* C++, for the benefit of those reading in c.l.c++.
So we have, without the usual parentheses since they'd just clutter the example:
#define min(a,b) ((a < b)? a: b)
It's illogical to remove required parentheses
and then insert extra one's that you like.
Minimally but correctly parenthesized:
#define min(a, b) ((b) > (a) ? a : (b))

pete  
P: n/a

[Removed c.l.c++ crosspost]
On Thu, 11 Sep 2003, pete wrote: Arthur J. O'Dwyer wrote: On Wed, 10 Sep 2003, Ron Natalie wrote: > > > x = min( a, min( b, c ) );
The typical min is inlined to something like a < b ? a : b
So we have, without the usual parentheses since they'd just clutter the example:
#define min(a,b) ((a < b)? a: b)
It's illogical to remove required parentheses and then insert extra one's that you like. Minimally but correctly parenthesized:
#define min(a, b) ((b) > (a) ? a : (b))
Well, the usual parentheses would just have cluttered the
example. (Remember, I was trying to display the control
flow succinctly, not show how nicely I could mimic the
preprocessor by hand!) I had originally removed the outer
parentheses as well, but then realized that that would
have produced an incorrect expansion. So I had to keep
them.
Your version is indeed the correct way to parenthesize
the macro in the general case, although I'd throw an
extra pair of parentheses around the second 'a', too,
just for paranoia's sake.
Arthur  
P: n/a

Jirka Klaue wrote: Irrwahn Grausewitz wrote: Steve Zimmerman <st******@sonic.net> wrote:Sona wrote:
I need to find a minimum of three float values [...]
Why not make use of the standard C library? Code modified:
#include <stdio.h> #include <math.h> /* for fminf() */
Is this in C89, too? If not, the standard C library provides qsort. It is not *that* efficient for 3 values, but in return it's scalable.
No, it's not. Taking the minimum of N values takes time proportional to
O(N). Sorting them takes O(Nlog(N)). Try:
#include <stdlib.h>
float fmin(size_t n, float *values) {
float min = values[0];
size_t i;
for(i = 1; i < n; i++){
if(values[i] < min)
min = values[i];
}
return min
}

Nonsequiturs make me eat lampshades.  
P: n/a

bd wrote: Jirka Klaue wrote:
.... Is this in C89, too? If not, the standard C library provides qsort. It is not *that* efficient for 3 values, but in return it's scalable.
No, it's not.
It's not what? Not scalable? Sure it is.
Jirka   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 2511
 replies: 34
 date asked: Nov 13 '05
