On Feb 1, 11:39 am, "Anjo Gasa" <anjog...@gmail .comwrote:
I'm having some cases where setprecision in combination with iostreams
gives some unepected behavior. Consider the following program:
#include <iostream>
#include <iomanip>
int _tmain(int argc, _TCHAR* argv[])
{
float value = 0.0633975267410 27832;
std::cout << value << std::endl;
std::cout << std::setprecisi on( 7 ) << value << std::endl;
std::cout << fixed << value << std::endl;
std::cout << std::scientific << value << std::endl;
}
which gives the following output:
0.0633975
0.06339753
0.0633975
6.3397527e-002
The first line makes sense (the default precision is 6), the second
line adds a digit of precision in response to the setprecision(7)
call, the third line calls for a fixed output and as a result seems to
only give 6 digits of precision. The final lines seems to give 8
digits of precision. When using scientific is the "precision" of the
number refer to digits right of the decimal point. But why when using
fixed and a precision of 7, does it only give 6 digits of precision.
Anjo
Anjo
precision for the general format specifies max digits
while for float and scientific notation, it specifies
the max digits after decimal points. See below example
void print_floats()
{
float f1 = 54.325617;
float f2 = 183.3;
float f3 = -1545.545;
float f4 = 3434;
float f5 = 3434.123456789;
float f6 = 12345678.543543 543;
float f7 = 0;
// general format : max 6 digits
// 54.3256 183.3 -1545.55 3434 3434.12 1.23457e
+07 0
std::cout << f1 << " " << f2 << " " << f3 << " " << f4
<< " " << f5 << " " << f6 << " " << f7 << "\n";
// setprecision with general format decides max num of digits
// 54.325619 183.3 -1545.545 3434 3434.1235
12345679 0
std::cout.preci sion(8);
std::cout << f1 << " " << f2 << " " << f3 << " " << f4
<< " " << f5 << " " << f6 << " " << f7 << "\n";
// setprecision with fixed format decides max num of digits after
decimal point
// 54.33 183.30 -1545.55 3434.00 3434.12
12345679.00 0.00
std::cout.setf( std::ios_base:: fixed, std::ios_base:: floatfield);
std::cout.preci sion(2);
std::cout << f1 << " " << f2 << " " << f3 << " " << f4
<< " " << f5 << " " << f6 << " " << f7 << "\n";
}