使用头文件cfloat中的符号常量获知浮点类型数据的表数范围
cfloat头文件包含了系统的浮点数的长度限制
头文件climits中的符号常量是获知整型数据的表数范围,并不能获取浮点类型数据的表数范围。
<climits>该头文件包含了系统的整数长度的限制,它已取代了头文件<limits.h>
<cfloat> 该头文件包含了系统的浮点数的长度限制,它以取代了头文件<float.h>
<cfloat> 头文件各字符常量的取值和含义
上图中在程序中常用到的定义浮点数大小的符号常量如下
FLT_MIN float类型最小的正数
FLT_MAX float类型最大的正数
DBL_MIN double类型最小的正数
DBL_MAX double类型最大的正数
?FLT_MANT_DIG float类型的二进制有效数字位
DBL_MANT_DIG double类型的二进制有效数字位
?FLT_DIG float类型的精度
DBL_DIG double类型的精度
FLT_MIN_EXP float型二进制浮点数的最小指数
FLT_MAX_EXP float型二进制浮点数的最大指数
DBL_MIN_EXP double型二进制浮点数的最小指数
DBL_MAX_EXP double型二进制浮点数的最大指数
FLT_MIN_10_EXP float型十进制浮点数的最小指数
FLT_MAX_10_EXP float型十进制浮点数的最大指数
DBL_MIN_10_EXP double型十进制浮点数的最小指数
DBL_MAX_10_EXP double型十进制浮点数的最大指数
[cpp] view plain copy
- #include <iostream>
- #include <cfloat>
- using namespace std;
- int main()
- {
- cout << "Size:" << endl;
- cout << "float is " << sizeof (float) << "bytes." << endl;
- cout << "double is " << sizeof (double) << "bytes." << endl;
- cout << "long double is " << sizeof (long double) << "bytes." << endl << endl;
- cout << "Bits per byte = " << CHAR_BIT << endl << endl;
- cout << "Maximum values:" << endl;
- cout << "float: " << FLT_MAX << endl;
- cout << "double: " << DBL_MAX << endl;
- cout << "Minimum values:" << endl;
- cout << "float: " << FLT_MIN << endl;
- cout << "double: " << DBL_MIN << endl;
- system("pause");
- return 0;
- }
运行结果
[cpp] view plain copy
- #include <iostream>
- #include <cfloat>
- using namespace std;
- int main()
- {
- //浮点数在内存中的存储大小(字节)
- cout << "浮点数在内存中的存储大小(字节):" << endl;
- cout << "float is " << sizeof (float) << "bytes." << endl;
- cout << "double is " << sizeof (double) << "bytes." << endl;
- cout << "long double is " << sizeof (long double) << "bytes." << endl << endl;
- cout << "Bits per byte = " << CHAR_BIT << endl << endl;
- //浮点数的最大值
- cout << "浮点数的最大值:" << endl;
- cout << "float: " << FLT_MAX << endl;
- cout << "double: " << DBL_MAX << endl;
- //浮点数的最小值
- cout << "浮点数的最小值:" << endl;
- cout << "float: " << FLT_MIN << endl;
- cout << "double: " << DBL_MIN << endl;
- //浮点数的精度
- cout << "浮点数的精度:" << endl;
- cout << "float: " <<FLT_DIG << endl;
- cout << "double: " << DBL_DIG << endl;
- //浮点数的二进制有效数字位数(尾数所占位数,包括符号位)
- cout << "浮点数的二进制有效数字位数(尾数所占位数,包括符号位):" << endl;
- cout << "float: " <<FLT_MANT_DIG << endl;
- cout << "double: " << DBL_MANT_DIG << endl;
- //二进制浮点数的指数最大值
- cout << "二进制浮点数的指数最大值 :" << endl;
- cout << "float: " << FLT_MAX_EXP << endl;
- cout << "double: " << DBL_MAX_EXP << endl;
- //二进制浮点数的指数最小值
- cout << "二进制浮点数的指数最小值:" << endl;
- cout << "float: " << FLT_MIN_EXP << endl;
- cout << "double: " << DBL_MIN_EXP << endl;
- //十进制浮点数的指数最大值
- cout << "十进制浮点数的指数最大值:" << endl;
- cout << "float: " << FLT_MAX_10_EXP << endl;
- cout << "double: " << DBL_MAX_10_EXP << endl;
- //十进制浮点数的指数最小值
- cout << "十进制浮点数的指数最小值:" << endl;
- cout << "float: " << FLT_MIN_10_EXP << endl;
- cout << "double: " << DBL_MIN_10_EXP << endl;
- system("pause");
- return 0;
- }
运行结果