Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

functions
c++
communitycreator

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

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The fmax() function in C++ is used to return the larger number among two arguments (x and y) passed to the function.

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

Syntax

double fmax (double x, double y);
float fmax (float x, float y);
long double fmax (long double x, long double y);
Syntax

Parameter value

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

Return value

The fmax() function returns the larger value among the x and y values.

Example

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

Code explanation

We can see from the code above that the fmax() function correctly returned -2.05 as the larger value when compared to -3.5.

Using different types of argument

We can also use the fmax() function to compute for the larger number 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 types
double x = 89.1, result;
int y = 89;
result = fmax(x, y);
cout << "fmax(89.1, 89) = " << result << endl;
return 0;
}
Implementing the fmax() function for different argument types

Code explanation

We can see from the code above that the fmax() correctly returned the double (89.1) as the larger value when compared to the int (89).

RELATED TAGS

functions
c++
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring