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;
}
When you do multiplication with
doublevalues, the results are not precise. There is an inherent precision available todouble, which, while fairly accurate, is not precise enough to exactly represent your value.I recommend reading What Every Computer Scientist Should Know About Floating-Point Arithmetic.