Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of min and max functions in C++

Tags:

c++

c

max

min

From C++, are std::min and std::max preferable over fmin and fmax? For comparing two integers, do they provide basically the same functionality?

Do you tend to use one of these sets of functions or do you prefer to write your own (perhaps to improve efficiency, portability, flexibility, etc.)?

Notes:

  1. The C++ Standard Template Library (STL) declares the min and max functions in the standard C++ algorithm header.

  2. The C standard (C99) provides the fmin and fmax function in the standard C math.h header.

Thanks in advance!

like image 571
bporter Avatar asked Oct 27 '09 16:10

bporter


People also ask

What is the use of max and min function?

The MAX and MIN functions are two such functions. The MAX function allows you to find the highest number in given range. The MIN function does the opposite, providing you with the lowest number in a defined range.

What kind of functions are MIN and MAX in C?

What kind of functions are min and max in c++? Explanation: The min/max functions are type specific but they will not force everything to be converted to/from floating point. The functions that will force everything to be converted to/from floating point are fmin/fmax. 3.

Can we use MAX function in C?

Say max() function is used to find maximum between two numbers. Second, we need to find maximum between two numbers. Hence, the function must accept two parameters of int type say, max(int num1, int num2). Finally, the function should return maximum among given two numbers.

What is the difference between MAX () and MIN () with example?

Minimum means the least you can do of something. For example, if the minimum amount of dollars you must pay for something is seven, then you cannot pay six dollars or less (you must pay at least seven). You can do more than the minimum, but no less. Maximum means the most you can have of something.


1 Answers

fmin and fmax are specifically for use with floating point numbers (hence the "f"). If you use it for ints, you may suffer performance or precision losses due to conversion, function call overhead, etc. depending on your compiler/platform.

std::min and std::max are template functions (defined in header <algorithm>) which work on any type with a less-than (<) operator, so they can operate on any data type that allows such a comparison. You can also provide your own comparison function if you don't want it to work off <.

This is safer since you have to explicitly convert arguments to match when they have different types. The compiler won't let you accidentally convert a 64-bit int into a 64-bit float, for example. This reason alone should make the templates your default choice. (Credit to Matthieu M & bk1e)

Even when used with floats the template may win in performance. A compiler always has the option of inlining calls to template functions since the source code is part of the compilation unit. Sometimes it's impossible to inline a call to a library function, on the other hand (shared libraries, absence of link-time optimization, etc.).

like image 95
Cogwheel Avatar answered Oct 13 '22 22:10

Cogwheel