Use of min and max functions in C

0 votes
Are std::min and std::max better than fmin and fmax in C++?

Do they provide essentially the same functionality when comparing two integers?

Do you prefer to use one of these libraries of functions or write your own (to improve efficiency, portability, flexibility, and so on)?

Notes:
The min and max functions are declared in the standard C++ algorithm header by the C++ Standard Template Library (STL).

The fmin and fmax functions are included in the standard C math.h header according to the C standard (C99).

Thank you very much in advance!
Jun 6, 2022 in C++ by Nicholas
• 7,760 points
9,065 views

1 answer to this question.

0 votes
The functions fmin and fmax are designed to work with floating point numbers (hence the "f").

Depending on your compiler/platform, you may experience performance or precision losses when using it for ints due to conversion, function call overhead, and other factors.

std::min and std::max are two standard variables.

are template functions (defined in the header) that work on any type that has a less-than () operator, allowing them to operate on any data type that allows a comparison.

If you don't want it to work off, you can create your own comparison function.

This is safer because when arguments have different types, you must explicitly convert them to match.

For example, the compiler will not allow you to convert a 64-bit int to a 64-bit float by mistake.

The templates should be made for this reason alone.

Even when used with floats, the template may outperform the others in terms of performance.

Because the source code is part of the compilation unit, a compiler can always inline calls to template functions.

On the other hand, in-lining a call to a library function isn't always possible (shared libraries, absence of link-time optimization, etc.).
answered Jun 21, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

functions in c++ call by value and call by reference

The code below shows how to call a function in both methods.  Please explain the major differences or meanings of call by value and call by reference to me.  1.Make a value-based call.  2.Call based on a reference.  The call by value method is demonstrated in the following code. In a comment, I expressed my reservations. #include<iostream> int main(){ void change(int);//why function prototype is before ...READ MORE

Jun 6, 2022 in C++ by Nicholas
• 7,760 points
306 views
0 votes
1 answer

How to use new[ ] and delete[ ] operator in C++

int main(){ char *str; ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
346 views
0 votes
1 answer

Use of "this" keyword in C++ [duplicate]

Yes, it is optional and generally omitted.  However, it may be essential for accessing variables after they have been overridden in the scope: Person::Person() { int age; ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
261 views
0 votes
1 answer

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
2,006 views
0 votes
0 answers

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
316 views
0 votes
0 answers

How can I get the maximum or minimum value in a vector?

In C++, how can I find the greatest or minimum value in a vector? Is it correct to assume that it would be similar with an array? Do I require an iterator?  I tried max element, but I kept receiving errors. vector<int>::const_iterator it; it = max_element(cloud.begin(), cloud.end()); error: request for ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
348 views
0 votes
2 answers

In List of Dicts, find min() value of a common Dict field

lst = [{'price': 99, 'barcode': '2342355'}, {'price': ...READ MORE

answered Aug 31, 2018 in Python by Omkar
• 69,210 points
7,224 views
0 votes
1 answer

Is there an easy way to make a min heap in C++?

Use make heap() and its buddies from algorithm>, or priority queue from queue>.  Make heap and friends are used by priority queue. #include <queue> // functional,iostream,ctime,cstdlib using namespace std; int main(int ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
658 views
0 votes
1 answer

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
448 views
0 votes
1 answer

functions in c++ call by value and call by reference

Calling a function by value copies the argument and stores it in a local variable for use by the function, so the argument is unaffected if the value of the local variable changes.  The argument is passed to the function as a reference rather than a copy, so if the function changes the value of the argument, the argument is also changed.   The void change(int); function prototype informs the compiler that there is a function named change that takes a single int argument and returns void (i.e. nothing).  Because there is no & with the argument, it is called by value.  You have the line change(orig); later in your code, which actually calls the function with. Take a look at the output of ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
416 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP