473,320 Members | 1,825 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,320 software developers and data experts.

<iomanip> precision value

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?

Jul 29 '06 #1
2 3006
rupert wrote:
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.    double r = 0.01;
  8.    double s = 1.04;
  9.    double t = 2.23;
  10.    double u = 2.23;
  11.    vector<doublev;
  12.    v.push_back(r);
  13.    v.push_back(s);
  14.    v.push_back(t);
  15.    v.push_back(u);
  16.    streamsize prec = cout.precision();
  17.    cout << "precision of doubles:..." << prec << endl;
  18.    double sum = v[0]+v[1]+v[2]+v[3];
  19.    cout << "actual number:..." << sum << endl;
  20.    cout <<"presision set at 2:..."<< setprecision(2) << sum << endl
  21.    << "precision set at "<< prec << ":..." << setprecision(prec)
  22.    << "  "<< sum << endl;
  23.    return EXIT_SUCCESS;
  24. }
  25.  
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
Jul 29 '06 #2

"rupert" <ru****@web-ideas.com.auwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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.   double r = 0.01;
  8.   double s = 1.04;
  9.   double t = 2.23;
  10.   double u = 2.23;
  11.   vector<doublev;
  12.   v.push_back(r);
  13.   v.push_back(s);
  14.   v.push_back(t);
  15.   v.push_back(u);
  16.   streamsize prec = cout.precision();
  17.   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.  
  9.         
  10.                   double sum = v[0]+v[1]+v[2]+v[3];
  11.   cout << "actual number:..." << sum << endl;
  12.   cout <<"presision set at 2:..."<< setprecision(2) << sum << endl
  13.   << "precision set at "<< prec << ":..." << setprecision(prec)
  14.   << "  "<< sum << endl;
  15.   return EXIT_SUCCESS;
  16. }
  17.  
  18.  
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
Jul 29 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: {AGUT2} {H}-IWIK | last post by:
Hi, I'm trying to pass my vector to a function external to my main(), but my compiler is complaining. FYI, the struct for the facetInfo works perfectly, it's just the vector passing. A quick...
31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
11
by: Charles L | last post by:
I have read that the inclusion of <fstream.h> makes the inclusion of <iostream.h> unnecessary. Is this correct? Charles L
9
by: liaoo | last post by:
Dear all, I have a question about using cout<<... In standard C, I can use printf("%x",...) to print "hex" number. But in Watcom C++, when using cout<<, I got the "decimal" representation ! ...
4
by: alternativa | last post by:
Hi, I'd like to obtain an output looking as follows: name number phone address Caroline 233 34234 White St. 12 Anna 929043 093284 Brown St. 325...
2
by: Rafael Olarte | last post by:
I really appreciate any feed back in order to resolve this challenge. My goal is to be able to obtain the error value as follows: Fahrenheit Centigrade Approximation % Err...
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
6
by: Bernd Gaertner | last post by:
Dear experts, according to the standard, manipulators like noskipws are declared in the header ios (). But when I look at code that is around, I usually see #include<iomanipbeing used. What is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.