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.
double fmin(double x, double y);
float fmin(float x, float y);
long double fmin(long double x, long double y);
The fmin()
function takes two parameter values, x
and y
, which represent the first and second arguments of the function, respectively.
The fmin()
function returns the minimum value among the two arguments (x
and y
) passed to the function.
#include <iostream>#include <cmath>using namespace std;int main(){double x = -2.05, y = -3.5, result;// using the fmin() functionresult = fmin(x, y);cout << "fmax(x, y) = " << result << endl;return 0;}
We can see from the code above that the fmin()
function correctly returns -3.5
as the smaller value when compared to -2.05
.
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
.
#include <iostream>#include <cmath>using namespace std;int main(){// using different argument typesdouble x = 89.1, result;int y = 89;result = fmin(x, y);cout << "fmax(89.1, 89) = " << result << endl;return 0;}
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
).