English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Usage and examples of fmin() function

C++ Library Function <cmath>

C ++adopts two parameters and returns the minimum parameter among them. If one of the parameters is NAN, it returns the other parameter.

The function in<cmath>defined in the header file.

fmin() prototype [from C ++ 11Standard begins]

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

From C ++ 11Starting from C, if the parameters passed to fmin() are of type long double, the returned type Promoted is long double. If not, the returned type Promoted is double.

fmin() parameters

  • x: The first parameter of fmin().

  • y: The second parameter of fmin().

fmin() return value

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

Example1: The fmin() function is used for parameters of the 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 running the program, the output is:

fmin(x, y) = -2.05

Example2: The fmin() function is used for different types of parameters

#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 running the program, the output is:

fmin(x, y) = 56.13

C++ Library Function <cmath>