TAking the minimum of three values

S

Sona

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
 
H

Howard

Sona said:
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
 
R

Ron Natalie

Sona said:
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));
}
 
V

Vincent Lascaux

#include said:
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
 
S

Steve Zimmerman

Sona said:
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;
}
 
I

Irrwahn Grausewitz

[cross-posting to c.l.c++ fixed]

Steve Zimmerman said:
Sona said:
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>
 
P

Peter Nilsson

Howard said:
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) )
 
M

Martin Ambuhl

Peter said:
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.
 
J

Jirka Klaue

Irrwahn said:
Steve Zimmerman said:
Sona said:
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
 
I

Irrwahn Grausewitz

Martin Ambuhl said:
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
 
C

Carsten Hansen

Sona said:
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
 
J

Jirka Klaue

Carsten said:
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
 
I

Irrwahn Grausewitz

Carsten Hansen said:
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*
 
C

Carsten Hansen

Jirka Klaue said:
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
 
J

Jeff

Ron Natalie said:
In C++

#include <algoritm>

double min3(double a, double b, double c) {
return std::min(a, std::min(b, c));
}


Please do not post off-topic things here. It waste the bandwidth and time.
 
I

Irrwahn Grausewitz

Jeff said:
Please do not post off-topic things here. It waste the bandwidth and time.

Erm, (almost) the whole thread is cross-posted between c.l.c and c.l.c++
 
J

Jirka Klaue

Carsten said:
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
 
S

Steve Zimmerman

Carsten said:
Give an example where it fails.

Carsten Hansen

Carsten,


Thank you for your post.

I don't intend this post as mean-spirited; 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
 
A

Alf P. Steinbach

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.
 
C

Carsten Hansen

Jirka Klaue said:
#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top