Namespaces
Variants
Views
Actions

Numeric limits

From cppreference.com
< c‎ | types
Revision as of 09:07, 12 February 2014 by Newatthis (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Defined in header <stdint.h>
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_MAX
(C99)
maximum value of object of size_t type
(macro constant)
SIG_ATOMIC_MIN
(C99)
maximum 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)
WCHAR_MIN
(C99)
maximum value of object of wchar_t type
(macro constant)
WCHAR_MAX
(C99)
maximum value of object of wchar_t type
(macro constant)
WINT_MIN
(C99)
maximum value of object of wint_t type
(macro constant)
WINT_MAX
(C99)
maximum value of object of wint_t type
(macro constant)

[edit] Limits of integer types

Defined in header <limits.h>
CHAR_BIT
number of bits in byte
(macro constant)
MB_LEN_MAX
maximum number of bytes in a multibyte character
(macro constant)
CHAR_MIN
minimum value of char
(macro constant)
CHAR_MAX
maximum value of char
(macro constant)
SCHAR_MIN
SHRT_MIN
INT_MIN
LONG_MIN
LLONG_MIN
(C99)
minimum value of signed char, short, int, long and long long respectively
(macro constant)
SCHAR_MAX
SHRT_MAX
INT_MAX
LONG_MAX
LLONG_MAX
(C99)
maximum value of signed char, short, int, long and long long respectively
(macro constant)
UCHAR_MAX
USHRT_MAX
UINT_MAX
ULONG_MAX
ULLONG_MAX
(C99)
maximum value of unsigned char, unsigned short, unsigned int,
unsigned long and unsigned long long respectively
(macro constant)

[edit] 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)
number of decimal digits that can be converted to long double and back without losing precision
(macro constant)
FLT_MIN
DBL_MIN
LDBL_MIN
minimum, normalized, positive value of float, double and long double respectively
(macro constant)
FLT_MAX
DBL_MAX
LDBL_MAX
maximum value of float, double and long double respectively
(macro constant)
FLT_EPSILON
DBL_EPSILON
LDBL_EPSILON
difference between 1.0 and the next representable value for float, double and long double respectively
(macro constant)
FLT_DIG
DBL_DIG
LDBL_DIG
number of decimal digits that can be represented without losing precision for float, double and long double respectively
(macro constant)
FLT_MANT_DIG
DBL_MANT_DIG
LDBL_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_EXP
DBL_MIN_EXP
LDBL_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_EXP
DBL_MIN_10_EXP
LDBL_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_EXP
DBL_MAX_EXP
LDBL_MAX_EXP
maximum positive 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_MAX_10_EXP
DBL_MAX_10_EXP
LDBL_MAX_10_EXP
maximum positive integer such that 10 raised by power one less than that integer is a normalized float, double and long double respectively
(macro constant)
FLT_ROUNDS
rounding mode of floating-point arithmetics, equal to float_round_style
(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_SUBNORM
DBL_HAS_SUBNORM
LDBL_HAS_SUBNORM
(C11)
whether the type supports subnormal (denormal) numbers: -1 indeterminable, 0 absent, 1 present
(macro constant)

[edit] Example

#include <stdio.h>
#include <float.h> 
#include <math.h>
 
int main()
{
    /* FLT_RADIX                                                        */
    /* The value of the base, or radix, of the exponent representation. */
    /* Guaranteed to be a constant expression.                          */
    printf("FLT_RADIX = %d\n", FLT_RADIX);   /* 2 */
    printf("\n");
 
    /* DECIMAL_DIG                                     */
    printf("DECIMAL_DIG = %d\n", DECIMAL_DIG);   /* 21 */
    printf("\n");
 
    /* FLT_MIN */
    /* FLT_MAX */
    printf("FLT_MIN      = %e\n", FLT_MIN);        /* 1.175494e-38 */
    printf("FLT_MAX      = %e\n", FLT_MAX);        /* 3.402823e+38 */
    printf("\n");
 
    /* FLT_EPSILON                                                 */
    /* Adding FLT_EPSILON to 1.0F changes the binary,              */
    /* floating-point representation of 1.0F.                      */
    /* 1.0F:                 0 01111111 00000000000000000000000    */
    /* 1.0F + FLT_EPSILON:   0 01111111 00000000000000000000001    */
    printf("FLT_EPSILON  = %e\n", FLT_EPSILON);    /* 1.192093e-07 */
    printf("\n");
 
    /* FLT_DIG                                                            */
    printf("FLT_DIG      = %d\n", FLT_DIG);        /* 6 decimal digits    */
    /* According to this constant, any integral value in the range        */
    /* [-999999,+999999] can be placed into a float, then cast into a     */
    /* sufficiently large integer datatype, and cast back again without   */
    /* any loss of precision.  The result will be exactly the same as     */
    /* the original number.  999999 works, but 16777216 also survives the */
    /* transformations.  Evidentally, all 7-digit and some 8-digit values */
    /* do not suffer a loss of precision.                                 */
    float x;
    int i;
    x = 999999;
    i = x;
    printf("999999   ==> %.8g\n", (float)i);   // 999999
    /* But, test two more values: */
    x = 16777216;
    i = x;
    printf("16777216 ==> %.8g\n", (float)i);   // 16777216
    x = 16777217;
    i = x;
    printf("16777217 ==> %.8g\n", (float)i);   // 16777216, lost precision
    printf("\n");
 
    /* FLT_MANT_DIG */
    printf("FLT_MANT_DIG = %d\n", FLT_MANT_DIG);   /* 24 binary digits */
    printf("\n");
 
    /* FLT_MIN_EXP                                */
    /* Smallest possible exponent for type float. */
    /* FLT_MIN_EXP-1 ==> -125-1 ==> -126          */   
    printf("FLT_MIN_EXP = %d\n",  FLT_MIN_EXP);                              /* -125 */
    printf("isnormal(powf(2.0F,-125)) = %d\n", isnormal(powf(2.0F,-125)));   /* 1 */
    printf("isnormal(powf(2.0F,-126)) = %d\n", isnormal(powf(2.0F,-126)));   /* 1 */
    printf("isnormal(powf(2.0F,-127)) = %d\n", isnormal(powf(2.0F,-127)));   /* 0 */
    printf("\n");
 
    /* FLT_MIN_10_EXP */
    printf("FLT_MIN_10_EXP = %d\n",  FLT_MIN_10_EXP);                        /* -37 */
    printf("isnormal(powf(10.0F,-37)) = %d\n", isnormal(powf(10.0F,-37)));   /* 1   */
    printf("isnormal(powf(10.0F,-38)) = %d\n", isnormal(powf(10.0F,-38)));   /* 0   */
    printf("\n");
 
    /* FLT_MAX_EXP                                     */
    /* Largest possible exponent value for type float. */
    /* FLT_MAX_EXP-1 ==> +128-1 ==> +127 */
    printf("FLT_MAX_EXP              = %d\n",  FLT_MAX_EXP);               /* +128 */
    printf("isnormal(powf(2.0F,127)) = %d\n", isnormal(powf(2.0F,127)));   /* 1 */
    printf("isnormal(powf(2.0F,128)) = %d\n", isnormal(powf(2.0F,128)));   /* 0 */
    printf("\n");
 
    /* FLT_MAX_10_EXP */
    printf("FLT_MAX_10_EXP = %d\n",  FLT_MAX_10_EXP);                      /* +38 */
    printf("isnormal(powf(10.0F,38)) = %d\n", isnormal(powf(10.0F,38)));   /* 1   */
    printf("isnormal(powf(10.0F,39)) = %d\n", isnormal(powf(10.0F,39)));   /* 0   */
    printf("\n");
 
    /* FLT_ROUNDS                                                      */
    /* Current rounding mode.                                          */
    /* Possible values:                                                */
    /* -1    mode is indeterminable.                                   */
    /*  0    towards zero (truncation)                                 */
    /*  1    towards the nearest integer (halfway case away from zero) */
    /*  2    towards positive infinity (ceiling)                       */
    /*  3    towards negative infinity (floor)                         */
    printf("FLT_ROUNDS = %d\n",  FLT_ROUNDS);   /* 1 */
    printf("\n");
 
    /* FLT_EVAL_METHOD                                          */
    printf("FLT_EVAL_METHOD  = %d\n",  FLT_EVAL_METHOD);   /* 0 */
    printf("\n");
 
    /* FLT_HAS_SUBNORM */
    /* printf("FLT_HAS_SUBNORM  = %d\n",  FLT_HAS_SUBNORM); */
    /* printf("\n"); */
 
    return 0;
}

Output:

FLT_RADIX = 2
 
DECIMAL_DIG = 21
 
FLT_MIN      = 1.175494e-38
FLT_MAX      = 3.402823e+38
 
FLT_EPSILON  = 1.192093e-07
 
FLT_DIG      = 6
999999   ==> 999999
16777216 ==> 16777216
16777217 ==> 16777216
 
FLT_MANT_DIG = 24
 
FLT_MIN_EXP = -125
isnormal(powf(2.0F,-125)) = 1
isnormal(powf(2.0F,-126)) = 1
isnormal(powf(2.0F,-127)) = 0
 
FLT_MIN_10_EXP = -37
isnormal(powf(10.0F,-37)) = 1
isnormal(powf(10.0F,-38)) = 0
 
FLT_MAX_EXP              = 128
isnormal(powf(2.0F,127)) = 1
isnormal(powf(2.0F,128)) = 0
 
FLT_MAX_10_EXP = 38
isnormal(powf(10.0F,38)) = 1
isnormal(powf(10.0F,39)) = 0
 
FLT_ROUNDS = 1
 
FLT_EVAL_METHOD  = 0