小编str*_*ner的帖子

浮点舍入效果说明

#include <stdio.h>
int main() {
    printf("%.14f\n", 0.0001f * 10000000.0f);  // 1
    printf("%.14f\n", 0.001f * 1000000.0f);  // 2
    printf("%.14f\n", 0.01f * 100000.0f);  // 3
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

神箭

这段代码的输出是:

1000.00000000000000
1000.00006103515625
1000.00000000000000
Run Code Online (Sandbox Code Playgroud)

我知道,小数不能用浮点数准确表示。但是为什么第 1 行和第 3 行计算正确,而第 2 行却没有?你对这里发生的事情有清楚的解释吗?

c floating-point

3
推荐指数
1
解决办法
96
查看次数

标签 统计

c ×1

floating-point ×1