为什么乘两个大的双数给出错误的结果? [英] Why does multiplying two large double numbers give a wrong result?
问题描述
此双乘法(双)1000000007 *(双)11111111的输出应以7(或等于11111111077777777精确)结束。但我写的这段代码输出结果以6(或等于11111111077777776要精确)。我不知道我可能做错了什么。任何帮助将是巨大的。
The output for this double multiplication (double)1000000007 * (double)11111111 should end with 7 (or equal to 11111111077777777 be precise). But this piece of code I wrote outputs the result ending with 6 (or equal to 11111111077777776 to be precise). I cannot figure out what I might be doing wrong. Any help would be great.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setprecision(40) << (double)1000000007 * (double)11111111;
}
推荐答案
code> double 值,结果不准确。 double
有一个固有的精度,虽然相当准确,但不够准确,不足以代表您的价值。
When you do multiplication with double
values, the results are not precise. There is an inherent precision available to double
, which, while fairly accurate, is not precise enough to exactly represent your value.
我建议您阅读每个计算机科学家应该知道的浮点运算。
这篇关于为什么乘两个大的双数给出错误的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!