char ,int ,long,double等数据类型的范围
来源:互联网 发布:在linux中安装jdk9 编辑:程序博客网 时间:2024/06/06 08:56
#include<iostream>#include<string>#include <limits>using namespace std;int main(){ cout << "type: \t\t" << "************size**************"<< endl; cout << "bool: \t\t" << "所占字节数:" << sizeof(bool); cout << "\t最大值:" << (numeric_limits<bool>::max)(); cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl; cout << "char: \t\t" << "所占字节数:" << sizeof(char); cout << "\t最大值:" << (numeric_limits<char>::max)(); cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl; cout << "signed char: \t" << "所占字节数:" << sizeof(signed char); cout << "\t最大值:" << (numeric_limits<signed char>::max)(); cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl; cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char); cout << "\t最大值:" << (numeric_limits<unsigned char>::max)(); cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl; cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t); cout << "\t最大值:" << (numeric_limits<wchar_t>::max)(); cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl; cout << "short: \t\t" << "所占字节数:" << sizeof(short); cout << "\t最大值:" << (numeric_limits<short>::max)(); cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl; cout << "int: \t\t" << "所占字节数:" << sizeof(int); cout << "\t最大值:" << (numeric_limits<int>::max)(); cout << "\t最小值:" << (numeric_limits<int>::min)() << endl; cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned); cout << "\t最大值:" << (numeric_limits<unsigned>::max)(); cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl; cout << "long: \t\t" << "所占字节数:" << sizeof(long); cout << "\t最大值:" << (numeric_limits<long>::max)(); cout << "\t最小值:" << (numeric_limits<long>::min)() << endl; cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long); cout << "\t最大值:" << (numeric_limits<unsigned long>::max)(); cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl; cout << "long long: \t" << "所占字节数:" << sizeof(long long); cout << "\t最大值:" << (numeric_limits<long long>::max)(); cout << "\n\t\t\t\t最小值:" << (numeric_limits<long long>::min)() << endl; cout << "unsigned long long: \n" << "所占字节数:" << sizeof(unsigned long long); cout << "\t最大值:" << (numeric_limits<unsigned long long>::max)(); cout << "\n\t\t最小值:" << (numeric_limits<long long>::min)() << endl; cout << "double: \t" << "所占字节数:" << sizeof(double); cout << "\t最大值:" << (numeric_limits<double>::max)(); cout << "\t最小值:" << (numeric_limits<double>::min)() << endl; cout << "long double: \t" << "所占字节数:" << sizeof(long double); cout << "\t最大值:" << (numeric_limits<long double>::max)(); cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl; cout << "float: \t\t" << "所占字节数:" << sizeof(float); cout << "\t最大值:" << (numeric_limits<float>::max)(); cout << "\t最小值:" << (numeric_limits<float>::min)() << endl; cout << "size_t: \t" << "所占字节数:" << sizeof(size_t); cout << "\t最大值:" << (numeric_limits<size_t>::max)(); cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl; cout << "string: \t" << "所占字节数:" << sizeof(string) << endl; // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl; cout << "type: \t\t" << "************size**************"<< endl; return 0;}
运行结果
以上结果已经很明白了,一下补充说明几点:
概念、整型:表示整数、字符和布尔值的算术类型合称为整型(integral type)。
关于带符号与无符号类型:整型 int、stort 和 long 都默认为带符号型。要获得无符号型则必须制定该类型为unsigned,比如unsigned long。unsigned int类型可以简写为unsigned,也就是说,unsigned后不加其他类型说明符就意味着是unsigned int。
一字节表示八位,即:1byte = 8 bit;
int: 4byte = 32 bit 有符号signed范围:2^31-1 ~ -2^31即:2147483647 ~ -2147483648无符号unsigned范围:2^32-1 ~ 0即:4294967295 ~ 0
long: 4 byte = 32 bit 同int型
double: 8 byte = 64 bit 范围:1.79769e+308 ~ 2.22507e-308
long double: 12 byte = 96 bit 范围: 1.18973e+4932 ~ 3.3621e-4932
float: 4 byte = 32 bit 范围: 3.40282e+038 ~ 1.17549e-038
int、unsigned、long、unsigned long 、double的数量级最大都只能表示为10亿,即它们表示十进制的位数不超过10个,即可以保存所有9位整数。而short只是能表示5位;
另外对于浮点说而言:使用double类型基本上不会有错。在float类型中隐式的精度损失是不能忽视的,二双精度计算的代价相对于单精度可以忽略。事实上,在有些机器上,double类型比float类型的计算要快得多。float型只能保证6位有效数字,而double型至少可以保证15位有效数字(小数点后的数位),long double型提供的精度通常没有必要,而且还要承担额外的运行代价。
double是8字节共64位,其中小数位占52位,2-^52=2.2204460492503130808472633361816e-16,量级为10^-16,故能够保证2^-15的所有精度。
在有些机器上,用long类型进行计算所付出的运行时代价远远高于用int类型进行同样计算的代价,所以算则类型前要先了解程序的细节并且比较long类型与int类型的实际运行时性能代价。
- char ,int ,long,double等数据类型的范围
- C/C++ int、long、double、char等各种内置数据类型的表示范围(最大最小值)
- C/C++中各种数据类型 如 int ,char long,double 的范围(最大值和最小值)
- 关于C和C++中的基本数据类型int、long、long long、float、double、char、string的大小及表示范围
- 关于C和C++中的基本数据类型int、long、long long、float、double、char、string的大小及表示范围
- c++ 类型int、long、double、char等的表示范围(最大最小值)
- C# int、long、double、char等各种内置数据类型的字节数、最大值、最小值
- int,long,long,double,float的范围
- char,short,int,long等范围
- C\C++中int、double等数据类型范围的理解
- (JAVA & C)数据类型取值范围——short int long float double等
- C++ int long long double 等数据范围!
- int、long、longlong、float、double、long double的范围
- String数据类型转换成long、int、double、float、boolean、char等七种数据类型
- int,long long,double,float等的范围(和精度)
- char,short,int,long等数据类型
- int/long/long long/double类型的取值范围
- C/C++ 数据范围(int ,char long, float,double)
- 使用c++ 的stream遇到的问题
- 仔细触摸Java编程中的“文档”和“包”
- android源码 launcher之android:clearTaskOnLaunch属性
- 《机器学习技法》第八讲:Adaptive Boosting
- echarts通过ajax向服务器发送post请求,servlet从数据库读取数据并返回前端
- char ,int ,long,double等数据类型的范围
- java 运算符
- iOS常用小功能的实现
- 文字输入组件的使用与介绍
- 对称加密算法之RC4介绍及OpenSSL中RC4常用函数使用举例
- 初学者 学好JAVA最关键几点
- 2015 大寒,奔跑
- 大数据——hadoop1.2.1+hbase0.94.11+nutch2.2.1+elasticsearch0.90.5安装配置攻略(中集)
- 地址联动(基于xpath的实现)