c++数据类型大小

c++数据类型大小
void main()
{

float fa=10.23f;
float* pa=&fa;
int* ia=(int*)pa;
cout<<ia<<'\t'<<pa<<endl;
cout<<*ia<<'\t'<<*pa<<endl;
cout<<sizeof(int)<<'\t'<<sizeof(long int)<<'\t'<<sizeof(unsigned long)<<endl
<<sizeof(short)<<'\t'<<sizeof(unsigned short)<<'\t'<<sizeof(float)<<endl
<<sizeof(double)<<'\t'<<sizeof(long double)<<endl;
cout<<sizeof(char)<<'\t'<<sizeof(signed char)<<'\t'<<sizeof(unsigned char)<<endl;
}

结果是:
4 4 4
2 2 4
8 8
1 1 1
可是钱能的书上说在16位机器上依次是:
2 4 4
2 2 4
8 10
1 1 1
为什么我的32位机器的long double反而比16位的精度低?
为什么32位机器不是16位机器精度的一倍?
好困惑啊!~~~~~~~~~~~~~~~
IEEEE规定:扩展浮点数为80位,也就是10字节,可我的才8字节,那么它的表示范畴不是明显低于IEEEE标准?而且,数的表示范畴不能用Cout说明,这个我理解.如果,我让一个双精度浮点数是1.7*10的308次方(Double的最大表示值),它的精度为15位;我让一个扩展浮点数是1.1*10的4932次方,它的精度是19位(长双精度的最大表示值),那么,在我的机器上后一个数就会溢出!因为在我的机器上用8字节存储它,那我不是不能用长双精度的数值?我是32位机啊,不能连16位机的精度都达不到吧?那不是太惨了点!请问:我有什么办法突破这个限制吗?
回答jeff_sn:
我对汇编也不一点了解,能把代码写给我吗?

第1个回答  2007-06-06
c++ primer 4th edition:
The types float, double, and long double represent floating-point single-, double-, and extended-precision values. Typically, floats are represented in one word (32 bits), doubles in two words (64 bits), and long double in either three or four words (96 or 128 bits). The size of the type determines the number of significant digits a floating-point value might contain.

根据Intel的文档的描述,双精度浮点的精度是53位,去掉1位,还有52位,总共还有12位可以给符号位以及指数位使用,符号位占据1位,剩下11位给指数位使用。而范围则是从2^(-1022)到2^(1023),换算成十进制,是2.23*10^(-308)到1.79*10^(308)

而long double本来的长度是80位,有效精度64位(实际63位),15位指数位,加上1符号位,中间还要加上一个integer bit(Intel的文档里面这么描述的),范围是2^(-16382)到2^(16383),十进制的为3.37*10(-4932)到1.18*10^(4932)。

楼主所说的问题不在于机器,而在于编译器,VC和GCC里面的long double和double其实是一样的,如果真的需要使用long double,应该要用汇编了:)

补充:汇编用long double不是说它的语法,是因为可以直接操作到80bit的寄存器,自然就可以存储long double类型的数据,如果楼主只是需要一个80bitlong double型的话可以看看这个:http://www.codeproject.com/csharp/LongDouble.asp
这里提供了一个例子,简介如下:
Abstract
I recently found myself needing to read Intel 80-bit long doubles from a binary stream whilst integrating with another system. For cross platform compatibility reasons, Microsoft decided not to include a long double (a.k.a. extended) type in the framework and thus I was forced to interpret the bytes myself.

This is not an implementation of a long double type, but a BitConverter class that translates between long double byte arrays and regular doubles. Naturally, a certain amount of range and precision is lost during the process; however, for my purposes, this is acceptable.

Introduction
A long double is a floating point number that is 16 bits bigger than a regular double. These additional bits are used to increase both the range and precision of the number and are usually used for mathematical and scientific calculations.

This article describes how to read a long double byte array and create a regular double value from it. The inverse operation is simply a matter of reversing the procedure and will not be covered here.
第2个回答  2007-06-12
这个要看ansi/iso标准,C,C++都遵从这些标准,而不是IEEE的标准,在ansi/iso标准中并没有规定long double为多少字节,在vc中, long double等同double,也就是8字节,在C++ builder中为10字节,在gcc中为12字节。

因此long double并不常用。
相似回答