Heres what I dont understand. If I have value=157.8926 and I want to print that value. Why does %g give 157.893 and %f give 157.892600. I thought they would give the same value since %g is supposed to use %f or %e, depending on the size of the value.
I don't really know, but it might have to do with how the output is formatted. I'm not sure what you mean by %f, %e, or %g, but when you print a float, I think the default is 6 or 7 significant figures. %f looks like it has 9 significant figures. If you're using cout, you can use
1 2 3 4 5
//Applies to the very next ostream only
cout.width(value);
//Applies to the rest of the program
cout.precision(value);
to display how ever many figures you want (where value is the number of figures want to display). Of course, those apply to any ostream, not just cout, and they work on strings and chars, too.
%f, %e and %g are conversion specifiers for output statements right? All three of those specifiers can be used for the float and double variable time. I know that the default is 6 for float. So thats why I dont get why %g gives 157.893. Im testing this with the two codes below. I just want to know how %g is different from %f.
Sorry, by size of the value, I ment the length. And in this case %g is not giving an %e value. So I can only assume that its giving a %f value. So why are are those two codes giving different values.