DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   iomanip, setprecision - inprecise? (http://www.daniweb.com/forums/thread75194.html)

rugae Apr 13th, 2007 2:51 am
iomanip, setprecision - inprecise?
 
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?

    double a = 0.1, b = 0.2, c = 0.3, d = 0.4, e = 0.5;
    double f = 0.6, g = 0.7, h = 0.8, i = 0.9;
    cout << setprecision(20) << a << endl;
    cout << setprecision(20) << b << endl;
    cout << setprecision(20) << c << endl;
    cout << setprecision(20) << d << endl;
    cout << setprecision(20) << e << endl;
    cout << setprecision(20) << f << endl;
    cout << setprecision(20) << g << endl;
    cout << setprecision(20) << h << endl;
    cout << setprecision(20) << i << endl << endl;
   
    float a2 = 0.1, b2 = 0.2, c2 = 0.3, d2 = 0.4, e2 = 0.5;
    float f2 = 0.6, g2 = 0.7, h2 = 0.8, i2 = 0.9;
    cout << setprecision(20) << a2 << endl;
    cout << setprecision(20) << b2 << endl;
    cout << setprecision(20) << c2 << endl;
    cout << setprecision(20) << d2 << endl;
    cout << setprecision(20) << e2 << endl;
    cout << setprecision(20) << f2 << endl;
    cout << setprecision(20) << g2 << endl;
    cout << setprecision(20) << h2 << endl;
    cout << setprecision(20) << i2 << endl;
outputs:
0.10000000000000001000
0.20000000000000001000
0.29999999999999999000
0.40000000000000002000
0.50000000000000000000
0.59999999999999998000
0.69999999999999996000
0.80000000000000004000
0.90000000000000002000

0.10000000149011612000
0.20000000298023224000
0.30000001192092896000
0.40000000596046448000
0.50000000000000000000
0.60000002384185791000
0.69999998807907104000
0.80000001192092896000
0.89999997615814209000

vishesh Apr 13th, 2007 3:24 am
Re: iomanip, setprecision - inprecise?
 
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.

rugae Apr 13th, 2007 3:42 am
Re: iomanip, setprecision - inprecise?
 
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?

Infarction Apr 13th, 2007 4:01 am
Re: iomanip, setprecision - inprecise?
 
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.

rugae Apr 13th, 2007 4:49 am
Re: iomanip, setprecision - inprecise?
 
I see, so for those scientic calculators in C++ they use other libraries for better precisions instead of the standard one.
Got it, thanks.


All times are GMT -4. The time now is 7:00 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC