首页 > 编程知识 正文

CC 编程数值的极值,数控编程与数值分析

时间:2023-05-05 19:49:43 阅读:256773 作者:274

C++标准库的极值由template numberic_limits提供,用来取代C的预处理器常量,优点在于:

提供了更好的类型安全性可以写出一个template以核定这些极值

 

numberic_limits是一个template,它对所有类型提供了一个通用的解决方案。并且为某些类型提供了一个特化版本

通用型的 numeric_limits 以及其特化版本都被包含在<limits>头文件中。 特性化版本包括基础类型:bool, char, signed char, unsigned char, char16_t, char32_t, wchar_t, short, unsigned short, int, unsigned int, long, unsigned long, long long, unsigned long long, float, double, and long double。可以为自定义的数值类型加上一份补充。

 

 

numeric_limits<>成员和含义 成员含义对应C常量is_specialized是否有极值 is_signed带有正负号 is_integer整数类型 is_exact计算结果不产生舍入误差 is_bounded数值集的个数有限 is_modulo两正值相加,结果可能溢出而回绕为较小的值 is_iec559遵从IEC559及IEEE754标准 min()最小有限值INT_MIN,FLT_MIN,CHAR_MIN,…max()最大极值INT_MAX,FLT_MAX,…lowest()最大负有限值 digits字符和整数:不含正负号位数CHAR_BIT浮点数: radix(见下)的位数 max_digits10必要的十进制数位个数 radix整数:表述式的底(base),几乎总是2 浮点数: 指数表达式的底(Base)FLT_RADIXmin_exponent底radix的最小负整数指数FLT_MIN_EXP,…max_exponent底radix的最大负整数指数FLT_MAX_EXP,…min_exponent10底10的最小负整数指数FLT_MIN_10_EXP,…max_exponent10底10的最大负整数指数FLT_MAX_10_EXP,…epsilon()1和接近1的值之间的差距FLT_EPSILON,…round_style舍入风格 示例

你可以对任何类型进行询问,无论它是否定义了极值

#include <iostream>#include <limits>#include <string>using namespace std;int main() { //对bool使用文本表示 cout << boolalpha; // print maximum of integral types cout << "max(short): " << numeric_limits<short>::max() << endl; cout << "max(int): " << numeric_limits<int>::max() << endl; cout << "max(long): " << numeric_limits<long>::max() << endl; cout << endl; // print maximum of floating-point types cout << "max(float): " << numeric_limits<float>::max() << endl; cout << "max(double): " << numeric_limits<double>::max() << endl; cout << "max(long double): " << numeric_limits<long double>::max() << endl; cout << endl; // print whether char is signed cout << "is_signed(char): " << numeric_limits<char>::is_signed << endl; cout << endl; // print whether numeric limits for type string exist cout << "is_specialized(string): " << numeric_limits<string>::is_specialized << endl; return 0;}

 

 

 

最后一行显示,string没有定义数值极限。这是当然,因为string并非数值类型

 

 

参考:14 Numeric Limits 数值极限

 

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。