-1

I have a function which gives the result of the multiplication of two big integers ( around 100 digits). How do I test to verify that the function is working correctly? Thank you.

Each big integer is represented by an array. Therefore, the multiplication code works on two arrays containing two big integers and spits out another array that contains the result. Now I need to test this function. I cannot generate test cases using built in data types.

More description: I have the following data structure

struct BigNumber
{
   int bigNum[1000];
   int numDigits;
};

I have written a function BigNumber Mult(BigNumber* first, BigNumber* second); This function gives me the result of first * second. The numbers first and second, can be upto 1000 digits long. Now I need to test the function to verify that it is working correctly.

pritorius
  • 39
  • 6
  • That depends on which language. It's much easier in some languages than others. – Dennis Meng Nov 19 '13 at 04:38
  • I have written the code in C. So, I have a function which takes two arrays (each of which represents a big integer number, possibly 100 digits or more) and prints out the multiplication result of those two integer numbers. Now I need to test if this function works correctly or not. – pritorius Nov 19 '13 at 04:45
  • 1
    Construct a few test cases using a language which does arbitrary precision arithmetic, such as Ruby or Python. Then confirm that your function yields the same results. For instance, did you know that `1234567890123456789 * 9876543210987654321 => 12193263113702179522374638011112635269`? Both Python and Ruby agree on that. – pjs Nov 19 '13 at 05:23
  • Thanks for the response. Actually java has a similar library to handle large numbers. I did not intend to use such libraries for testing. May be there exists a formal way of proving that the code works (representing it by a pseudocode). – pritorius Nov 19 '13 at 05:46
  • Formal proofs based on pseudocode may be able to show the algorithm is conceptually correct, but can't guarantee that there aren't implementation errors. I'd still recommend test cases, whether they're generated internally or externally to Java. Also, confirming the same calculations with multiple sources helps insure that if there's a discrepancy between your answer and theirs it isn't due to a bug in their implementations. – pjs Nov 19 '13 at 16:45

1 Answers1

1

Implement multiplication by addition which is just adding operand a in accumulator b times which is very simple to implement. Then compare the results to check whether your multiplication

Vikram Bhat
  • 6,106
  • 3
  • 20
  • 19
  • Thanks for the response. This means that I need to now implement the addition algorithm for big integer numbers. Then the question comes how to test an addition function that adds two big integers (each possibly 100 digits long). – pritorius Nov 19 '13 at 05:44
  • Addition function very simple to implement you can easily test it using some basic test cases – Vikram Bhat Nov 19 '13 at 05:49
  • Thanks for the response. As you can imagine, I already have addition code implemented in the multiplication code. I need too test that to as part of the multiplication code. I cannot use a part of the code which itself needs to be tested to test my code. – pritorius Nov 19 '13 at 05:53
  • you addition method for multiplication then that is very inefficeient should try some better method like one we do while solving it manually – Vikram Bhat Nov 19 '13 at 06:23