次のテストケースがあります
#include <stdio.h>
int main() {
double x = 3.987;
printf("x = %lf\n", x);
printf("(double) (long) (x) = %lf\n", (double) (long) (x));
printf("(x*100)/100 = %lf\n", (x*100)/100);
printf("(double) (long) (x*100)/100 = %lf\n", (double) (long) (x*100)/100);
printf("(double) (long) (x*10)/10 = %lf\n", (double) (long) (x*10)/10);
return 0;
}
出力は次のとおりです。
x = 3.987000
(double) (long) (x) = 3.000000
(x*100)/100 = 3.987000
(double) (long) (x*100)/100 = 3.980000
(double) (long) (x*10)/10 = 3.900000
100を掛けると100で割ると相殺されるように思えますか?しかし、実際には精度が低下しています。これはどのように正確に機能しますか?