474,060 Members | 4,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TAking the minimum of three values

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

Nov 13 '05 #1
34 2981

"Sona" <so**********@n ospam.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


Nov 13 '05 #2
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));
}
Nov 13 '05 #3
> #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
Nov 13 '05 #4
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;
}

Nov 13 '05 #5
[cross-posting 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.
Nov 13 '05 #6
"Howard" <al*****@hotmai l.com> wrote in message news:<bj******* *@dispatch.conc entric.net>...
"Sona" <so**********@n ospam.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
Nov 13 '05 #7
Peter Nilsson wrote:
"Howard" <al*****@hotmai l.com> wrote in message news:<bj******* *@dispatch.conc entric.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

Nov 13 '05 #8
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

Nov 13 '05 #9
Martin Ambuhl <ma*****@earthl ink.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.
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
9172
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
3
13072
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
0
10586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10385
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11640
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
12114
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6701
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6901
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4964
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.