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
13,495 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
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
1,034 views
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
857 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
1,020 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
860 views