C++ std::fmin() Function

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

Definition

In C++, std::fmin(x, y) returns the smaller of two decimal numbers. fmin(3.0, 7.5) is 3. Its partner std::fmax() returns the bigger one.

Like fmax(), it ignores NaN. If one value is NaN, fmin() returns the other, so a missing value does not spoil the minimum. Together, fmin() and fmax() can limit a number to a range.

std::fmin() 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::fmin. Start with these lines.

C++
double a = std::fmin(3.0, 7.5);          // 3
double b = std::fmin(-2.0, -5.0);        // -5
double c = std::fmin(std::nan(""), 4.0); // 4 (NaN is ignored)
CallWhat it does
std::fmin(3.0, 7.5)Returns the smaller value, 3.
std::fmin(-2.0, -5.0)For negatives, the one further from zero is smaller. Returns -5.
std::fmin(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       fmin(float x, float y);
double      fmin(double x, double y);
long double fmin(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
Smaller valueThe smaller 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 Smaller of Two Numbers

Print fmin() for positive, negative, equal, and small numbers.

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

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

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

How It Works

  1. The loop gives each pair to std::fmin(), which returns the smaller number.
  2. fmin(3, 7.5) is 3. For negative numbers, -5 is smaller than -2. Equal numbers give the same number back.
  3. fmin(0.001, 0.5) returns 0.001. A small positive number is still bigger than any negative number, but smaller than 0.5.

Example 2 — Lowest Price with a Missing Value

Four shops list a price, but one shop has no price (NaN). fmin() skips the missing one.

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

int main() {
    double prices[] = {12.99, std::nan(""), 9.5, 11.25};
    double lowest = std::numeric_limits<double>::infinity();

    for (double p : prices) {
        lowest = std::fmin(lowest, p);
    }
    std::cout << "Lowest price: " << lowest << '\n';
    return 0;
}

How It Works

  1. The start value infinity is bigger than any real price, so the first real price becomes the lowest.
  2. When the loop reaches the NaN, fmin(lowest, NaN) returns lowest and the missing shop is ignored.
  3. The final lowest price is 9.5. With std::min() a NaN in the wrong place could give a NaN answer.

Example 3 — Limit a Value to a Range

Combine fmin() and fmax() to keep a number between 0 and 10.

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

int main() {
    double low = 0.0;
    double high = 10.0;

    for (double x : {-5.0, 5.0, 15.0}) {
        double limited = std::fmax(low, std::fmin(high, x));
        std::cout << x << " -> " << limited << '\n';
    }
    return 0;
}

How It Works

  1. fmin(high, x) cuts a number down to at most 10. fmax(low, ...) then lifts it to at least 0.
  2. -5 is cut by fmin to -5 (it is below 10) and lifted by fmax to 0. The value 5 passes through unchanged.
  3. 15 is cut down to 10 by fmin, and fmax leaves it there. In C++17 std::clamp() does the same job.

Frequently Asked Questions

std::fmin(x, y) returns the smaller of x and y. If one of them is NaN, it returns the other one, so NaN is ignored.
The smaller of x and y. If one argument is NaN, the other argument is returned, and if both are NaN the result is NaN.
fmin() is made for decimals and ignores NaN. std::min() works with any type that has <, but a NaN can give a NaN answer. For integers use std::min().
Use std::fmax(low, std::fmin(high, x)), or std::clamp(x, low, high) in C++17. Both keep the number between low and high.
Include <cmath> and write std::fmin(). That always works and picks the C++ overloads. The global name ::fmin often works too, but the standard does not promise it, and it may only take a double.

Next: fmod()

Keep going through the <cmath> functions. Remainder of a division of two decimal numbers.

fmod() 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.

12 people found this page helpful