几年前的代码发现一个BUG:
#include "stdafx.h"
#include "stdio.h"
#include<stdlib.h>
void test(void)
{
double t;
U32 P=1,Q=3;
printf("sizeof(U32)=%d, sizeof(int)=%d, sizeof(long)=%d \n", sizeof(U32), sizeof(int), sizeof(long));
t=P/Q/10;
printf("11. t=%f\n", t);
t=P/Q/10.0;
printf("21. t=%f\n", t);
t=1.0*P/Q/10.0;
printf("22. t=%f\n", t);
t=((double)P)/Q/10.0;
printf("31. t=%f\n", t);
t=P/((double)Q)/10.0;
printf("32. t=%f\n", t);
t=(double)P/(double)Q/10.0;
printf("33. t=%f\n", t);
}
int _tmain(int argc, _TCHAR* argv[])
{
test();
return 0;
}
运行结果:
X:\vs2010\Debug>test_vs2010.exe
sizeof(U32)=4, sizeof(int)=4, sizeof(long)=4
11. t=0.000000
21. t=0.000000
22. t=0.033333
31. t=0.033333
32. t=0.033333
33. t=0.033333