Connecting Tech Pros Worldwide Forums | Help | Site Map

<iomanip> precision value

rupert
Guest
 
Posts: n/a
#1: Jul 29 '06
i've got the following code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <iomanip>
  5. using namespace std;
  6. int main(double argc, char* argv[ ]) {
  7.  
  8. double r = 0.01;
  9. double s = 1.04;
  10. double t = 2.23;
  11. double u = 2.23;
  12.  
  13. vector<doublev;
  14. v.push_back(r);
  15. v.push_back(s);
  16. v.push_back(t);
  17. v.push_back(u);
  18.  
  19. streamsize prec = cout.precision();
  20. cout << "precision of doubles:..." << prec << endl;
  21. double sum = v[0]+v[1]+v[2]+v[3];
  22. cout << "actual number:..." << sum << endl;
  23. cout <<"presision set at 2:..."<< setprecision(2) << sum << endl
  24. << "precision set at "<< prec << ":..." << setprecision(prec)
  25. << "  "<< sum << endl;
  26.  
  27. return EXIT_SUCCESS;
  28. }
  29.  
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?


Kai-Uwe Bux
Guest
 
Posts: n/a
#2: Jul 29 '06

re: <iomanip> precision value


rupert wrote:
Quote:
i've got the following code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <iomanip>
  5. using namespace std;
  6. int main(double argc, char* argv[ ]) {
  7. >
  8.    double r = 0.01;
  9.    double s = 1.04;
  10.    double t = 2.23;
  11.    double u = 2.23;
  12. >
  13.    vector<doublev;
  14.    v.push_back(r);
  15.    v.push_back(s);
  16.    v.push_back(t);
  17.    v.push_back(u);
  18. >
  19.    streamsize prec = cout.precision();
  20.    cout << "precision of doubles:..." << prec << endl;
  21.    double sum = v[0]+v[1]+v[2]+v[3];
  22.    cout << "actual number:..." << sum << endl;
  23.    cout <<"presision set at 2:..."<< setprecision(2) << sum << endl
  24.    << "precision set at "<< prec << ":..." << setprecision(prec)
  25.    << "  "<< sum << endl;
  26. >
  27.    return EXIT_SUCCESS;
  28. }
  29.  
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
Mike Wahler
Guest
 
Posts: n/a
#3: Jul 29 '06

re: <iomanip> precision value



"rupert" <rupert@web-ideas.com.auwrote in message
news:1154165842.892899.248590@m73g2000cwd.googlegr oups.com...
Quote:
i've got the following code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <iomanip>
  5. using namespace std;
  6. int main(double argc, char* argv[ ]) {
  7. >
  8.   double r = 0.01;
  9.   double s = 1.04;
  10.   double t = 2.23;
  11.   double u = 2.23;
  12. >
  13.   vector<doublev;
  14.   v.push_back(r);
  15.   v.push_back(s);
  16.   v.push_back(t);
  17.   v.push_back(u);
  18. >
  19.   streamsize prec = cout.precision();
  20.   cout << "precision of doubles:..." << prec << endl;
Expand|Select|Wrap|Line Numbers
  1.  
  2. No, this prints the precision with which the stream
  3. prints.  'precision' has nothing directly to do with
  4. the actual type 'double' objects themselves, only with
  5. how a stream prints them.  (Note that you wrote
  6. 'cout.precision';  i.e. precision of 'cout', not of
  7. a double.
  8.     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;
  • }
  •  
  • Quote:
    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.
    Quote:
    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


    Closed Thread