Operator* Overloading question?

This is a discussion on Operator* Overloading question? within the C++ Programming forums, part of the General Programming Boards category; I think this may be more of a math problem then anything else; the problem is trying to use C++ ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    Operator* Overloading question?

    I think this may be more of a math problem then anything else; the problem is trying to use C++ code to do the math that I need. I'm trying to overload the * operator so I can multiply 2 objects of the same class. The class is called "HugeInt" and it's basically what it is, which allows me to go beyond the 4 billion limit of "double" in a 32-bit machine. But for the life of me I can't figure out how to multiply arrays. The addition part was easy, but multiplication just doesn't want to work for me. Anyway, here is the code

    Code:
     
    HugeInt HugeInt::operator *( const HugeInt &op2 ) const
    {
       HugeInt tempMultiply; // temporary result
       int carryMultiply = 0;
    
       for ( int i = 29; i >= 0; i-- )
       {
          tempMultiply.integer[ i ] = 
             integer[ i ] * op2.integer[ i ] + carryMultiply;
    
           //determine whether to carry a 1
          if ( tempMultiply.integer[ i ] > 9 )
          {
             tempMultiply.integer[ i ] %= 10;  // reduce to 0-9
             carryMultiply = 1;
          } // end if
          else // no carry 
             carryMultiply = 0;
       } // end inner for
    
       return tempMultiply;
    }
    If the code is absolutely sloppy, I apologize for that. Thanks for any help

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Well, not that you've been real specific about what the class is/does, but I'm guessing that HugeInt is an array of 30 digits, and the whole thing strung together represents a number.
    If that's the case, say we have two HugeInts (pretending HugeInt has 10 digits, not 30) like this:
    Code:
    Host object (this):
    [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
     3   4   5   1   0   6   3   5   2   7
    
    op2:
    [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
     4   5   2   1   5   9   0   1   4   5
    representing 3451063527 and 4521590145, respectively.

    By your algorithm, it seems we first loop from the back of the numbers forward. i = 9 in this case. Then we multiply this.integer[9] by op2.integer[9], check to see if we need to carry, and decrement i. Then we multiply this.integer[8] by op2.integer[8], check to see if we need to carry. In the case above, we multiply 7 by 5, and store the result in tempMultiply[9] (checking for carry). Then we multiply 2 by 4, add one for the carry, and assign the result to tempMultiply[8] (checking for carry).

    So far tempMultiply[] looks like:
    Code:
    [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
     ?   ?   ?   ?   ?   ?   ?   ?   7   2
    I trust my calculator's answer of 15604294833452141415, (which wouldn't fit in 10 digits anyway, how are you accounting for that in HugeInt?) so something's already wrong with your algorithm.

    Is this how we multiply two numbers? I don't think so...

    [edit]And my assumptions better be right because that post took way longer that I thought it would... [/edit]
    There is a difference between tedious and difficult.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2
    Well, I wasn't checking to make sure that the numbers would fit into a 30-element array, but I can always add this at a later time after I figure out how to make this algorithm multiply correctly.

    You’re absolutely correct about the class. I apologize for not being more specific, but the array does represent a number, whether it was input as a string or as integers, the constructor is able to convert the numbers and write them to the array.

    I'm not sure what it is that I'm missing. I know what I want the function to do, which is multiply numbers, but I'm just not sure I'm translating the math correctly into C++ code.

    I really appreciate you taking the time in trying to help, Decrypt. Thank you!

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, how do you do multiplication by hand?

    E.g. How do you do 21 * 34 on paper?

    Your program's logic would be that you'd do 1* 4 = 4, and 2 * 3 = 6, and get an answer of 64.

    How would you correctly do the math?
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I believe the standard way to multiply numbers using paper and pecil goes like that:

    Code:
    456 * 789 =
    6*789 + 50*789 + 400 *789 = 
        4734
    +  39450 
    + 315600
    ------------ 
    = 359784
    You might need a whole temporary array for HugeInts and an overloaded + operator.

    Code:
    for ( int i = 29; i >= 0; i-- ) //don't use magic numbers
    //a HugeInt might also have a value that shows how many digits it has
    You'd probably need dynamic containers to store HugeInts. You can easily go over this arbitrary limit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Operator overloading question
    By 39ster in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2007, 12:26 PM
  2. constructor overloading question
    By rozner in forum C++ Programming
    Replies: 6
    Last Post: 10-06-2006, 09:54 PM
  3. overloading question
    By ygfperson in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2003, 08:12 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Operator Overloading newbie question
    By Panopticon in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2003, 06:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21