In C++, std::fmax(x, y) returns the bigger of two decimal numbers. fmax(3.0, 7.5) is 7.5. The matching function std::fmin() returns the smaller one.
What makes fmax() different from std::max() is how it treats NaN (not a number). If one value is NaN, fmax() simply ignores it and returns the other. That makes it a good fit for data with missing values, because a missing reading does not spoil the whole maximum.
std::fmax() is declared in <cmath> and is part of the group Remainder, scale, and compare. These functions split a number, scale it by a power of two, find its remainder, or pick between two numbers. They work directly on the binary form of a double.
Syntax
Include <cmath> and call std::fmax. Start with these lines.
C++
double a = std::fmax(3.0, 7.5); // 7.5
double b = std::fmax(-2.0, -5.0); // -2
double c = std::fmax(std::nan(""), 4.0); // 4 (NaN is ignored)
Call
What it does
std::fmax(3.0, 7.5)
Returns the bigger value, 7.5.
std::fmax(-2.0, -5.0)
For negatives, the one closer to zero is bigger. Returns -2.
std::fmax(std::nan(""), 4.0)
One argument is NaN, so the other is returned. Result: 4.
Both arguments can have different types, such as an int and a double. They are converted to a common type first.
Library declarations
C++
float fmax(float x, float y);
double fmax(double x, double y);
long double fmax(long double x, long double y);
// plus overloads for mixed and integer arguments
Parameters
Parameter
Description
x
Required. The first number. It can be float, double, long double, or an integer type (converted to double).
y
Required. The second number.
Return Value
Return Value
Description
Larger value
The bigger of x and y.
The other argument
If exactly one argument is NaN, the non-NaN argument is returned.
NaN
Returned only when both arguments are NaN.
C++ Examples
Example 1 — The Larger of Two Numbers
Print fmax() for positive, negative, equal, and very different numbers.
std::nan("") makes a NaN to stand for a missing value. fmax() returns the number 4 in both orders.
std::max(a, b) is written as (a < b) ? b : a. Any comparison with NaN is false, so with NaN first it returns the NaN, and the program prints 1 for the check.
With the number first, std::max(4.0, NaN) returns 4 by luck. The answer depends on the order, which is why fmax() is safer with missing data.
Example 3 — Best Score So Far
Keep a running maximum with fmax(). Start from minus infinity so that any real score is bigger.
C++
#include <cmath>
#include <iostream>
#include <limits>
int main() {
double scores[] = {72.5, 88.0, 65.0, 91.5};
double best = -std::numeric_limits<double>::infinity();
for (double s : scores) {
best = std::fmax(best, s);
std::cout << "after " << s << ": best " << best << '\n';
}
return 0;
}
Output
after 72.5: best 72.5
after 88: best 88
after 65: best 88
after 91.5: best 91.5
How It Works
The start value -infinity is smaller than any real score, so the first score always becomes the best.
After 72.5 the best is 72.5, and 88 beats it. The score 65 is lower, so the best stays 88.
91.5 is the new best, so the last line shows 91.5. If a score were NaN, fmax() would skip it and keep the old best.
Frequently Asked Questions
std::fmax(x, y) returns the larger of x and y. If one of them is NaN, it returns the other one, so NaN is ignored.
The larger of x and y. If one argument is NaN, the other argument is returned, and if both are NaN the result is NaN.
fmax() is made for decimals and ignores NaN. std::max() works with any type that has <, such as int and string, and gives an unreliable answer when NaN is involved. For integers use std::max().
Use std::max({a, b, c}) with a braced list from <algorithm>, std::max_element() on a range, or call fmax() in a loop. fmax() itself takes only two arguments.
Include <cmath> and write std::fmax(). That always works and picks the C++ overloads. The global name ::fmax often works too, but the standard does not promise it, and it may only take a double.