• C++标准模板(STL)- 类型支持 (数值极限,min,lowest,max)


    数值极限

    提供查询所有基础数值类型的性质的接口


    定义于头文件

    template< class T > class numeric_limits;

    numeric_limits 类模板提供查询各种算术类型属性的标准化方式(例如 int 类型的最大可能值是 std::numeric_limits::max() )。
     

    辅助类

    指示浮点舍入模式

    std::float_round_style
    1. enum float_round_style {
    2. round_indeterminate = -1,
    3. round_toward_zero = 0,
    4. round_to_nearest = 1,
    5. round_toward_infinity = 2,
    6. round_toward_neg_infinity = 3
    7. };

     std::float_round_style 类型的枚举常量指示浮点算术在凡将表达式结果存储于浮点类型对象时所用的舍入模式。值为:

    枚举常量

    名称定义
    std::round_indeterminate无法确定舍入风格
    std::round_toward_zero向零舍入
    std::round_to_nearest向最近可表示值舍入
    std::round_toward_infinity向正无穷大舍入
    std::round_toward_neg_infinity向负无穷大舍入

    调用示例

    1. #include <iostream>
    2. #include <cmath>
    3. #include <limits>
    4. #include <cfenv>
    5. int main()
    6. {
    7. double number = 2.5;
    8. std::cout << "std::numeric_limits::round_style: "
    9. << std::numeric_limits<double>::round_style << std::endl;
    10. std::fesetround(FE_TONEAREST); // 设置舍入规则为最接近的整数
    11. std::cout << "Round to nearest: " << std::round(number) << std::endl;
    12. std::fesetround(FE_DOWNWARD); // 设置舍入规则为向零舍入
    13. std::cout << "Round toward zero: " << std::round(number) << std::endl;
    14. std::fesetround(FE_UPWARD); // 设置舍入规则为向正无穷大舍入
    15. std::cout << "Round toward infinity: " << std::round(number) << std::endl;
    16. std::cout << "std::numeric_limits::round_style: "
    17. << std::numeric_limits<double>::round_style << std::endl;
    18. return 0;
    19. }

    指示浮点非规格化模式

    std::float_denorm_style
    1. enum float_denorm_style {
    2. denorm_indeterminate = -1,
    3. denorm_absent = 0,
    4. denorm_present = 1
    5. };

    枚举常量

    名称定义
    std::denorm_indeterminate无法确定是否支持非正规值
    std::denorm_absent类型不支持非正规值
    std::denorm_present类型允许非正规值

     调用示例

    1. #include <iostream>
    2. #include <cmath>
    3. #include <limits>
    4. int main()
    5. {
    6. std::cout << "Using denormalized values: ";
    7. if (std::numeric_limits<float>::has_denorm == std::denorm_present)
    8. {
    9. std::cout << "yes" << std::endl;
    10. std::cout << "std::numeric_limits::denorm_min(): "
    11. << std::numeric_limits<double>::denorm_min() << std::endl;
    12. float a = std::numeric_limits<float>::denorm_min();
    13. float b = std::numeric_limits<float>::denorm_min();
    14. float c = a * b;
    15. std::cout << "Denormalized value: " << c << std::endl;
    16. }
    17. else
    18. {
    19. std::cout << "no" << std::endl;
    20. }
    21. return 0;
    22. }

    在这个例子中,首先检查std::numeric_limits::has_denorm来判断系统是否支持denormalized值。如果支持,就将浮点数的denormalized值处理方式设置为std::denorm_present。然后,通过std::numeric_limits::denorm_min()获取最小的denormalized值,并计算它们的乘积。最后,输出计算结果。

    输出

  • 相关阅读:
    vue3 中 ref、toRef、toRefs 和 reactive 的区别
    路由转发的过程
    css--内外边距、 盒子模型、位置、浮动
    签名墙互动项目分析
    【python】机器学习算法(KNN)入门——手写数字识别
    bzip2原理分享
    C++常用格式化输出转换
    QImage图片处理详解
    【STM32F407+CUBEMX+FreeRTOS+lwIP之UDP记录】
    leetCode 139.单词拆分 + 完全背包 + 排列数
  • 原文地址:https://blog.csdn.net/qq_40788199/article/details/133832404