By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

Unexpected 'setprecision' behaviour.....

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.