std::numeric_limits 介绍

本文介绍了C++标准库中的std::numeric_limits模板类,详细讲解了其提供的获取数值类型特性(如最小值、最大值、精度等)以及判断类型特性的成员函数和静态成员常量。
摘要由CSDN通过智能技术生成

std::numeric_limits 是 C++ 标准库中的一个模板类,定义在 <limits> 头文件中,用于提供各种数值类型的特性信息,如最小值、最大值、精度等。

以下是 std::numeric_limits 提供的一些常用成员函数和静态成员常量:

  1. 最大最小值

    • std::numeric_limits<T>::min():返回类型 T 的最小值。
    • std::numeric_limits<T>::max():返回类型 T 的最大值。
  2. 精度信息

    • std::numeric_limits<T>::digits:返回类型 T 的有效位数(二进制位数)。
    • std::numeric_limits<T>::digits10:返回类型 T 的有效位数(十进制位数)。
    • std::numeric_limits<T>::epsilon():返回类型 T 的最小精度,即相邻可表示的最小差值。
  3. 表示范围

    • std::numeric_limits<T>::lowest():返回类型 T 的最小有限值(负无穷)。
    • std::numeric_limits<T>::infinity():返回类型 T 的正无穷值。
    • std::numeric_limits<T>::quiet_NaN():返回类型 T 的静默 NaN(Not-a-Number)值。
  4. 类型属性

    • std::numeric_limits<T>::is_signed:返回一个布尔值,表示类型 T 是否是有符号类型。
    • std::numeric_limits<T>::is_integer:返回一个布尔值,表示类型 T 是否是整数类型。
    • std::numeric_limits<T>::is_exact:返回一个布尔值,表示类型 T 是否是精确表示的类型。
#include <iostream>
#include <limits>

int main() {
    std::cout << "Minimum value of int: " << std::numeric_limits<int>::min() << std::endl;
    std::cout << "Maximum value of int: " << std::numeric_limits<int>::max() << std::endl;
    std::cout << "Number of digits in int: " << std::numeric_limits<int>::digits10 << std::endl;
    std::cout << "Epsilon value of int: " << std::numeric_limits<int>::epsilon() << std::endl;
    std::cout << "Is int signed: " << std::boolalpha << std::numeric_limits<int>::is_signed << std::endl;
    std::cout << "Is int integer: " << std::boolalpha << std::numeric_limits<int>::is_integer << std::endl;
    std::cout << "Is int exact: " << std::boolalpha << std::numeric_limits<int>::is_exact << std::endl;

    return 0;
}

std::numeric_limits<int>::digits10std::numeric_limits 模板类的一个静态成员常量,它用于获取类型 int 的十进制位数(即有效位数)。

std::numeric_limits<int>::epsilon()std::numeric_limits 模板类的一个静态成员函数,用于获取类型 int 的最小精度。在 C++ 中,整数类型不具有可表示的精度,因此对于整数类型 intstd::numeric_limits<int>::epsilon() 返回的值始终为 0。

std::boolalpha<iomanip> 头文件中定义的一个标记,用于设置流输出时,将布尔值解释为字符串 "true" 或 "false",而不是整数 1 或 0。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值