Namespaces
Variants

std::numeric_limits<T>::min

来自 zh.cppreference.net
 
 
 
类型支持
基本类型
定宽整数类型 (C++11)
定宽浮点类型 (C++23)
(C++11)    
(C++17)
数值极限
C 数值极限接口
运行时类型信息
 
 
定义于头文件 <limits>
static T min() throw();
(直到 C++11)
static constexpr T min() noexcept;
(自 C++11 起)

返回数值类型 T 所能表示的最小有限值。

对于支持非规格化数的浮点类型,min() 返回最小正规格化值。请注意此行为可能出乎意料,尤其是与min()对整数类型的行为比较时。若要寻找没有任何值小于它的值,请使用lowest()。(自 C++11 起)

min()仅对有界类型和无界无符号类型有意义。

返回值

T std:: numeric_limits < T > :: min ( )
/* 非特化版本 */ T ( )
bool false
char CHAR_MIN
signed char SCHAR_MIN
unsigned char ​ 0 ​
wchar_t WCHAR_MIN
char8_t (C++20 起) ​ 0 ​
char16_t (C++11 起) ​ 0 ​
char32_t (C++11 起) ​ 0 ​
short SHRT_MIN
unsigned short ​ 0 ​
int INT_MIN
unsigned int ​ 0 ​
long LONG_MIN
unsigned long ​ 0 ​
long long (C++11 起) LLONG_MIN
unsigned long long (C++11 起) ​ 0 ​
float FLT_MIN
double DBL_MIN
long double LDBL_MIN

示例

演示了typedef类型的使用,以及整数类型与浮点类型在结果符号上的差异:

#include <cstddef>
#include <iomanip>
#include <iostream>
#include <limits>
// we want to print char types as an integer without leading Fs
auto p(auto x) { return x; }
auto p(char x) { return x & static_cast<unsigned char>(-1); }
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 << p(min);
    std::cout << '\n';
}
#define SHOW(T) print_one<T>(#T)
int main()
{
    SHOW(bool);
    SHOW(char);
    SHOW(unsigned char);
    SHOW(short);
    SHOW(unsigned short);
    SHOW(signed);
    SHOW(unsigned);
    SHOW(std::ptrdiff_t);
    SHOW(std::size_t);
    SHOW(float);
    SHOW(double);
    SHOW(long double);
}

可能的输出:

          bool ( 1 bytes): 0
          char ( 1 bytes): -128 or 0x80
 unsigned char ( 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

参见

[static] (C++11)
返回给定类型的最小有限值,即对于有符号类型为最负值,对于无符号类型为 ​ 0 ​
(公开静态成员函数)
[static]
返回给定浮点类型的最小正次正规值
(公开静态成员函数)
[static]
返回给定类型的最大有限值
(公开静态成员函数)