double 到 int 的精度转换问题
#include<stdio.h>
int main(){
double a = 1;
double b = 1.2;
double c = 1.00;
double d = 1.02;
double e = 5;
double f = 6.01;
printf("double %f -> %d\n",a,a);
printf("double %f -> %d\n",b,b);
printf("double %f -> %d\n",c,c);
printf("double %f -> %d\n",d,d);
printf("double %f -> %d\n",e,e);
printf("double %lf -> %d\n",f,f);
printf("(int)double %f -> %d\n",a,(int)a);
printf("(int)double %f -> %d\n",b,(int)b);
printf("(int)double %f -> %d\n",c,(int)c);
printf("(int)double %f -> %d\n",d,(int)d);
printf("(int)double %f -> %d\n",e,(int)e);
printf("(int)double %f -> %d\n",f,(int)f);
return 0;
}
double 1.000000 -> 0
double 1.200000 -> 858993459
double 1.000000 -> 0
double 1.020000 -> -2061584302
double 5.000000 -> 0
double 6.010000 -> 1889785610
(int)double 1.000000 -> 1
(int)double 1.200000 -> 1
(int)double 1.000000 -> 1
(int)double 1.020000 -> 1
(int)double 5.000000 -> 5
(int)double 6.010000 -> 6
1.2在内存中是以double类型存储的,具有64位的长度,但是%d输出时只能截取低32位进行输出。
本博文本意在于记录个人的思考与经验,部分博文采用英语写作,可能影响可读性,请见谅
本文来自博客园,作者:ZXYFrank,转载请注明原文链接:https://www.cnblogs.com/zxyfrank/p/13938884.html