C++

# How to Use fmax() Function in C++

A variety of programs, including games, graphics, web servers, and more, can be made using the C++ programming language. However, sometimes we may need to perform some mathematical operations on the data in our programs, such as finding the maximum or minimum value among two or more numbers. For this purpose, we can use the mathematical functions provided by the C++ standard library.

The mathematical functions are predefined functions that are declared in the <cmath> header file.  One of the mathematical functions that can be used to find the maximum value among two numbers is fmax().

## What is fmax() Function in C++

The fmax() function in C++ is a function that takes two arguments and returns the larger number among them. When either of the arguments is NaN (indicating it’s not a number), the function will output the other argument. However, if both arguments turn out to be NaN, then the function will return NaN and the syntax of using fmax() function is as follows:

#include <cmath>

fmax(int/double/float a, int/double/float b);

Here variable a and b are the two numbers that this function will compare.

## How to Use fmax() Function in C++

The fmax() function can be used to find the maximum value among two numbers in various situations. For example, we can use it to compare two user inputs, two random numbers or two array elements, two function results, here are some examples of how to use fmax() function in C++:

## Example 1: Compare Two User Inputs

Here is an example code that will use the fmax() function to compare two numbers entered by the user and displays the larger one in the output:

#include <iostream>

using namespace std;

#include <cmath>

int main()

{

double a;

double b;

//input the numbers

cout<<"Enter the first number : ";

cin>>a;

cout<<"Enter the second number: ";

cin>>b;// declare two double variables

cout<<endl;

return 0;

}

First the program defines the necessary libraries and variables and then it prompts user input for two numbers. Next the fmax() function compares the two numbers given by the user and then displays the result: ## Example 2: Compare Two Numbers with different data types

Here is an example code that will use the fmax() function to compare two random numbers generated by the rand() function and display the larger one on the screen:

#include <iostream>

using namespace std;

#include <cmath>

int main()

{

float a = 78.69;

int b = 95;

cout << "fmax(a, b) = " << answer<< endl;

return 0;

}

First the program defines the necessary libraries and variables and then it compares two variables, one with float data type and other with integer data type. Next the fmax() function compares the two numbers given by the user and then displays the result: ## Conclusion

The fmax() function is a mathematical operation that picks out the greater value between two provided arguments. It does not need to convert the floating-point numbers to integers, which can be a time-consuming operation. This makes the fmax() function a good choice for performance-critical applications. 