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 34 2858
"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
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));
}
> #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
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;
}
[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.
"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
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
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
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.
"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
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
"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.
"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
"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
"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.
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
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
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.
"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
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
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
"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.
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
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
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?
"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.
"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
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
"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.
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
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
[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
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.
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 thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sona 
last post by:
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

by: Sona 
last post by:
I need to find a minimum of three float values.. what would be the most
efficient way of doing this? Can someone please share some code with me
for doing this? Thanks
Sona

by: DJRhino 
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...

by: Aliciasmith 
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...

by: tracyyun 
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...

by: giovanniandrean 
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...

by: NeoPa 
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currentlyselected (and open) object (Form or Report).
I know it can be done by selecting :...

by: Teri B 
last post by:
Hi, I have created a subform Roles. In my course form the user selects the roles assigned to the course.
0netomany. One course many roles.
Then I created a report based on the Course form and...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...

by: NeoPa 
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
 