//p24.c -- @CopyLeft by tsaiwn@csie.nctu.edu.tw // demo the precision of double in C Language // double 實數準確度約十五位多 #include double x, xdelta; int i; /*precdbl.c */ main( ) { double y; x = 1234567.2, xdelta = 0.0001; printf("Before loop, x=%f\n", x); for(i=1; i<= 8000; i++){ y = x + xdelta; /******/ if(i == 1) printf("first y = %f\n", y); x = y; } printf("After loop, x=%f\n", x); printf("Hit RETURN key..."); getchar( ); return 0; } //main( /****** ccbsd2:precision/> gcc precdbl.c ccbsd2:precision/> ./a.out Before loop, x=1234567.200000 first y = 1234567.200100 After loop, x=1234568.000001 ccbsd2:precision/> ****** double 實數佔用 64 bits ====== run on Windows ====== C:\test2>gcc p24.c C:\test2>a.exe Before loop, x=1234567.200000 first y = 1234567.200100 After loop, x=1234568.000001 C:\test2> *************************/