C++ std::fmax() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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)
CallWhat 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

ParameterDescription
xRequired. The first number. It can be float, double, long double, or an integer type (converted to double).
yRequired. The second number.

Return Value

Return ValueDescription
Larger valueThe bigger of x and y.
The other argumentIf exactly one argument is NaN, the non-NaN argument is returned.
NaNReturned 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.

C++
#include <cmath>
#include <iostream>

int main() {
    double pairs[][2] = {{3, 7.5}, {-2, -5}, {4, 4}, {1e10, 2}};

    for (auto& p : pairs) {
        std::cout << "fmax(" << p[0] << ", " << p[1] << ") = "
                  << std::fmax(p[0], p[1]) << '\n';
    }
    return 0;
}

How It Works

  1. The loop gives each pair to std::fmax(), which returns the larger number.
  2. fmax(3, 7.5) is 7.5. For two negative numbers, -2 is bigger than -5. Equal numbers give the same number back.
  3. A big value such as 1e10 wins easily and prints in scientific form, 1e+10, because cout only shows six digits.

Example 2 — fmax() vs std::max() with NaN

See what happens when one value is missing (NaN). std::max() depends on the order, and fmax() does not.

C++
#include <algorithm>
#include <cmath>
#include <iostream>

int main() {
    double missing = std::nan("");

    std::cout << "fmax(NaN, 4) = " << std::fmax(missing, 4.0) << '\n';
    std::cout << "fmax(4, NaN) = " << std::fmax(4.0, missing) << '\n';
    std::cout << "std::max(NaN, 4) is NaN: " << std::isnan(std::max(missing, 4.0)) << '\n';
    std::cout << "std::max(4, NaN) = " << std::max(4.0, missing) << '\n';
    return 0;
}

How It Works

  1. std::nan("") makes a NaN to stand for a missing value. fmax() returns the number 4 in both orders.
  2. 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.
  3. 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;
}

How It Works

  1. The start value -infinity is smaller than any real score, so the first score always becomes the best.
  2. After 72.5 the best is 72.5, and 88 beats it. The score 65 is lower, so the best stays 88.
  3. 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.

Next: fmin()

Keep going through the <cmath> functions. The smaller of two numbers, ignoring NaN.

fmin() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful