C++ Math fmin
The function outputs the smallest value between a pair of numbers.
Conditions:
Consider two numbers 'x' and 'y'.
If a condition x is satisfied, the function returns x. If the condition x is greater than y is satisfied, the function returns y. If the condition x is NaN, the function returns y. If the condition y is NaN, the function returns x.
Syntax
float fmin(float x, float y);
double fmin(double x, double y);
long double fmin(long double x, long double y);
promoted fmin(Arithmetic x, Arithmetic y);
Note: If any argument has an integral type, then it is cast to double. If any other argument is long double, then it is cast to long double.
Parameter
(x,y): Range of values within which the calculation of the minimum value is required.
Return value
It returns the minimum value between two numbers.
Example 1
Let's see a simple example.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=1.1;
float y=2.1;
std::cout <<"Values of x and y are :"<<x<<","<<y<< std::endl;
cout<<"Minimum value is :"<<fmin(x,y);
return 0;
}
Output:
Values of x and y are :1.1,2.1
Minimum value is :1.1
In this instance, the value assigned to x is lower than that of y. Consequently, the function fmin will provide the value of x.
Example 2
Let's examine a basic scenario where one of the values is NaN.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=10.1;
double y=NAN;
std::cout <<"Values of x and y are :"<<x<<","<<y<< std::endl;
cout<<"Minimum value is :"<<fmin(x,y);
return 0;
}
Output:
Values of x and y are :10.1,nan
Minimum value is :10.1
In this scenario, the value of y is NaN. Consequently, the function returns the value of x.