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

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.

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?

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.

I see, so for those scientic calculators in C++ they use other libraries for better precisions instead of the standard one.
Got it, thanks.

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;


int main()
{
    const int n=4;
    int x[n][n] = {0}; int y[n][n] = {0}; int z[n][n] = {0};
    srand((unsigned)time(NULL));
    for(register int i = 0; i < n; i++){
        for(register int j = 0; j < n; j++){
            x[i][j] = rand()%2; y[i][j] = rand()%2;
            cout << x[i][j] << "  "; 
        }
        cout << endl;
    }
    cout << endl;
    for(register int i = 0; i < n; i++){
        for(register int j = 0; j < n; j++){
            cout << y[i][j] << "  "; 
        }
        cout << endl;
    }
    cout << endl;
    for(register int i = 0; i < n; i++){
        for(register int j = 0; j < n; j++){
            for(register int k = 0; k < n; k++){
                if((x[i][k] & y[k][j]) ==1){
                    z[i][j] = 1;
                    break;
                }
            }
        }
    }
	  for(register int i = 0; i < n; i++){
        for(register int j = 0;  j < n; j++){
            cout << z[i][j] << "  "; 
        }
        cout << endl;
    }
    return 0;
}

Edited 6 Years Ago by Nick Evan: added code tags

Comments
3 years late, no code tags
This article has been dead for over six months. Start a new discussion instead.