Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::numeric_limits

      From cppreference.com
      <cpp‎ |types
       
       
      Utilities library
       
       
      std::numeric_limits
      Static constants
      Static member functions
      Helper types
       
      Defined in header<limits>
      template<class T>class numeric_limits;

      Thestd::numeric_limits class template provides a standardized way to query various properties of arithmetic types (e.g. the largest possible value for typeint isstd::numeric_limits<int>::max()).

      This information is provided via specializations of thestd::numeric_limits template. Thestandard library makes available specializations for all arithmetic types (only lists the specializations for cv-unqualified arithmetic types):

      Defined in header<limits>
      template<>class numeric_limits<bool>;
      template<>class numeric_limits<char>;
      template<>class numeric_limits<signedchar>;
      template<>class numeric_limits<unsignedchar>;
      template<>class numeric_limits<wchar_t>;
      template<>class numeric_limits<char8_t>;
      (since C++20)
      template<>class numeric_limits<char16_t>;
      (since C++11)
      template<>class numeric_limits<char32_t>;
      (since C++11)
      template<>class numeric_limits<short>;
      template<>class numeric_limits<unsignedshort>;
      template<>class numeric_limits<int>;
      template<>class numeric_limits<unsignedint>;
      template<>class numeric_limits<long>;
      template<>class numeric_limits<unsignedlong>;
      template<>class numeric_limits<longlong>;
      (since C++11)
      template<>class numeric_limits<unsignedlonglong>;
      (since C++11)
      template<>class numeric_limits<float>;
      template<>class numeric_limits<double>;
      template<>class numeric_limits<longdouble>;

      The value of each member of a specialization ofstd::numeric_limits on a cv-qualified typecvT is equal to the value of the corresponding member of the specialization on the unqualified typeT. For example,std::numeric_limits<int>::digits is equal tostd::numeric_limits<constint>::digits.

      Aliases of arithmetic types (such asstd::size_t orstd::streamsize) may also be examined with thestd::numeric_limits type traits.

      Non-arithmetic standard types, such asstd::complex<T> orstd::nullptr_t, do not have specializations.

      If the implementation defines anyinteger-class types, specializations ofstd::numeric_limits must also be provided for them.

      (since C++20)

      Implementations may provide specializations ofstd::numeric_limits for implementation-specific types: e.g. GCC providesstd::numeric_limits<__int128>. Non-standard libraries mayadd specializations for library-provided types, e.g.OpenEXR providesstd::numeric_limits<half> for a 16-bit floating-point type.

      Contents

      [edit]Template parameters

      T - a type to retrieve numeric properties for

      [edit]Member constants

      identifies types for whichstd::numeric_limits is specialized
      (public static member constant)[edit]
      [static]
      identifies signed types
      (public static member constant)[edit]
      [static]
      identifies integer types
      (public static member constant)[edit]
      [static]
      identifies exact types
      (public static member constant)[edit]
      [static]
      identifies floating-point types that can represent the special value "positive infinity"
      (public static member constant)[edit]
      identifies floating-point types that can represent the special value "quiet not-a-number" (NaN)
      (public static member constant)[edit]
      identifies floating-point types that can represent the special value "signaling not-a-number" (NaN)
      (public static member constant)[edit]
      [static]
      identifies the denormalization style used by the floating-point type
      (public static member constant)[edit]
      identifies the floating-point types that detect loss of precision as denormalization loss rather than inexact result
      (public static member constant)[edit]
      [static]
      identifies the rounding style used by the type
      (public static member constant)[edit]
      [static]
      identifies the IEC 559/IEEE 754 floating-point types
      (public static member constant)[edit]
      [static]
      identifies types that represent a finite set of values
      (public static member constant)[edit]
      [static]
      identifies types that handle overflows with modulo arithmetic
      (public static member constant)[edit]
      [static]
      number ofradix digits that can be represented without change
      (public static member constant)[edit]
      [static]
      number of decimal digits that can be represented without change
      (public static member constant)[edit]
      [static](C++11)
      number of decimal digits necessary to differentiate all values of this type
      (public static member constant)[edit]
      [static]
      the radix or integer base used by the representation of the given type
      (public static member constant)[edit]
      [static]
      one more than the smallest negative power of the radix that is a valid normalized floating-point value
      (public static member constant)[edit]
      the smallest negative power of ten that is a valid normalized floating-point value
      (public static member constant)[edit]
      [static]
      one more than the largest integer power of the radix that is a valid finite floating-point value
      (public static member constant)[edit]
      the largest integer power of 10 that is a valid finite floating-point value
      (public static member constant)[edit]
      [static]
      identifies types which can cause arithmetic operations to trap
      (public static member constant)[edit]
      identifies floating-point types that detect tinyness before rounding
      (public static member constant)[edit]

      [edit]Member functions

      [static]
      returns the smallest finite value of the given non-floating-point type, or the smallest positive normal value of the given floating-point type
      (public static member function)[edit]
      [static](C++11)
      returns the lowest finite value of the given type, i.e. the most negative value for signed types,0 for unsigned types
      (public static member function)[edit]
      [static]
      returns the largest finite value of the given type
      (public static member function)[edit]
      [static]
      returns the difference between1.0 and the next representable value of the given floating-point type
      (public static member function)[edit]
      [static]
      returns the maximum rounding error of the given floating-point type
      (public static member function)[edit]
      [static]
      returns the positive infinity value of the given floating-point type
      (public static member function)[edit]
      [static]
      returns a quiet NaN value of the given floating-point type
      (public static member function)[edit]
      returns a signaling NaN value of the given floating-point type
      (public static member function)[edit]
      [static]
      returns the smallest positive subnormal value of the given floating-point type
      (public static member function)[edit]

      [edit]Helper classes

      indicates floating-point rounding modes
      (enum)[edit]
      indicates floating-point denormalization modes
      (enum)[edit]

      [edit]Relationship with C library macro constants

      Specialization
      std::numeric_limits<T>
      whereT is
      Members
      min()lowest()
      (C++11)
      max()radix
      boolfalsefalsetrue2
      charCHAR_MINCHAR_MINCHAR_MAX2
      signedcharSCHAR_MINSCHAR_MINSCHAR_MAX2
      unsignedchar00UCHAR_MAX2
      wchar_tWCHAR_MINWCHAR_MINWCHAR_MAX2
      char8_t00UCHAR_MAX2
      char16_t00UINT_LEAST16_MAX2
      char32_t00UINT_LEAST32_MAX2
      shortSHRT_MINSHRT_MINSHRT_MAX2
      signedshort
      unsignedshort00USHRT_MAX2
      intINT_MININT_MININT_MAX2
      signedint
      unsignedint00UINT_MAX2
      longLONG_MINLONG_MINLONG_MAX2
      signedlong
      unsignedlong00ULONG_MAX2
      longlongLLONG_MINLLONG_MINLLONG_MAX2
      signedlonglong
      unsignedlonglong00ULLONG_MAX2
      Specialization
      std::numeric_limits<T>
      whereT is
      Members
      denorm_min()min()lowest()
      (C++11)
      max()epsilon()digitsdigits10
      floatFLT_TRUE_MINFLT_MIN-FLT_MAXFLT_MAXFLT_EPSILONFLT_MANT_DIGFLT_DIG
      doubleDBL_TRUE_MINDBL_MIN-DBL_MAXDBL_MAXDBL_EPSILONDBL_MANT_DIGDBL_DIG
      longdoubleLDBL_TRUE_MINLDBL_MIN-LDBL_MAXLDBL_MAXLDBL_EPSILONLDBL_MANT_DIGLDBL_DIG
      Specialization
      std::numeric_limits<T>
      whereT is
      Members (continue)
      min_exponentmin_exponent10max_exponentmax_exponent10radix
      floatFLT_MIN_EXPFLT_MIN_10_EXPFLT_MAX_EXPFLT_MAX_10_EXPFLT_RADIX
      doubleDBL_MIN_EXPDBL_MIN_10_EXPDBL_MAX_EXPDBL_MAX_10_EXPFLT_RADIX
      longdoubleLDBL_MIN_EXPLDBL_MIN_10_EXPLDBL_MAX_EXPLDBL_MAX_10_EXPFLT_RADIX

      [edit]Example

      Run this code
      #include <iostream>#include <limits> int main(){std::cout<<"type\t│ lowest()\t│ min()\t\t│ max()\n"<<"bool\t│ "<< std::numeric_limits<bool>::lowest()<<"\t\t│ "<< std::numeric_limits<bool>::min()<<"\t\t│ "<< std::numeric_limits<bool>::max()<<'\n'<<"uchar\t│ "<<+std::numeric_limits<unsignedchar>::lowest()<<"\t\t│ "<<+std::numeric_limits<unsignedchar>::min()<<"\t\t│ "<<+std::numeric_limits<unsignedchar>::max()<<'\n'<<"int\t│ "<< std::numeric_limits<int>::lowest()<<"\t│ "<< std::numeric_limits<int>::min()<<"\t│ "<< std::numeric_limits<int>::max()<<'\n'<<"float\t│ "<< std::numeric_limits<float>::lowest()<<"\t│ "<< std::numeric_limits<float>::min()<<"\t│ "<< std::numeric_limits<float>::max()<<'\n'<<"double\t│ "<< std::numeric_limits<double>::lowest()<<"\t│ "<< std::numeric_limits<double>::min()<<"\t│ "<< std::numeric_limits<double>::max()<<'\n';}

      Possible output:

      type│ lowest()│ min()│ max()bool│ 0│ 0│ 1uchar│ 0│ 0│ 255int│ -2147483648│ -2147483648│ 2147483647float│ -3.40282e+38│ 1.17549e-38│ 3.40282e+38double│ -1.79769e+308│ 2.22507e-308│ 1.79769e+308

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 201C++98specializations for all fundamental types need to be providedexcluded non-arithmetic types
      LWG 559C++98it was unclear whether thestd::numeric_limits
      specialization for a cv-qualified type behaves as the same as
      the corresponding specialization for the cv-unqualified type
      they have the
      same behavior

      [edit]See also

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/numeric_limits&oldid=167908"

      [8]ページ先頭

      ©2009-2025 Movatter.jp