#include<stdio.h>
void main(){
printf("%f\n",5/9); // prints 0.000000
printf("%f\n",9.0/5.0); // prints 1.800000
printf("%f\n",5/9); // prints 1.799999
}
Run Code Online (Sandbox Code Playgroud)
第 5 行值应像第 3 行一样打印 0.000000
我在本地机器和在线编译器中尝试过,结果是相同的
jpa*_*jpa 12
printf("%f\n",5/9); // 打印 1.799999
您正在传递整数值,同时指定双精度浮点格式。实际上,这会导致printf()读取未初始化的内存,其中几乎可以包含任何内容。
在这种情况下,它包含先前打印的剩余部分,1.8并且整数 0 仅替换它的低位,导致值发生微小变化。
打开编译器警告(例如-Wall在 GCC 和 clang 中),您应该会收到类似 的警告warning: format '%f' expects argument of type 'double', but argument 2 has type 'int'。
| 归档时间: |
|
| 查看次数: |
139 次 |
| 最近记录: |