double fat = 0.2654654645486684646846865584656566554566556564654654899866223625564668186456564564664564;
cout<<fat<<endl;
results in:
0.265465
Should it be 7 charcters longer? I thought that a double could hold more than that?
I also get the same result of a "long double".
There are two issues here:
you only get 7 significant figures because your cout stream is defaulting to a precision of 7, as the other answers state you can increase this to
std::numeric_limits<double>::digits10
double can only store a fixed amount of precision anyway so most of the digits assigned to fat will be thrown away (on most machines you will get up to 15 significant figures into a double)
You're just seeing the default precision used by an
iostream
.To improve things, use
std::setprecision()
.Use
std::setprecision(std::numeric_limits<double>::digits10)
for maximum precisionThe problem is with
cout
, which defaults to a certain number of decimal places. You can set cout's precision and then add afixed
to give you the precision you needuse
cout.precision()
.or, you can also use
std::numeric_limits< double >
and#include <limits>
to get the maximum precision of a float or double..see this SO post.