Connecting Tech Pros Worldwide Help | Site Map

Unexpected setprecision behavior

Anjo Gasa
Guest
 
Posts: n/a
#1: Feb 1 '07
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

Jacek Dziedzic
Guest
 
Posts: n/a
#2: Feb 1 '07

re: Unexpected setprecision behavior


Anjo Gasa wrote:
Quote:
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.
Marcus Kwok
Guest
 
Posts: n/a
#3: Feb 1 '07

re: Unexpected setprecision behavior


Anjo Gasa <anjogasa@gmail.comwrote:
Quote:
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).

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Diwa
Guest
 
Posts: n/a
#4: Feb 14 '07

re: Unexpected setprecision behavior


On Feb 1, 11:39 am, "Anjo Gasa" <anjog...@gmail.comwrote:
Quote:
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";
}



Closed Thread


Similar C / C++ bytes