"rupert" <ru****@web-ideas.com.auwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
i've got the following code:
-
#include <iostream>
-
#include <string>
-
#include <vector>
-
#include <iomanip>
-
using namespace std;
-
int main(double argc, char* argv[ ]) {
-
double r = 0.01;
-
double s = 1.04;
-
double t = 2.23;
-
double u = 2.23;
-
vector<doublev;
-
v.push_back(r);
-
v.push_back(s);
-
v.push_back(t);
-
v.push_back(u);
-
streamsize prec = cout.precision();
-
cout << "precision of doubles:..." << prec << endl;
-
-
No, this prints the precision with which the stream
-
prints. 'precision' has nothing directly to do with
-
the actual type 'double' objects themselves, only with
-
how a stream prints them. (Note that you wrote
-
'cout.precision'; i.e. precision of 'cout', not of
-
a double.
-
-
-
double sum = v[0]+v[1]+v[2]+v[3];
-
cout << "actual number:..." << sum << endl;
-
cout <<"presision set at 2:..."<< setprecision(2) << sum << endl
-
<< "precision set at "<< prec << ":..." << setprecision(prec)
-
<< " "<< sum << endl;
-
return EXIT_SUCCESS;
-
}
-
-
-
and the value of precision is 6 when there's only 3 sigificant figures.
The value of precision became six when you restored the default
you saved (in 'prec') at the start of your program. The default
stream precision for floating point types is six.
I would have exptected precision() to return an integer of value 3, any
ideas?
Yes, do some reading, and don't guess at what things mean. See
www.accu.org
for peer reviews of C++ books. For good information and examples on using
the C++ standard library, I (and many others here) personally recommend:
www.josuttis.com/libbook
For those who want more in-depth information on IOstreams, I recommend:
http://www.angelikalanger.com/iostreams.html
Finally, your remark about 'significant figures' leads me to believe you
don't fully understand how most floating point values are not exactly
representable by a computer. See the following link for a good essay
on this subject:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
HTH,
-Mike