std::fmin,s...f,std::fminl(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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>&lt;cmath&gt;</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 num1 or num2 has type long double, then std::fmin(num1, num2) has the same effect as std::fmin(static_cast<long double>(num1), static_cast<long double>(num2)).
  • otherwise, if num1 and/or num2 has type double or an integer type, then std::fmin(num1, num2) has the same effect as std::fmin(static_cast<double>(num1), static_cast<double>(num2)) β€” (until C++23).
  • otherwise, if num1 or num2 has type float, then std::fmin(num1, num2) has the same effect as std::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