Possible Duplicate:
Floating point inaccuracy examples
Im having a problem… When i compile the src, the variable showed isn’t the same that i initialized, see it:
#include <iostream>
using namespace std;
int main()
{
long double mynum = 4.7;
cout.setf(ios::fixed,ios::floatfield);
cout.precision( 20 );
cout << mynum << endl;
}
And then:
[fpointbin@fedora ~]$ ./a.out
4.70000000000000017764
How to fix it? I want to “cout” shows 4.700000…
Your variable is
long double, but the default precision of the literal4.7is onlydouble. Since you’re printing it aslong double, the interpretation chooses to print it with enough significant digits to distinguish it from neighbouringlong doublevalues, even though those neighbouring values are not possibledoubles.