Thread: Please help - dividing doubles

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    17

    Please help - dividing doubles

    I have two variables:
    double a;
    double b;

    I can display them...
    std::cout<<a<<" "<<b;

    and I can display their sum, difference, and product. But when I make a new variable...
    double c = a/b;

    the program just stops when I try to display it. Can someone please tell me why this is?

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, generally you'll want to actually post the code you're having problems with.
    Code:
    #include <iostream>
    
    int main( )
    {
            double a, b, c;
            a = 1.1;
            b = 2.2;
            c = a / b;
            std::cout << "a is " << a
                      << " and b is " << b
                      << " and c is " << c
                      << "\n";
            return 0;
    }
    
    /*
    a is 1.1 and b is 2.2 and c is 0.5
    */
    
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    17
    Well, generally you'll want to actually post the code you're having problems with.
    That's what I was afraid of. I was hoping someone had experienced this before and could give a quick answer. Well, here it goes.

    It is part of a matrix class I am trying to make. One of the functions I am having problems with is one which multiplies a row by a number.

    Code:
    void Matrix::MultiplyRow(int Row,double Factor)
    {
    	for(int u=1;u<=j;u++) {
    		ChangeElement(Row,u,Factor*GetElement(Row,u));
    	}
    }
    It works fine most of the time but fails if I try something like this:
    matrix.MultiplyRow(1,1.0/b.GetElement(1,1));

    I can display the denominator like this:
    std::cout<<b.GetElement(1,1);

    but this doesn't work:
    std::cout<<1.0/b.GetElement(1,1);

    Here are the ChangeElement() and GetElement() functions:
    Code:
    double GetElement(int row,int column) 
    {
    return thematrix[column-1][row-1];
    }
    void ChangeElement(int row,int column,double value) 
    {
    thematrix[column-1][row-1]=value;
    }
    Thanks,
    hunterdude

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Really? It works for me:
    Code:
    #include <iostream>
    
    class Foo
    {
            private:
                    double matrix[5][5];
            public:
                    double const GetElement( const int row, const int col )
                    {
                            return matrix[row][col];
                    }
    
                    void ChangeElement( const int row, const int col, double d )
                    {
                            matrix[row][col] = d;
                    }
    
                    Foo()
                    {
                            int a, b;
                            for( a = 0; a < 5; a++ )
                                    for( b = 0; b < 5; b++ )
                                            matrix[a][b] = a * 5 + b;
                    }
    };
    
    int main( )
    {
            Foo f;
            std::cout << 1.0 / f.GetElement( 1, 2 );
            return 0;
    }
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    17
    Thank you for your help.
    I started a new project and used the exact same code I used before and this time it worked. Hmm, what could have caused that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dividing Doubles
    By yukapuka in forum C++ Programming
    Replies: 9
    Last Post: 06-13-2008, 05:39 PM
  2. parse doubles from socket read?
    By willy in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:32 AM
  3. help with dividing doubles into integers
    By Amyaayaa in forum C++ Programming
    Replies: 4
    Last Post: 02-21-2008, 04:57 PM
  4. Passing array of doubles to ATL COM dll
    By wakeup in forum C++ Programming
    Replies: 3
    Last Post: 04-11-2006, 02:53 AM
  5. Dividing int numbers by 256, but fast!
    By Boksha in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2002, 03:44 PM