Unexpected setprecision behavior

A

Anjo Gasa

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.063397526741027832;

std::cout << value << std::endl;
std::cout << std::setprecision( 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
 
J

Jacek Dziedzic

Anjo said:
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.063397526741027832;

std::cout << value << std::endl;
std::cout << std::setprecision( 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.

The behaviour is expected. At the moment you setprecision()
the first time, you are neither in fixed, nor in scientific mode.
There, setprecision sets the number of _all_ digits, not those
after the decimal point, AFAIR. Try _first_ going into fixed,
then setprecision().

HTH,
- J.
 
M

Marcus Kwok

Anjo Gasa said:
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.063397526741027832;

std::cout << value << std::endl;
std::cout << std::setprecision( 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.

IIUC when you are using fixed, then the precision tells you the total
number of digits after the decimal point. So, since the precision is 7,
there are 7 digits after the decimal point (you must count the first 0
after the decimal point).
 
D

Diwa

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.063397526741027832;

std::cout << value << std::endl;
std::cout << std::setprecision( 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.543543543;
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.precision(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.precision(2);
std::cout << f1 << " " << f2 << " " << f3 << " " << f4
<< " " << f5 << " " << f6 << " " << f7 << "\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,696
Latest member
BarbraOLog

Latest Threads

Top