我有以下测试用例
#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 会相互抵消吗?但它实际上是在降低精度。这究竟是如何工作的?