18 Language support library [language.support]

18.3 Implementation properties [support.limits]

18.3.2 Numeric limits [limits]

18.3.2.1 Class template numeric_limits [limits.numeric]

The numeric_limits class template provides a C++ program with information about various properties of the implementation's representation of the arithmetic types.

Specializations shall be provided for each arithmetic type, both floating point and integer, including bool. The member is_specialized shall be true for all such specializations of numeric_limits.

For all members declared static constexpr in the numeric_limits template, specializations shall define these values in such a way that they are usable as constant expressions.

Non-arithmetic standard types, such as complex<T> ([complex]), shall not have specializations.

18.3.2.2 Header <limits> synopsis [limits.syn]

namespace std {
  template<class T> class numeric_limits;
  enum float_round_style;
  enum float_denorm_style;

  template<> class numeric_limits<bool>;

  template<> class numeric_limits<char>;
  template<> class numeric_limits<signed char>;
  template<> class numeric_limits<unsigned char>;
  template<> class numeric_limits<char16_t>;
  template<> class numeric_limits<char32_t>;
  template<> class numeric_limits<wchar_t>;

  template<> class numeric_limits<short>;
  template<> class numeric_limits<int>;
  template<> class numeric_limits<long>;
  template<> class numeric_limits<long long>;
  template<> class numeric_limits<unsigned short>;
  template<> class numeric_limits<unsigned int>;
  template<> class numeric_limits<unsigned long>;
  template<> class numeric_limits<unsigned long long>;

  template<> class numeric_limits<float>;
  template<> class numeric_limits<double>;
  template<> class numeric_limits<long double>;
}

18.3.2.3 Class template numeric_limits [numeric.limits]

namespace std {
  template<class T> class numeric_limits {
  public:
    static constexpr bool is_specialized = false;
    static constexpr T min() noexcept { return T(); }
    static constexpr T max() noexcept { return T(); }
    static constexpr T lowest() noexcept { return T(); }

    static constexpr int  digits = 0;
    static constexpr int  digits10 = 0;
    static constexpr int  max_digits10 = 0;
    static constexpr bool is_signed = false;
    static constexpr bool is_integer = false;
    static constexpr bool is_exact = false;
    static constexpr int  radix = 0;
    static constexpr T epsilon() noexcept { return T(); }
    static constexpr T round_error() noexcept { return T(); }

    static constexpr int  min_exponent = 0;
    static constexpr int  min_exponent10 = 0;
    static constexpr int  max_exponent = 0;
    static constexpr int  max_exponent10 = 0;

    static constexpr bool has_infinity = false;
    static constexpr bool has_quiet_NaN = false;
    static constexpr bool has_signaling_NaN = false;
    static constexpr float_denorm_style has_denorm = denorm_absent;
    static constexpr bool has_denorm_loss = false;
    static constexpr T infinity() noexcept { return T(); }
    static constexpr T quiet_NaN() noexcept { return T(); }
    static constexpr T signaling_NaN() noexcept { return T(); }
    static constexpr T denorm_min() noexcept { return T(); }

    static constexpr bool is_iec559 = false;
    static constexpr bool is_bounded = false;
    static constexpr bool is_modulo = false;

    static constexpr bool traps = false;
    static constexpr bool tinyness_before = false;
    static constexpr float_round_style round_style = round_toward_zero;
  };

  template<class T> class numeric_limits<const T>;
  template<class T> class numeric_limits<volatile T>;
  template<class T> class numeric_limits<const volatile T>;
}

The default numeric_limits<T> template shall have all members, but with 0 or false values.

The value of each member of a specialization of numeric_limits on a cv-qualified type cv T shall be equal to the value of the corresponding member of the specialization on the unqualified type T.

18.3.2.4 numeric_limits members [numeric.limits.members]

static constexpr T min() noexcept;

Minimum finite value.197

For floating types with denormalization, returns the minimum positive normalized value.

Meaningful for all specializations in which is_bounded != false, or is_bounded == false && is_signed == false.

static constexpr T max() noexcept;

Maximum finite value.198

Meaningful for all specializations in which is_bounded != false.

static constexpr T lowest() noexcept;

A finite value x such that there is no other finite value y where y < x.199

Meaningful for all specializations in which is_bounded != false.

static constexpr int digits;

Number of radix digits that can be represented without change.

For integer types, the number of non-sign bits in the representation.

For floating point types, the number of radix digits in the mantissa.200

static constexpr int digits10;

Number of base 10 digits that can be represented without change.201

Meaningful for all specializations in which is_bounded != false.

static constexpr int max_digits10;

Number of base 10 digits required to ensure that values which differ are always differentiated.

Meaningful for all floating point types.

static constexpr bool is_signed;

True if the type is signed.

Meaningful for all specializations.

static constexpr bool is_integer;

True if the type is integer.

Meaningful for all specializations.

static constexpr bool is_exact;

True if the type uses an exact representation. All integer types are exact, but not all exact types are integer. For example, rational and fixed-exponent representations are exact but not integer.

Meaningful for all specializations.

static constexpr int radix;

For floating types, specifies the base or radix of the exponent representation (often 2).202

For integer types, specifies the base of the representation.203

Meaningful for all specializations.

static constexpr T epsilon() noexcept;

Machine epsilon: the difference between 1 and the least value greater than 1 that is representable.204

Meaningful for all floating point types.

static constexpr T round_error() noexcept;

Measure of the maximum rounding error.205

static constexpr int min_exponent;

Minimum negative integer such that radix raised to the power of one less than that integer is a normalized floating point number.206

Meaningful for all floating point types.

static constexpr int min_exponent10;

Minimum negative integer such that 10 raised to that power is in the range of normalized floating point numbers.207

Meaningful for all floating point types.

static constexpr int max_exponent;

Maximum positive integer such that radix raised to the power one less than that integer is a representable finite floating point number.208

Meaningful for all floating point types.

static constexpr int max_exponent10;

Maximum positive integer such that 10 raised to that power is in the range of representable finite floating point numbers.209

Meaningful for all floating point types.

static constexpr bool has_infinity;

True if the type has a representation for positive infinity.

Meaningful for all floating point types.

Shall be true for all specializations in which is_iec559 != false.

static constexpr bool has_quiet_NaN;

True if the type has a representation for a quiet (non-signaling) “Not a Number.”210

Meaningful for all floating point types.

Shall be true for all specializations in which is_iec559 != false.

static constexpr bool has_signaling_NaN;

True if the type has a representation for a signaling “Not a Number.”211

Meaningful for all floating point types.

Shall be true for all specializations in which is_iec559 != false.

static constexpr float_denorm_style has_denorm;

denorm_present if the type allows denormalized values (variable number of exponent bits)212, denorm_absent if the type does not allow denormalized values, and denorm_indeterminate if it is indeterminate at compile time whether the type allows denormalized values.

Meaningful for all floating point types.

static constexpr bool has_denorm_loss;

True if loss of accuracy is detected as a denormalization loss, rather than as an inexact result.213

static constexpr T infinity() noexcept;

Representation of positive infinity, if available.214

Meaningful for all specializations for which has_infinity != false. Required in specializations for which is_iec559 != false.

static constexpr T quiet_NaN() noexcept;

Representation of a quiet “Not a Number,” if available.215

Meaningful for all specializations for which has_quiet_NaN != false. Required in specializations for which is_iec559 != false.

static constexpr T signaling_NaN() noexcept;

Representation of a signaling “Not a Number,” if available.216

Meaningful for all specializations for which has_signaling_NaN != false. Required in specializations for which is_iec559 != false.

static constexpr T denorm_min() noexcept;

Minimum positive denormalized value.217

Meaningful for all floating point types.

In specializations for which has_denorm == false, returns the minimum positive normalized value.

static constexpr bool is_iec559;

True if and only if the type adheres to IEC 559 standard.218

Meaningful for all floating point types.

static constexpr bool is_bounded;

True if the set of values representable by the type is finite.219Note: All fundamental types ([basic.fundamental]) are bounded. This member would be false for arbitrary precision types. — end note ]

Meaningful for all specializations.

static constexpr bool is_modulo;

True if the type is modulo.220 A type is modulo if, for any operation involving +, -, or * on values of that type whose result would fall outside the range [min(),max()], the value returned differs from the true value by an integer multiple of max() - min() + 1.

On most machines, this is false for floating types, true for unsigned integers, and true for signed integers.

Meaningful for all specializations.

static constexpr bool traps;

true if, at program startup, there exists a value of the type that would cause an arithmetic operation using that value to trap.221

Meaningful for all specializations.

static constexpr bool tinyness_before;

true if tinyness is detected before rounding.222

Meaningful for all floating point types.

static constexpr float_round_style round_style;

The rounding style for the type.223

Meaningful for all floating point types. Specializations for integer types shall return round_toward_zero.

Equivalent to CHAR_MIN, SHRT_MIN, FLT_MIN, DBL_MIN, etc.

Equivalent to CHAR_MAX, SHRT_MAX, FLT_MAX, DBL_MAX, etc.

lowest() is necessary because not all floating-point representations have a smallest (most negative) value that is the negative of the largest (most positive) finite value.

Equivalent to FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIG.

Equivalent to FLT_DIG, DBL_DIG, LDBL_DIG.

Equivalent to FLT_RADIX.

Distinguishes types with bases other than 2 (e.g. BCD).

Equivalent to FLT_EPSILON, DBL_EPSILON, LDBL_EPSILON.

Rounding error is described in ISO/IEC 10967-1 Language independent arithmetic - Part 1 Section 5.2.8 and Annex A Rationale Section A.5.2.8 - Rounding constants.

Equivalent to FLT_MIN_EXP, DBL_MIN_EXP, LDBL_MIN_EXP.

Equivalent to FLT_MIN_10_EXP, DBL_MIN_10_EXP, LDBL_MIN_10_EXP.

Equivalent to FLT_MAX_EXP, DBL_MAX_EXP, LDBL_MAX_EXP.

Equivalent to FLT_MAX_10_EXP, DBL_MAX_10_EXP, LDBL_MAX_10_EXP.

Required by LIA-1.

Required by LIA-1.

Required by LIA-1.

See IEC 559.

Required by LIA-1.

Required by LIA-1.

Required by LIA-1.

Required by LIA-1.

International Electrotechnical Commission standard 559 is the same as IEEE 754.

Required by LIA-1.

Required by LIA-1.

Required by LIA-1.

Refer to IEC 559. Required by LIA-1.

Equivalent to FLT_ROUNDS. Required by LIA-1.

18.3.2.5 Type float_round_style [round.style]

namespace std {
  enum float_round_style {
    round_indeterminate       = -1,
    round_toward_zero         =  0,
    round_to_nearest          =  1,
    round_toward_infinity     =  2,
    round_toward_neg_infinity =  3
  };
}

The rounding mode for floating point arithmetic is characterized by the values:

  • round_indeterminate if the rounding style is indeterminable

  • round_toward_zero if the rounding style is toward zero

  • round_to_nearest if the rounding style is to the nearest representable value

  • round_toward_infinity if the rounding style is toward infinity

  • round_toward_neg_infinity if the rounding style is toward negative infinity

18.3.2.6 Type float_denorm_style [denorm.style]

namespace std {
  enum float_denorm_style {
    denorm_indeterminate = -1,
    denorm_absent = 0,
    denorm_present = 1
  };
}

The presence or absence of denormalization (variable number of exponent bits) is characterized by the values:

  • denorm_indeterminate if it cannot be determined whether or not the type allows denormalized values

  • denorm_absent if the type does not allow denormalized values

  • denorm_present if the type does allow denormalized values

18.3.2.7 numeric_limits specializations [numeric.special]

All members shall be provided for all specializations. However, many values are only required to be meaningful under certain conditions (for example, epsilon() is only meaningful if is_integer is false). Any value that is not “meaningful” shall be set to 0 or false.

Example:

namespace std {
  template<> class numeric_limits<float> {
  public:
    static constexpr bool is_specialized = true;

    inline static constexpr float min() noexcept { return 1.17549435E-38F; }
    inline static constexpr float max() noexcept { return 3.40282347E+38F; }
    inline static constexpr float lowest() noexcept { return -3.40282347E+38F; }

    static constexpr int digits   = 24;
    static constexpr int digits10 =  6;
    static constexpr int max_digits10 =  9;

    static constexpr bool is_signed  = true;
    static constexpr bool is_integer = false;
    static constexpr bool is_exact   = false;

    static constexpr int radix = 2;
    inline static constexpr float epsilon() noexcept     { return 1.19209290E-07F; }
    inline static constexpr float round_error() noexcept { return 0.5F; }

    static constexpr int min_exponent   = -125;
    static constexpr int min_exponent10 = - 37;
    static constexpr int max_exponent   = +128;
    static constexpr int max_exponent10 = + 38;

    static constexpr bool has_infinity             = true;
    static constexpr bool has_quiet_NaN            = true;
    static constexpr bool has_signaling_NaN        = true;
    static constexpr float_denorm_style has_denorm = denorm_absent;
    static constexpr bool has_denorm_loss          = false;

    inline static constexpr float infinity()      noexcept { return value; }
    inline static constexpr float quiet_NaN()     noexcept { return value; }
    inline static constexpr float signaling_NaN() noexcept { return value; }
    inline static constexpr float denorm_min()    noexcept { return min(); }

    static constexpr bool is_iec559  = true;
    static constexpr bool is_bounded = true;
    static constexpr bool is_modulo  = false;
    static constexpr bool traps      = true;
    static constexpr bool tinyness_before = true;

    static constexpr float_round_style round_style = round_to_nearest;
  };
}

 — end example ]

The specialization for bool shall be provided as follows:

namespace std {
   template<> class numeric_limits<bool> {
   public:
     static constexpr bool is_specialized = true;
     static constexpr bool min() noexcept { return false; }
     static constexpr bool max() noexcept { return true; }
     static constexpr bool lowest() noexcept { return false; }

     static constexpr int  digits = 1;
     static constexpr int  digits10 = 0;
     static constexpr int  max_digits10 = 0;

     static constexpr bool is_signed = false;
     static constexpr bool is_integer = true;
     static constexpr bool is_exact = true;
     static constexpr int  radix = 2;
     static constexpr bool epsilon() noexcept { return 0; }
     static constexpr bool round_error() noexcept { return 0; }

     static constexpr int  min_exponent = 0;
     static constexpr int  min_exponent10 = 0;
     static constexpr int  max_exponent = 0;
     static constexpr int  max_exponent10 = 0;

     static constexpr bool has_infinity = false;
     static constexpr bool has_quiet_NaN = false;
     static constexpr bool has_signaling_NaN = false;
     static constexpr float_denorm_style has_denorm = denorm_absent;
     static constexpr bool has_denorm_loss = false;
     static constexpr bool infinity() noexcept { return 0; }
     static constexpr bool quiet_NaN() noexcept { return 0; }
     static constexpr bool signaling_NaN() noexcept { return 0; }
     static constexpr bool denorm_min() noexcept { return 0; }

     static constexpr bool is_iec559 = false;
     static constexpr bool is_bounded = true;
     static constexpr bool is_modulo = false;

     static constexpr bool traps = false;
     static constexpr bool tinyness_before = false;
     static constexpr float_round_style round_style = round_toward_zero;
   };
}