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)
Call
What 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
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
Smaller value
The smaller 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 Smaller of Two Numbers
Print fmin() for positive, negative, equal, and small numbers.
The start value infinity is bigger than any real price, so the first real price becomes the lowest.
When the loop reaches the NaN, fmin(lowest, NaN) returns lowest and the missing shop is ignored.
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;
}
Output
-5 -> 0
5 -> 5
15 -> 10
How It Works
fmin(high, x) cuts a number down to at most 10. fmax(low, ...) then lifts it to at least 0.
-5 is cut by fmin to -5 (it is below 10) and lifted by fmax to 0. The value 5 passes through unchanged.
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.