iomanip, setprecision - inprecise?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

iomanip, setprecision - inprecise?

 
0
  #1
Apr 13th, 2007
Learning about all functions in <iomanip> and was testing out setprecision() and found that something weird is going on...? I did try to do a search on setprecision() limitatations or similar keywords but can't seem to find the right site... can someone please tell me what's going on?

  1. double a = 0.1, b = 0.2, c = 0.3, d = 0.4, e = 0.5;
  2. double f = 0.6, g = 0.7, h = 0.8, i = 0.9;
  3. cout << setprecision(20) << a << endl;
  4. cout << setprecision(20) << b << endl;
  5. cout << setprecision(20) << c << endl;
  6. cout << setprecision(20) << d << endl;
  7. cout << setprecision(20) << e << endl;
  8. cout << setprecision(20) << f << endl;
  9. cout << setprecision(20) << g << endl;
  10. cout << setprecision(20) << h << endl;
  11. cout << setprecision(20) << i << endl << endl;
  12.  
  13. float a2 = 0.1, b2 = 0.2, c2 = 0.3, d2 = 0.4, e2 = 0.5;
  14. float f2 = 0.6, g2 = 0.7, h2 = 0.8, i2 = 0.9;
  15. cout << setprecision(20) << a2 << endl;
  16. cout << setprecision(20) << b2 << endl;
  17. cout << setprecision(20) << c2 << endl;
  18. cout << setprecision(20) << d2 << endl;
  19. cout << setprecision(20) << e2 << endl;
  20. cout << setprecision(20) << f2 << endl;
  21. cout << setprecision(20) << g2 << endl;
  22. cout << setprecision(20) << h2 << endl;
  23. cout << setprecision(20) << i2 << endl;
outputs:
  1. 0.10000000000000001000
  2. 0.20000000000000001000
  3. 0.29999999999999999000
  4. 0.40000000000000002000
  5. 0.50000000000000000000
  6. 0.59999999999999998000
  7. 0.69999999999999996000
  8. 0.80000000000000004000
  9. 0.90000000000000002000
  10.  
  11. 0.10000000149011612000
  12. 0.20000000298023224000
  13. 0.30000001192092896000
  14. 0.40000000596046448000
  15. 0.50000000000000000000
  16. 0.60000002384185791000
  17. 0.69999998807907104000
  18. 0.80000001192092896000
  19. 0.89999997615814209000
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: iomanip, setprecision - inprecise?

 
0
  #2
Apr 13th, 2007
setprecision does nothting but increase decimal places at right.

For example setprecision(2) for 2 will give 2.00 and setprecision(5) will give 2.00000

Which compiler do you use.
Last edited by vishesh; Apr 13th, 2007 at 3:28 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

Re: iomanip, setprecision - inprecise?

 
0
  #3
Apr 13th, 2007
I compiled in cygwin so g++. I didn't mean anything wrong with setprecision() but discovered it while playing with it but why ain't the numbers precise? Isn't that very bad for scientific calculations?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,582
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: iomanip, setprecision - inprecise?

 
1
  #4
Apr 13th, 2007
Floating point numbers are not stored precisely in memory. They are usually stored according to the IEEE 754 standard. If you want exact precision, you need to use a 3rd party library or write your own. For most calculations, however, the small degree of error is acceptable.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 27
Reputation: rugae is an unknown quantity at this point 
Solved Threads: 0
rugae rugae is offline Offline
Light Poster

Re: iomanip, setprecision - inprecise?

 
0
  #5
Apr 13th, 2007
I see, so for those scientic calculators in C++ they use other libraries for better precisions instead of the standard one.
Got it, thanks.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC