Click here to Skip to main content
15,868,122 members
Articles / Programming Languages / C++
Article

A class for operations with Large Integer Numbers

Rate me:
Please Sign up or sign in to vote.
4.91/5 (14 votes)
26 May 2003Ms-PL 126.4K   2.4K   41   15
Presenting some algorithms for operations with large integer numbers in a C++ class using the STL vector container

The full code of the class is contained in the article's associated project LargeNumber.zip. Here I present only the interface of the class:

class CLargeNumber
{
public:
  //Default Constructor
  CLargeNumber() : m_cSign(1) { m_oNumber.push_back(0); }

  //Copy Constructor - Default OK
  //CLargeNumber(CLargeNumber const& rcoLargeNumber) : 
  //	m_cSign(rcoLargeNumber.m_cSign), m_oNumber(rcoLargeNumber.m_oNumber) {}

  //Constructor From a Number
  CLargeNumber(int iNumber);
  //Constructor From a String
  CLargeNumber(string const& rostrNumber);
  //Constructor From Members
  CLargeNumber(char cSign, vector<char> const& rcoNumber) : 
  	m_cSign(cSign), m_oNumber(rcoNumber) {}

  //Assignment Operator - Default OK
  //CLargeNumber& operator=(CLargeNumber const& roLN);

protected:
  //Auxiliary class Functions:
  //Build from unsigned long
  static void Build(unsigned uN, vector<char>& rvN);
  //Build from string
  static void Build(string const& rostrNumber, vector<char>& rvN);
  //Cleaning
  static void Clean(vector<char>& rvN);
  //Comparison Function
  static int Compare(vector<char> const& rcvN1, vector<char> const& rcvN2);
  //Addition
  static void Add(vector<char> const& rcvN1, vector<char> const& rcvN2, <BR>                  vector<char>& rvNRes);
  //Subtraction
  static void Subtract(vector<char> const& rcvN1, vector<char> const& rcvN2, <BR>                       vector<char>& rvNRes);
  //Product with one digit
  static void Multiply(vector<char> const& rcvN, char c, vector<char>& rvNRes);
  //Shift Left
  static void ShiftLeft(vector<char>& rvN, int iLeft);
  //Multiplication
  static void Multiply(vector<char> const& rcvN1, vector<char> const& rcvN2, <BR>                       vector<char>& rvNRes);
  //Get the Position of the most significant Digit
  static int Position(vector<char> const& rcvN);
  //Compute a Power of 10
  static void Pow10(unsigned uPow, vector<char>& rvNRes);
  //Division
  static void Divide(vector<char> const& rcvN1, vector<char> const& rcvN2, 
                     vector<char>& rvQ, vector<char>& rvR);

public:
  //Transform to a string
  string ToString() const;
  //Operators
  //Equality Operator
  bool operator==(CLargeNumber const& roLN);
  //Inequality Operator
  bool operator!=(CLargeNumber const& roLN);
  CLargeNumber& operator-();
  bool operator<(CLargeNumber const& roLN) const;
  bool operator>(CLargeNumber const& roLN) const;
  bool operator<=(CLargeNumber const& roLN) const;
  bool operator>=(CLargeNumber const& roLN) const;
  CLargeNumber operator+(CLargeNumber const& roLN) const;
  CLargeNumber operator-(CLargeNumber const& roLN) const;
  CLargeNumber operator*(CLargeNumber const& roLN) const;
  CLargeNumber operator/(CLargeNumber const& roLN) const;
  CLargeNumber operator%(CLargeNumber const& roLN) const;
  CLargeNumber& operator+=(CLargeNumber const& roLN);
  CLargeNumber& operator-=(CLargeNumber const& roLN);
  CLargeNumber& operator*=(CLargeNumber const& roLN);
  CLargeNumber& operator/=(CLargeNumber const& roLN);
  CLargeNumber& operator%=(CLargeNumber const& roLN);
  //Convertion operator
  operator int() const;
  //Square Root
  CLargeNumber SquareRoot() const;

private:
  //-1 - Negative, +1 - Positive or zero
  char m_cSign;
  vector<char> m_oNumber;
};

I am using the STL vector<char> container m_oNumber to keep the decimal digits of the number. The digits are stored in the order from lowest to highest. The sign of the number is kept in the char m_cSign field. The operations for positive large numbers are implemented in some auxiliary static functions. I prefered to use static functions because these auxiliary functions are not dependent on the class's field members. The operations for signed large numbers are implemented using extensive operator overloading and are using internally the auxiliary static functions. Some examples of how to use the class:

Addition

CLargeNumber oLN1("1111111434311111");
CLargeNumber oLN2("2222222233422222");
cout << (oLN1+oLN2).ToString() << endl;

Subtraction

CLargeNumber oLN1("12323523664");
CLargeNumber oLN2("325454361234");
cout << (oLN1-oLN2).ToString() << endl;

Multiplication

CLargeNumber oLN1("123456834333466");
CLargeNumber oLN2(1000);
cout << (oLN1*oLN2).ToString() << endl;

Division

This will throw an exception for division by 0

try
{
  CLargeNumber oLN1("1234655123667");
  CLargeNumber oLN2(500);
  cout << (oLN1/oLN2).ToString() << endl;
  cout << (oLN1%oLN2).ToString() << endl;
}
catch(exception& roEx)
{
  cout << roEx.what() << endl;
}

Square Root

This will throw an exception for negative numbers

try
{
  CLargeNumber oLN1("1000000000000000000");
  cout << oLN1.SquareRoot().ToString() << endl;
}
catch(exception& roEx)
{
  cout << roEx.what() << endl;
}

Factorial

In the testing program I also have implemented a small function for factorial calculation:

CLargeNumber Factorial(int iNumber)
{
  CLargeNumber oLN("1");
  if(iNumber > 1)
  {
    for(int i=2; i<=iNumber; i++)
      oLN *= i;
  }
  return oLN;
}

Example

As an example I give the 158 digits of 100!:

9332621544394415268169923885626670049071596826438162146859296389521759999322991
5608941463976156518286253697920827223758251185210916864000000000000000000000000

I hope you will have some fun playing with this class!

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Web Developer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalvery good Pin
Southmountain20-Oct-20 9:25
Southmountain20-Oct-20 9:25 
QuestionWhy doesn't work some operations ??? Pin
Pahanuch28-Nov-12 3:39
Pahanuch28-Nov-12 3:39 
QuestionProblem Pin
mheese26-Mar-12 11:42
mheese26-Mar-12 11:42 
GeneralMultumesc mult! Pin
george schiopu17-May-10 11:38
george schiopu17-May-10 11:38 
GeneralINT_MIN INT_MAX error [modified] Pin
Erick Birbe26-Jul-09 10:01
Erick Birbe26-Jul-09 10:01 
QuestionBug? Pin
Samo Jemec14-Mar-06 22:45
Samo Jemec14-Mar-06 22:45 
AnswerRe: Bug? Pin
Samo Jemec15-Mar-06 3:40
Samo Jemec15-Mar-06 3:40 
GeneralThanks Pin
gnumerteac14-Oct-05 2:03
gnumerteac14-Oct-05 2:03 
I thought to bulid an binary class in order to get to those huge numbers...
but you made it much easier...

GeneralThank you very very much!! Pin
Anonymous7-Apr-05 2:54
Anonymous7-Apr-05 2:54 
GeneralCode Usage Pin
Member 164155228-Feb-05 18:18
Member 164155228-Feb-05 18:18 
GeneralCool Pin
G Poulose25-Sep-04 6:14
G Poulose25-Sep-04 6:14 
Generalboost::operators Pin
Jonathan de Halleux27-May-03 20:32
Jonathan de Halleux27-May-03 20:32 
GeneralCLargeNumber += doesn't work !!!!! Pin
danlemas25-May-03 22:53
danlemas25-May-03 22:53 
GeneralRe: CLargeNumber += doesn't work !!!!! Pin
George Anescu27-May-03 5:20
George Anescu27-May-03 5:20 
GeneralTwo Quick additions Pin
James Curran18-Jun-01 7:14
James Curran18-Jun-01 7:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.