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 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
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;
}
[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.
"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
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
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*****@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. 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: 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...
|
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,...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |