473,378 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Unexpected setprecision behavior

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

Feb 1 '07 #1
3 2464
Anjo Gasa wrote:
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.
Feb 1 '07 #2
Anjo Gasa <an******@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.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
Feb 1 '07 #3
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.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";
}

Feb 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Woodster | last post by:
I am using the std::setprecision function to format variables of type double in a string however I am unsure how to stop this appearing in scientific notation. For example std::stringstream...
9
by: Jeff Louie | last post by:
In C# (and C++/cli) the destructor will be called even if an exception is thrown in the constructor. IMHO, this is unexpected behavior that can lead to an invalid system state. So beware! ...
62
by: ashu | last post by:
hi look at this code include <stdio.h> int main(void) { int i,j=2; i=j++ * ++j * j++; printf("%d %d",i,j); return 0;
1
by: Gary Wessle | last post by:
hi the code below is giving me what I want but it is very ugly. and will not work for a long list of different length numbers. could you please look at it and comment. thank you the...
2
by: Dimitri Furman | last post by:
SQL Server 2000 SP4. Running the script below prints 'Unexpected': ----------------------------- DECLARE @String AS varchar(1) SELECT @String = 'z' IF @String LIKE ''
7
by: jacek.dziedzic | last post by:
Hello! Can someone tell me what the expected output of the following program is? #include <fstream> #include <iomanip> using namespace std;
2
by: mahesh.kanakaraj | last post by:
Hi All, I am new to C++ programming. I encounter an unexpected behavior of 'setprecision'. The code snippet is : #include <iostream.h> #include <stdio.h> #include <iomanip.h>
3
by: PengYu.UT | last post by:
Hi, I setprecision to be 100 for both cases. I'm wondering why the number of digits are different. Also, for a double number, I think any digits that are longer than 15 (or 16) are not...
2
by: victoryusami | last post by:
I'm not 100% sure whats even going on, but I'm working on a C++ assignment for school, and its a road trip program #include <iostream> #include <iomanip> using namespace std; char const...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.