std::fmin,s...f,std::fminl(3)
π std::fmin, std::fminf, std::fminl π
okay so bestie, we're in the C++ Standard Library manual today, and this page is std::fmin, std::fminf, std::fminl (section 3). it's literally the "give me the smaller one, but be emotionally mature about NaN" page and honestly it ate π
β¨ NAME β¨
std::fmin, std::fminf, std::fminl β and that's it, that's the name: std::fmin,std::fminf,std::fminl. no notes, no cap π
πΈ Synopsis πΈ
<div>defined in header <code><cmath></code> π</div>
here's the og trio, and yes all three of these are in play until C++23:
float fmin ( float x, float y );
double fmin ( double x, double y );
long double fmin ( long double x, long double y );
then (1), the glow-up version β since C++23 β¨:
constexpr /* floating-point-type */
fmin ( /* floating-point-type */ x,
/* floating-point-type */ y );
that's (1), she's constexpr, she's flexible, she's the main character:
float fminf( float x, float y );β (2) (since C++11), and constexpr since C++23 π¦long double fminl( long double x, long double y );β (3) (since C++11), also constexpr since C++23 πΈ
Additional overloads (since C++11) β also defined in header <cmath>:
template< class Integer >
double fmin ( Integer x, Integer y );
that one is (A), and she's constexpr since C++23 π«
now the tea β β 1β3) return the smaller of two floating point arguments, and they treat NaNs as missing data (so if it's between a NaN and a numeric value, the numeric value wins, obviously, the numeric value is the whole point π).
since C++23, the library provides overloads of std::fmin for all cv-unqualified floating-point types as the type of the parameters. slay.
A) additional overloads are provided for all integer types, which are treated as double (since C++11). periodt.
π§ Parameters π§
x,yβ floating-point or integer values π«Ά
β Return value β
if it goes well, you get the smaller of the two floating point values. and the value returned is exact β it does not depend on any rounding modes. no rounding shenanigans, we don't do that here π
π Error handling π
this function is not subject to any of the error conditions specified in math_errhandling. she's unbothered, moisturised, in her lane β¨
if the implementation supports IEEE floating-point arithmetic (IEC 60559), then:
- if one of the two arguments is NaN, the value of the other argument is returned.
- only if both arguments are NaN, NaN is returned.
arguably the most emotionally intelligent behaviour in the whole standard library ngl π
π¦ Notes π¦
heads up: this function is not required to be sensitive to the sign of zero β although some implementations additionally enforce that if one argument is +0 and the other is -0, then -0 is returned. so don't come for her if -0 doesn't show up, it's permitted, not promised π
the additional overloads are not required to be provided exactly as (A). they only need to be sufficient to make sure that, for their first argument num1 and second argument num2:
- if
num1ornum2has typelong double, thenstd::fmin(num1, num2)has the same effect asstd::fmin(static_cast<long double>(num1), static_cast<long double>(num2)). - otherwise, if
num1and/ornum2has typedoubleor an integer type, thenstd::fmin(num1, num2)has the same effect asstd::fmin(static_cast<double>(num1), static_cast<double>(num2))β (until C++23). - otherwise, if
num1ornum2has typefloat, thenstd::fmin(num1, num2)has the same effect asstd::fmin(static_cast<float>(num1), static_cast<float>(num2)).
and (since C++23) β if num1 and num2 have arithmetic types, then std::fmin(num1, num2) has the same effect as:
std::fmin(static_cast</* common-floating-point-type */>(num1),
static_cast</* common-floating-point-type */>(num2))
where /* common-floating-point-type */ is the floating-point type with the greatest floating-point conversion rank and greatest floating-point conversion subrank between the types of num1 and num2. oh and arguments of integer type are considered to have the same floating-point conversion rank as double π
lowkey important caveat bestie: if no such floating-point type with the greatest rank and subrank exists, then overload resolution does not result in a usable candidate from the overloads provided. that's it, no candidate, we go home π
π Example π
// Run this code
#include <cmath>
#include <iostream>
int main()
{
std::cout << "fmin(2,1) = " << std::fmin(2, 1) << '\n'
<< "fmin(-Inf,0) = " << std::fmin(-INFINITY, 0) << '\n'
<< "fmin(NaN,-1) = " << std::fmin(NAN, -1) << '\n';
}
possible output: π₯
fmin(2,1) = 1
fmin(-Inf,0) = -inf
fmin(NaN,-1) = -1
look at that NaN result β -1 β she chose the numeric value, exactly like we said. iconique π§
π See also π
islessβ checks if the first floating-point argument is less than the second (C++11) (function)fmax,fmaxf,fmaxlβ larger of two floating-point values (C++11) (function) (C++11) (C++11) β yes she's the bigger sister πminβ returns the smaller of the given values (function template)min_elementβ returns the smallest element in a range (function template)minmax(C++11) β returns the smaller and larger of two elements (function template)minmax_element(C++11) β returns the smallest and the largest elements in a range (function template)- C documentation for
fminβ for when you want the same vibe but in C π
okay that's the whole page, you're officially fluent in std::fmin bestie, go pick the smaller number with confidence πβ¨
courtesy of http://cppreference.com, dated 2024.06.10 β page std::fmin,s...f,std::fminl(3). mwah π«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms