C++ std::numeric_limits

一 简介

头文件<limits>

template <class T> numeric_limits;

Provides information about the properties of arithmetic types (either integral or floating-point) in the specific platform for which the library compiles.

This class template is specialized for every fundamental arithmetic type, with its members describing the properties of type T. This template shall not be specialized for any other type.

 即:提供算术类型(整型或者浮点型)的属性信息。该类模板提供了每个基本算术类型的特化,类成员描述类型的属性,成员和成员函数均为静态;而其他类型并没有进行特化,将使用默认的模板定义

二 基本算术类型

C++98:

fundamental arithmetic types
integral typesbool
char
wchar_t
signed char
short int
int
long int
unsigned char
unsigned short int
unsigned int
unsigned long int
floating point typesfloat
double
long double

C++11:
 

fundamental arithmetic types
integral typesbool
char
char16_t
char32_t
wchar_t
signed char
short int
int
long int
long long int
unsigned char
unsigned short int
unsigned int
unsigned long int
unsigned long long int
floating point typesfloat
double
long double

三 默认模板定义

C++98:

template <class T> class numeric_limits {
public:
  static const bool is_specialized = false;
  static T min() throw();
  static T max() throw();
  static const int  digits = 0;
  static const int  digits10 = 0;
  static const bool is_signed = false;
  static const bool is_integer = false;
  static const bool is_exact = false;
  static const int radix = 0;
  static T epsilon() throw();
  static T round_error() throw();

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

  static const bool has_infinity = false;
  static const bool has_quiet_NaN = false;
  static const bool has_signaling_NaN = false;
  static const float_denorm_style has_denorm = denorm_absent;
  static const bool has_denorm_loss = false;
  static T infinity() throw();
  static T quiet_NaN() throw();
  static T signaling_NaN() throw();
  static T denorm_min() throw();

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

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

C++11:

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 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;
};

四 举例

#include <limits>
#include <iostream>

int main() {
  std::cout << "type\tlowest()\tmin()\t\tmax()\n\n";

  std::cout << "uchar\t"
            << +std::numeric_limits<unsigned char>::lowest() << '\t' << '\t'
            << +std::numeric_limits<unsigned char>::min() << '\t' << '\t'
            << +std::numeric_limits<unsigned char>::max() << '\n';
  std::cout << "int\t"
            << std::numeric_limits<int>::lowest() << '\t'
            << std::numeric_limits<int>::min() << '\t'
            << std::numeric_limits<int>::max() << '\n';
  std::cout << "float\t"
            << std::numeric_limits<float>::lowest() << '\t'
            << std::numeric_limits<float>::min() << '\t'
            << std::numeric_limits<float>::max() << '\n';
  std::cout << "double\t"
            << std::numeric_limits<double>::lowest() << '\t'
            << std::numeric_limits<double>::min() << '\t'
            << std::numeric_limits<double>::max() << '\n';

  getchar();
}

结果:

五 参考

std::numeric_limits 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值