473,698 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Feb 1 '07 #1
3 2493
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.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.
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.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.
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.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";
}

Feb 14 '07 #4

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

Similar topics

2
8717
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 buffer; buffer << setprecision(1) << 40.0 << "° C";
9
2180
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! http://www.geocities.com/jeff_louie/oop30.htm Regards, Jeff *** Sent via Developersdex http://www.developersdex.com ***
62
3763
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
2671
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 desired output is: 0.555504 of an inch foreach 12.55
2
2069
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
12503
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
1808
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
5061
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 meaningful, because it exceed the double number's precision limit. Even if I setprecision to be 100, shall it truncate the number to be of 15(or 16) digits?
2
5517
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 METRIC = 'M'; // Input read as metric char const ENGLISH = 'E'; // Input read as english char const QUIT = 'Q'; // Input read to quit program char const QUIT2 = 'q'; // Input read to quit program
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.