std::numeric_limits::min(3)
β¨ std::numeric_limits::min β¨
C++ Standard Library page for std::numeric_limits::min(3) β welcome to the group chat, bestie π«Ά. This oneβs about the minimum finite value T can represent, and the floating-point plot twist is very real.
πΈ NAME πΈ
std::numeric_limits::min β std::numeric_limits::min
π Synopsis π
Defined in header <limits>
static T min() throw(); (until C++11)
static constexpr T min() noexcept; (since C++11)
Okay so min() returns the minimum finite value representable by the numeric type T. Thatβs the whole assignment π
.
For floating-point types with denormalization, min() returns the minimum positive normalized value. Note that this behavior may be unexpected, especially when compared to the behavior of min() for integral types. To find the value that has no values less than it, use lowest(). (since C++11)
min() is only meaningful for bounded types and for unbounded unsigned types, that is, types that represent an infinite set of negative values have no meaningful minimum.
π Return value π
T on the left, std::numeric_limits<T>::min() on the right:
/* non-specialized */βT()boolβfalsecharβCHAR_MINsigned charβSCHAR_MINunsigned charβ0wchar_tβWCHAR_MINchar8_t(since C++20) β0char16_t(since C++11) β0char32_t(since C++11) β0shortβSHRT_MINunsigned shortβ0intβINT_MINunsigned intβ0longβLONG_MINunsigned longβ0long long(since C++11) βLLONG_MINunsigned long long(since C++11) β0floatβFLT_MINdoubleβDBL_MINlong doubleβLDBL_MIN
π¦ Example π¦
This demo shows use with typedef types, and the difference in the sign of the result between integer and floating-point types:
// Run this code
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <limits>
template <typename T>
void print_one(std::string_view type_name)
{
constexpr T min = std::numeric_limits<T>::min();
std::cout
<< std::dec << std::defaultfloat << std::setw(14) << type_name
<< " (" << std::setw(2) << sizeof(T) << " bytes): " << min;
if constexpr (min != 0)
std::cout << " or " << std::showbase << std::hex <<
std::hexfloat << min;
std::cout << '\n';
}
#define SHOW(T) print_one<T>(#T)
int main()
{
SHOW(bool);
SHOW(short);
SHOW(unsigned short);
SHOW(signed);
SHOW(unsigned);
SHOW(std::ptrdiff_t);
SHOW(std::size_t);
SHOW(float);
SHOW(double);
SHOW(long double);
}
Possible output:
bool ( 1 bytes): 0
short ( 2 bytes): -32768 or 0x8000
unsigned short ( 2 bytes): 0
signed ( 4 bytes): -2147483648 or 0x80000000
unsigned ( 4 bytes): 0
std::ptrdiff_t ( 8 bytes): -9223372036854775808 or 0x8000000000000000
std::size_t ( 8 bytes): 0
float ( 4 bytes): 1.17549e-38 or 0x1p-126
double ( 8 bytes): 2.22507e-308 or 0x1p-1022
long double (16 bytes): 3.3621e-4932 or 0x8p-16385
π See also π
lowestβ returns the lowest finite value of the given type[static](C++11) (public static member function)
denorm_minβ returns the smallest positive subnormal value of the given floating-point type[static](public static member function)
maxβ returns the largest finite value of the given type[static](public static member function)
Thatβs the tea π« β source/version: http://cppreference.com, date: 2024.06.10. 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