首先放一段代码:
//float double 精度测试
//编译器:mingw2.95
#include
#define PI 3.14
int main(){
float ftemp1,ftemp2;
double dtemp1,dtemp2;
ftemp1 = 21.195000000000000017763568394002504646778106689453125;
dtemp1 = 21.195000000000000017763568394002504646778106689453125;
ftemp2 = 21.195000953674316424013568394002504646778106689453125;
dtemp2 = 21.195000953674316424013568394002504646778106689453125;
printf("%.2fn", 21.195000000000000017763568394002504646778106689453125);
printf("%.2fn", ftemp1);
printf("%.2fnn", dtemp1);
printf("%.2fn", 21.195000953674316424013568394002504646778106689453125);
printf("%.2fn", ftemp2);
printf("%.2fn", dtemp2);
return 0;
}
/*
输出结果
21.20
21.19
21.20
21.20
21.20
21.20
*/
/*
内存存储数值:
ftemp1:41A98F5C
ftemp2:41A98F5D
*/
将你程序的运算结果 PIrr*h = 21.195 转换为二进制:
21.195(10) = 10101.0011000111101011100001010001111010111000010100011110110111111...(2)
128位精度内存存储:
01000000000000110101001100011110101110000101000111101011100001010001111011011111111111111111111111111111111111111111111111111111
double类型内存存储:
0100000000110101001100011110101110000101000111101011100001010010
float类型内存存储:
01000001101010011000111101011100
将内存存储的数据算成二进制:
10101.001100011110101110000101000111101011100001010001111011011111111111111111111111111111111111111111111111111111(2)
10101.001100011110101110000101000111101011100001010010(2) = 21.19500000000000028421709430404007434844970703125(10)
10101.0011000111101011100(2) = 21.19499969482421875(10)
可以看到,类型转换的时候,如果尾数位(M,或称底数位)不足,编译器会根据后面那位,1进位,0舍弃(个人猜测)
所以会出现你提到的结果
================
对个人猜测内容进行验证,仅供参考!
21.195000000000000017763568394002504646778106689453125(10)
10101.001100011110101110000101000111101011100001010001111011(2)
10101.0011000111101011100|(2)【此处被舍去】
=21.19499969482421875(10)
内存存储数值:41A98F5C
21.195000953674316424013568394002504646778106689453125(10)
10101.001100011110101110010101000111101011100001010001111011(2)
10101.0011000111101011101|(2)【此处进位】
=21.1950016021728515625(10)
内存存储数值:41A98F5D
所以会得到测试程序段的结果