【发布时间】:2015-12-21 03:32:19
【问题描述】:
以下是我在 64 位环境和 32 位环境中测试过的代码。结果每次都精确地减少一个。预期结果是:1180000000,实际结果是 1179999999。我不知道究竟是为什么,我希望有人能教育我:
#include <stdint.h>
#include <iostream>
using namespace std;
int main() {
double odds = 1.18;
int64_t st = 1000000000;
int64_t res = st * odds;
cout << "result: " << res << endl;
return 1;
}
感谢任何反馈。
【问题讨论】:
-
你会遇到这种浮点问题。
double是双精度,而不是无限精度!尝试cout << setprecision(30) << st * odds << '\n'以进一步了解正在发生的事情。 -
谢谢MM!我现在试试看
-
MM 这是使用 setprecision():无精度:1179999999 精度:1180000000
-
你为什么不发布你得到的结果?也许我们可以帮你算零:)
标签: c++