c++ Numeric limits

目录

Numeric limits

ptrdiff_t


Numeric limits

Limits of core language integer types

Defined in header <limits.h>

BOOL_WIDTH

(C23)

bit width of _Bool
(macro constant)

CHAR_BIT

number of bits in a byte
(macro constant)

MB_LEN_MAX

maximum number of bytes in a multibyte character
(macro constant)

CHAR_WIDTH

(C23)

bit width of char, same as CHAR_BIT
(macro constant)

CHAR_MIN

minimum value of char
(macro constant)

CHAR_MAX

maximum value of char
(macro constant)

SCHAR_WIDTHSHRT_WIDTHINT_WIDTHLONG_WIDTHLLONG_WIDTH

(C23)(C23)(C23)(C23)(C23)

bit width of signed char, short, int, long, and long long respectively
(macro constant)

SCHAR_MINSHRT_MININT_MINLONG_MINLLONG_MIN

(C99)

minimum value of signed char, short, int, long and long long respectively
(macro constant)

SCHAR_MAXSHRT_MAXINT_MAXLONG_MAXLLONG_MAX

(C99)

maximum value of signed char, short, int, long and long long respectively
(macro constant)

UCHAR_WIDTHUSHRT_WIDTHUINT_WIDTHULONG_WIDTHULLONG_WIDTH

(C23)(C23)(C23)(C23)(C23)

bit width of unsigned char, unsigned short, unsigned int, unsigned long, and unsigned long long respectively
(macro constant)

UCHAR_MAXUSHRT_MAXUINT_MAXULONG_MAXULLONG_MAX

(C99)

maximum value of unsigned char, unsigned short, unsigned int,
unsigned long and unsigned long long respectively
(macro constant)

Limits of library type aliases

Defined in header <stdint.h>

PTRDIFF_WIDTH

(C23)

bit width of object of ptrdiff_t type
(macro constant)

PTRDIFF_MIN

(C99)

minimum value of object of ptrdiff_t type
(macro constant)

PTRDIFF_MAX

(C99)

maximum value of object of ptrdiff_t type
(macro constant)

SIZE_WIDTH

(C23)

bit width of object of size_t type
(macro constant)

SIZE_MAX

(C99)

maximum value of object of size_t type
(macro constant)

SIG_ATOMIC_WIDTH

(C23)

bit width of object of sig_atomic_t type
(macro constant)

SIG_ATOMIC_MIN

(C99)

minimum value of object of sig_atomic_t type
(macro constant)

SIG_ATOMIC_MAX

(C99)

maximum value of object of sig_atomic_t type
(macro constant)

WINT_WIDTH

(C23)

bit width of object of wint_t type
(macro constant)

WINT_MIN

(C99)

minimum value of object of wint_t type
(macro constant)

WINT_MAX

(C99)

maximum value of object of wint_t type
(macro constant)

Defined in header <wchar.h>

Defined in header <stdint.h>

WCHAR_WIDTH

(C23)

bit width of object of wchar_t type
(macro constant)

WCHAR_MIN

(C99)

minimum value of object of wchar_t type
(macro constant)

WCHAR_MAX

(C99)

maximum value of object of wchar_t type
(macro constant)

Notes

The types of these constants, other than CHAR_BIT and MB_LEN_MAX, are required to match the results of the integral promotions as applied to objects of the types they describe: CHAR_MAX may have type int or unsigned int, but never char. Similarly USHRT_MAX may not be of an unsigned type: its type may be int.

A freestanding implementation may lack sig_atomic_t and/or wint_t typedef names, in which case the SIG_ATOMIC_* and/or WINT_* macros are correspondingly absent.

Example

Run this code

#include <stdio.h>
#include <limits.h>
#include <stdint.h>
 
int main(void)
{   
    printf("CHAR_BIT       = %d\n", CHAR_BIT);
    printf("MB_LEN_MAX     = %d\n\n", MB_LEN_MAX);
 
    printf("CHAR_MIN       = %+d\n", CHAR_MIN);
    printf("CHAR_MAX       = %+d\n", CHAR_MAX);
    printf("SCHAR_MIN      = %+d\n", SCHAR_MIN);
    printf("SCHAR_MAX      = %+d\n", SCHAR_MAX);
    printf("UCHAR_MAX      = %u\n\n", UCHAR_MAX);
 
    printf("SHRT_MIN       = %+d\n", SHRT_MIN);
    printf("SHRT_MAX       = %+d\n", SHRT_MAX);
    printf("USHRT_MAX      = %u\n\n", USHRT_MAX);
 
    printf("INT_MIN        = %+d\n", INT_MIN);
    printf("INT_MAX        = %+d\n", INT_MAX);
    printf("UINT_MAX       = %u\n\n", UINT_MAX);
 
    printf("LONG_MIN       = %+ld\n", LONG_MIN);
    printf("LONG_MAX       = %+ld\n", LONG_MAX);
    printf("ULONG_MAX      = %lu\n\n", ULONG_MAX);
 
    printf("LLONG_MIN      = %+lld\n", LLONG_MIN);
    printf("LLONG_MAX      = %+lld\n", LLONG_MAX);
    printf("ULLONG_MAX     = %llu\n\n", ULLONG_MAX);
 
    printf("PTRDIFF_MIN    = %td\n", PTRDIFF_MIN);
    printf("PTRDIFF_MAX    = %+td\n", PTRDIFF_MAX);
    printf("SIZE_MAX       = %zu\n", SIZE_MAX);
    printf("SIG_ATOMIC_MIN = %+jd\n",(intmax_t)SIG_ATOMIC_MIN);
    printf("SIG_ATOMIC_MAX = %+jd\n",(intmax_t)SIG_ATOMIC_MAX);
    printf("WCHAR_MIN      = %+jd\n",(intmax_t)WCHAR_MIN);
    printf("WCHAR_MAX      = %+jd\n",(intmax_t)WCHAR_MAX);
    printf("WINT_MIN       = %jd\n", (intmax_t)WINT_MIN);
    printf("WINT_MAX       = %jd\n", (intmax_t)WINT_MAX);
}

Possible output:

CHAR_BIT       = 8
MB_LEN_MAX     = 16
 
CHAR_MIN       = -128
CHAR_MAX       = +127
SCHAR_MIN      = -128
SCHAR_MAX      = +127
UCHAR_MAX      = 255
 
SHRT_MIN       = -32768
SHRT_MAX       = +32767
USHRT_MAX      = 65535
 
INT_MIN        = -2147483648
INT_MAX        = +2147483647
UINT_MAX       = 4294967295
 
LONG_MIN       = -9223372036854775808
LONG_MAX       = +9223372036854775807
ULONG_MAX      = 18446744073709551615
 
LLONG_MIN      = -9223372036854775808
LLONG_MAX      = +9223372036854775807
ULLONG_MAX     = 18446744073709551615
 
PTRDIFF_MIN    = -9223372036854775808
PTRDIFF_MAX    = +9223372036854775807
SIZE_MAX       = 18446744073709551615
SIG_ATOMIC_MIN = -2147483648
SIG_ATOMIC_MAX = +2147483647
WCHAR_MIN      = -2147483648
WCHAR_MAX      = +2147483647
WINT_MIN       = 0
WINT_MAX       = 4294967295

Limits of floating point types

Defined in header <float.h>

FLT_RADIX

the radix (integer base) used by the representation of all three floating-point types
(macro constant)

DECIMAL_DIG

(C99)

conversion from long double to decimal with at least DECIMAL_DIG digits and back to long double is the identity conversion: this is the decimal precision required to serialize/deserialize a long double
(macro constant)

FLT_DECIMAL_DIGDBL_DECIMAL_DIGLDBL_DECIMAL_DIG

(C11)

conversion from float/double/long double to decimal with at least FLT_DECIMAL_DIG/DBL_DECIMAL_DIG/LDBL_DECIMAL_DIG digits and back is the identity conversion: this is the decimal precision required to serialize/deserialize a floating point value. Defined to at least 6, 10, and 10 respectively, or 9 for IEEE float and 17 for IEEE double. (see also the C++ analog max_digits10)
(macro constant)

FLT_MINDBL_MINLDBL_MIN

minimum, normalized, positive value of float, double and long double respectively
(macro constant)

FLT_TRUE_MINDBL_TRUE_MINLDBL_TRUE_MIN

(C11)

minimum positive value of float, double and long double respectively
(macro constant)

FLT_MAXDBL_MAXLDBL_MAX

maximum finite value of float, double and long double respectively
(macro constant)

FLT_EPSILONDBL_EPSILONLDBL_EPSILON

difference between 1.0 and the next representable value for float, double and long double respectively
(macro constant)

FLT_DIGDBL_DIGLDBL_DIG

number of decimal digits that are guaranteed to be preserved in text -> float/double/long double -> text roundtrip without change due to rounding or overflow (see the C++ analog digits10 for detail)
(macro constant)

FLT_MANT_DIGDBL_MANT_DIGLDBL_MANT_DIG

number of base-FLT_RADIX digits that are in the floating-point mantissa and that can be represented without losing precision for float, double and long double respectively
(macro constant)

FLT_MIN_EXPDBL_MIN_EXPLDBL_MIN_EXP

minimum negative integer such that FLT_RADIX raised by power one less than that integer is a normalized float, double and long double respectively
(macro constant)

FLT_MIN_10_EXPDBL_MIN_10_EXPLDBL_MIN_10_EXP

minimum negative integer such that 10 raised by power one less than that integer is a normalized float, double and long double respectively
(macro constant)

FLT_MAX_EXPDBL_MAX_EXPLDBL_MAX_EXP

maximum positive integer such that FLT_RADIX raised by power one less than that integer is a representable finite float, double and long double respectively
(macro constant)

FLT_MAX_10_EXPDBL_MAX_10_EXPLDBL_MAX_10_EXP

maximum positive integer such that 10 raised by power one less than that integer is a representable finite float, double and long double respectively
(macro constant)

FLT_ROUNDS

rounding mode of floating-point arithmetics
(macro constant)

FLT_EVAL_METHOD

(C99)

use of extended precision for intermediate results: 0 not used, 1 double is used instead of float, 2: long double is used
(macro constant)

FLT_HAS_SUBNORMDBL_HAS_SUBNORMLDBL_HAS_SUBNORM

(C11)

whether the type supports subnormal (denormal) numbers: -1 indeterminable, 0 absent, 1 present
(macro constant)

Example

Run this code

#include <stdio.h>
#include <float.h> 
#include <math.h>
 
int main(void)
{
    printf("DECIMAL_DIG     = %d\n", DECIMAL_DIG);
    printf("FLT_DECIMAL_DIG = %d\n", FLT_DECIMAL_DIG);
    printf("FLT_RADIX       = %d\n", FLT_RADIX);
    printf("FLT_MIN         = %e\n", FLT_MIN);
    printf("FLT_MAX         = %e\n", FLT_MAX);
    printf("FLT_EPSILON     = %e\n", FLT_EPSILON);
    printf("FLT_DIG         = %d\n", FLT_DIG);
    printf("FLT_MANT_DIG    = %d\n", FLT_MANT_DIG);
    printf("FLT_MIN_EXP     = %d\n", FLT_MIN_EXP);
    printf("FLT_MIN_10_EXP  = %d\n", FLT_MIN_10_EXP);
    printf("FLT_MAX_EXP     = %d\n", FLT_MAX_EXP);
    printf("FLT_MAX_10_EXP  = %d\n", FLT_MAX_10_EXP);
    printf("FLT_ROUNDS      = %d\n", FLT_ROUNDS);
    printf("FLT_EVAL_METHOD = %d\n", FLT_EVAL_METHOD);
    printf("FLT_HAS_SUBNORM = %d\n", FLT_HAS_SUBNORM);
}

Possible output:

DECIMAL_DIG     = 37
FLT_DECIMAL_DIG = 9
FLT_RADIX       = 2
FLT_MIN         = 1.175494e-38
FLT_MAX         = 3.402823e+38
FLT_EPSILON     = 1.192093e-07
FLT_DIG         = 6
FLT_MANT_DIG    = 24
FLT_MIN_EXP     = -125
FLT_MIN_10_EXP  = -37
FLT_MAX_EXP     = 128
FLT_MAX_10_EXP  = 38
FLT_ROUNDS      = 1
FLT_EVAL_METHOD = 1
FLT_HAS_SUBNORM = 1

ptrdiff_t

Defined in header <stddef.h>

typedef /*implementation-defined*/ ptrdiff_t;

ptrdiff_t is the signed integer type of the result of subtracting two pointers.

The bit width of ptrdiff_t is not less than 17.

(since C99)

Notes

ptrdiff_t is used for pointer arithmetic and array indexing, if negative values are possible. Programs that use other types, such as int, may fail on, e.g. 64-bit systems when the index exceeds INT_MAX or if it relies on 32-bit modular arithmetic.

Only pointers to elements of the same array (including the pointer one past the end of the array) may be subtracted from each other.

If an array is so large (greater than PTRDIFF_MAX elements, but less than SIZE_MAX bytes), that the difference between two pointers may not be representable as ptrdiff_t, the result of subtracting two such pointers is undefined.

For char arrays shorter than PTRDIFF_MAXptrdiff_t acts as the signed counterpart of size_t: it can store the size of the array of any type and is, on most platforms, synonymous with intptr_t).

Example

#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
 
int main(void)
{
    const size_t N = 100;
    int numbers[N];
 
    printf("PTRDIFF_MAX = %ld\n", PTRDIFF_MAX);
    int *p1=&numbers[18], *p2=&numbers[23];
    ptrdiff_t diff = p2-p1;
    printf("p2-p1 = %td\n", diff);
 
    return 0;
}

Possible output:

PTRDIFF_MAX = 9223372036854775807
p2-p1 = 5

size_t

unsigned integer type returned by the sizeof operator
(typedef)

offsetof

byte offset from the beginning of a struct type to specified member
(function macro)

C++ documentation for ptrdiff_t

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值