473,398 Members | 2,120 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,398 software developers and data experts.

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

int prec = 0;

prec = cout.precision();
cout<<"Precision : "<<prec<<endl;

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

prec = cout.precision();
cout<<"Precision : "<<prec<<endl;

return 0;
}
The output I was expecting was :

Precision : 6
1234567890.1234567898
Precision : 10

But the output that was produced is :

Precision : 6
1234567890.1234567000
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 1790
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.1234567898;
int prec = 0;
prec = cout.precision();
cout<<"Precision : "<<prec<<endl;
cout.flags(ios::fixed);
cout << setprecision(10) << ld << endl;
prec = cout.precision();
cout<<"Precision : "<<prec<<endl;
return 0;
}
The output I was expecting was :
Precision : 6
1234567890.1234567898
Precision : 10
Why?
But the output that was produced is :
Precision : 6
1234567890.1234567000
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 objektorientierter Datenverarbeitung
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
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...
2
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...
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! ...
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...
3
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...
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;
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...
1
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...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.