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

Home Posts Topics Members FAQ

Unexpected 'setprecision' behaviour.....

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>

int main()
{
long double ld=1234567890.1 234567898;

int prec = 0;

prec = cout.precision( );
cout<<"Precisio n : "<<prec<<en dl;

cout.flags(ios: :fixed);
cout << setprecision(10 ) << ld << endl;

prec = cout.precision( );
cout<<"Precisio n : "<<prec<<en dl;

return 0;
}
The output I was expecting was :

Precision : 6
1234567890.1234 567898
Precision : 10

But the output that was produced is :

Precision : 6
1234567890.1234 567000
Precision : 10
What happened to the last 3 digits of the variable I am trying to
print.
Why is it filling zeros in the last three places? Is there any reasong
behind that?

Please enlighten me on this issue.

Thanks in Advance,
Mahesh K U

Aug 7 '07 #1
2 1807
AG
Hi Mahesh,

long double number are still represented on a finite number of bits,
which gives a limit to the representation of real numbers. You have
reached this limit.

try this for a first understanding of the problem :

http://en.wikipedia.org/wiki/IEEE_754

AG.

Aug 7 '07 #2
On Aug 7, 8:33 am, mahesh.kanaka.. .@gmail.com wrote:
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>
int main()
{
long double ld=1234567890.1 234567898;
int prec = 0;
prec = cout.precision( );
cout<<"Precisio n : "<<prec<<en dl;
cout.flags(ios: :fixed);
cout << setprecision(10 ) << ld << endl;
prec = cout.precision( );
cout<<"Precisio n : "<<prec<<en dl;
return 0;
}
The output I was expecting was :
Precision : 6
1234567890.1234 567898
Precision : 10
Why?
But the output that was produced is :
Precision : 6
1234567890.1234 567000
Precision : 10
What happened to the last 3 digits of the variable I am trying to
print.
They were never there. The original number you specified is
doubtlessly not representable as a floating point number.
You're library implementation decided to output zeros rather
than random noise once it has output everything significant.
Why is it filling zeros in the last three places? Is there any reasong
behind that?
See http://docs.sun.com/source/806-3568/ncg_goldberg.html. It's
the reference for floating point; until you've understood it,
you shouldn't be using floating point.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 7 '07 #3

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";
2
2165
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of virtual method calls. To further complicate (or perhaps simplify) matters, some compilers (GCC and MingW) produce the expected behaviour, while others (MSVS 7.1) do not. I can only offer two explanations for my observations: 1. The Microsoft...
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 ***
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
3
2493
by: Anjo Gasa | last post by:
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;
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;
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?
1
4875
by: jthep | last post by:
Hi, I'm converting codes that I worked with in C to C++ and having a problem with setprecision. At first the code worked, then I made some changes. When the changes didn't work I wrote it back to how it was before and now it's not working. I keep getting the error message where setprecision is undefined. Can anyone help plz. #include <iostream> using namespace std; float convertFtoC(int temperature); int roundUpNextMultiple(int...
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
8678
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9166
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
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,...
1
6525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
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
4621
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
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.