floating point - c++ wrong representation of a number fitting in double -


look @ snipped:

#include <iostream> #include <cstdio> using namespace std; int main() {     //double = 15670.1;     //a += 110420;     double = 1.1;     += 110420;     printf("%f\n",a);     cout << << endl;     = 1.1;     += 11042;     printf("%f\n",a);     cout << << endl; } 

and result is:

110421.100000 110421 11043.100000 11043.1 

as seems printf works correctly wrong cout in first test case? ommiting 0.1 , why ( cout ) correct in second test case?

by default, c++ stream formats floating-point values 6 significant figures, while printf, %f, formats them 6 decimal places.

you can use std::setprecision (declared in <iomanip>) specify higher precision:

cout << setprecision(7) << << endl; // 110421.1 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -