Thread: Matrices set precision

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    18

    Matrices set precision

    I wrote a code to perform a matrix operation but I need to set the precision to 3 decimal places for the results. I'm having trouble doing so because I'm not getting any different from if the set precision commands were not there. Any help?


    Code:
    #include <cstdlib>
    #include <iostream>
    #include <cmatrix>
    #include <iomanip>
    
    
    using namespace std;
    typedef techsoft::matrix<int> iMatrix;
    
    
    int main()
    {
         //Set Precision
        cout.setf(ios::fixed);
        cout.precision(3);
    
    
        //Declare Matrices
        int Bdata []={-2, 2,-1,5}, Cdata []={3, 2, -1, -2, 0, 2};
        iMatrix B(2,2,Bdata);
        iMatrix C(3,2,Cdata);
    
    
        //Display
        cout << "This program uses a matrix class from Techsoft." << endl;
        cout << endl;
        cout << "Matrix C" << endl;
        cout << setprecision(3) << C << endl;
        cout << endl;
        cout << "Matrix B" << endl;
        cout << setprecision(3) << B << endl;
        cout << endl;
        cout << "Matrix (CB)(C transpose)" << endl;
        cout << setprecision(3) << (C*B)*~C << endl;
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    precision() only affects the maximum number of meaningful digits displayed, not the minimum. So 2.0 with precision 5 will be displayed as 2, not as 2.2000.

    If you want to set the minimum number of digits allowed to be displayed, use the streams width() method, or the setw stream manipulator.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    18
    So something of this nature?

    Code:
        cout << "This program uses a matrix class from Techsoft." << endl;
        cout << endl;
        cout << "Matrix C" << endl;
        cout << setw(2) << C << endl;
        cout << endl;
        cout << "Matrix B" << endl;
        cout << B << endl;
        cout << endl;
        cout << "Matrix (CB)(C transpose)" << endl;
        cout << setw(3) << (C*B)*~C << endl;
    Last edited by Allen Sarkisov; 03-29-2014 at 08:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. %f precision
    By kermit in forum C Programming
    Replies: 4
    Last Post: 04-23-2010, 06:09 PM
  2. precision in f
    By -EquinoX- in forum C Programming
    Replies: 1
    Last Post: 02-04-2008, 11:16 AM
  3. Precision
    By acc988 in forum C++ Programming
    Replies: 8
    Last Post: 07-26-2005, 06:17 PM
  4. set precision?
    By Geo-Fry in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2003, 10:03 PM
  5. Set Precision Help !
    By Halo in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 10:24 AM

Tags for this Thread