What is the fmin() function in C++?

Overview

The fmin() function in C++ is used to return the smaller number amongst two arguments.

The fmin() function is defined in the <cmath> header file.

Syntax

double fmin(double x, double y);
float fmin(float x, float y);
long double fmin(long double x, long double y);

Parameters

The fmin() function takes two parameter values, x and y, which represent the first and second arguments of the function, respectively.

Return value

The fmin() function returns the minimum value among the two arguments (x and y) passed to the function.

Example

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = -2.05, y = -3.5, result;
// using the fmin() function
result = fmin(x, y);
cout << "fmax(x, y) = " << result << endl;
return 0;
}

Code explanation

We can see from the code above that the fmin() function correctly returns -3.5 as the smaller value when compared to -2.05.

Different types of arguments

We can also use the fmin() function to compute the smaller value between arguments of different data types, such as a double and an int.

Example

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// using different argument types
double x = 89.1, result;
int y = 89;
result = fmin(x, y);
cout << "fmax(89.1, 89) = " << result << endl;
return 0;
}

Code explanation

We can see from the code above that the fmin() function correctly returns the int (89) as the smaller value when compared to the double (89.1).