相关疑难解决方法(0)

对于使用float作为计数器的循环,增加0.01,不会打印预期的浮点值

for (float x = 0.00; x < 1.00; x += 0.01) {
    cout << x << endl;
}
Run Code Online (Sandbox Code Playgroud)

我预计

0.00
0.01
0.02
...
Run Code Online (Sandbox Code Playgroud)

它工作正常,直到 0.83:

...
0.81
0.82
0.83
0.839999
0.849999
0.859999
0.869999
0.879999
0.889999
0.899999
0.909999
0.919999
0.929999
0.939999
0.949999
0.959999
0.969999
0.979999
0.989999
0.999999
Run Code Online (Sandbox Code Playgroud)

那部分怎么了?为什么所有额外的9s?

c++ floating-point

-5
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1

floating-point ×1