C++ fmin()

The fmin() function in C++ takes two arguments and returns the smallest among them. If one of the argument is NaN, the other argument is returned.

The function is defined in <cmath> header file.


fmin() prototype [As of C++ 11 standard]

double fmin(double x, double y);
float fmin(float x, float y);
long double fmin(long double x, long double y);
Promoted fmin(Type1 x, Type2 y); // Additional overloads for arithmetic types

Since C++11, if any argument passed to fmin() is long double, the return type Promoted is long double. If not, the return type Promoted is double.


fmin() Parameters

  • x: The first argument of fmin().
  • y: The second argument of fmin().

fmin() Return value

The fmin() function returns the minimum value among x and y.


Example 1: fmin() function for arguments of same type

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double x = -2.05, y = NAN, result;
    
    result = fmin(x, y);
    cout << "fmin(x, y) = " << result << endl;

    return 0;
}

When you run the program, the output will be:

fmin(x, y) = -2.05

Example 2: fmin() function for arguments of different types

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double x = 56.13, result;
    int y = 89;

    result = fmin(x, y);
    cout << "fmin(x, y) = " << result << endl;

    return 0;
}

When you run the program, the output will be:

fmin(x, y) = 56.13
Did you find this article helpful?