<iomanip> precision value

R

rupert

i've got the following code:
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<double> v;
   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;
   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.
I would have exptected precision() to return an integer of value 3, any
ideas?
 
K

Kai-Uwe Bux

rupert said:
i've got the following code:
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<double> v;
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;
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.
I would have exptected precision() to return an integer of value 3, any
ideas?

When you say

double r = 1.04;

this is just a double to the compiler. Numbers do not have an intrinsic
precision. If you need that, you need to use interval arithmetic.

The precision that you print is not a property of the doubles but of the
stream that you are writing to. It just happens to default to 6. It would
do so regardless of which numbers (if any) you decide to write there.


Best

Kai-Uwe Bux
 
M

Mike Wahler

rupert said:
i've got the following code:
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<double> v;
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;[/QUOTE]

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.
[QUOTE]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top