Click here to Skip to main content
13,349,293 members (58,430 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 15 Jun 2001

A class for operations with Large Integer Numbers

, 26 May 2003
Rate this:
Please Sign up or sign in to vote.
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 Here I present only the interface of the class:

class CLargeNumber
  //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);

  //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);
  static void Clean(vector<char>& rvN);
  //Comparison Function
  static int Compare(vector<char> const& rcvN1, vector<char> const& rcvN2);
  static void Add(vector<char> const& rcvN1, vector<char> const& rcvN2, <BR>                  vector<char>& rvNRes);
  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);
  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);
  static void Divide(vector<char> const& rcvN1, vector<char> const& rcvN2, 
                     vector<char>& rvQ, vector<char>& rvR);

  //Transform to a string
  string ToString() const;
  //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;

  //-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:


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


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


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


This will throw an exception for division by 0

  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

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


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;


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


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


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


About the Author

George Anescu
Web Developer
Romania Romania
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionWhy doesn't work some operations ??? Pin
Pahanuch28-Nov-12 4:39
memberPahanuch28-Nov-12 4:39 
QuestionProblem Pin
mheese26-Mar-12 12:42
membermheese26-Mar-12 12:42 
GeneralMultumesc mult! Pin
george schiopu17-May-10 12:38
membergeorge schiopu17-May-10 12:38 
GeneralINT_MIN INT_MAX error [modified] Pin
Erick Birbe26-Jul-09 11:01
memberErick Birbe26-Jul-09 11:01 
QuestionBug? Pin
Samo Jemec14-Mar-06 23:45
memberSamo Jemec14-Mar-06 23:45 
AnswerRe: Bug? Pin
Samo Jemec15-Mar-06 4:40
memberSamo Jemec15-Mar-06 4:40 
Sorry not a bug. But still - when I was trying to use your code in Borlands Builder6, the compiler reported an error (Undefined simbol 'i'). This was caused in CLargeNumber::Subtract function, because the scope of variable "i" in the first "for" loop in Builder isn't visible in the other one also present. So I "corrected" the second "for" loop to:

from original:
for(; i<vN1.size(); i++)
for(int i=0; i<vN1.size(); i++)

This was obviosly a mistake, causing an error mentioned in a previous message.
I was surprised when discovered that your code syntax go through when compiled in Visual Studio. By debuging it I realised that variable "i" stays visible for second "for" loop, so my mistake has been revealed.
To work your code in Builder to, I move and initalize the variable "i" before the first "for" loop and omit the declaration in it:

void CLargeNumber::Subtract(vector<char> const& rcvN1, vector<char> const& rcvN2, vector<char>& rvNRes)
//Local copy
vector<char> vN1 = rcvN1;
int i=0;
for(; i<rcvN2.size(); i++)
if(rcvN2[i] > vN1[i])
vN1[i] += 10;
for(int j=i+1; j<vN1.size(); j++)
if(vN1[j] > 0)
vN1[j] = 9;
vector<char>::iterator it = vN1.end();
if(0 == *it)
for(; i<vN1.size(); i++)

Well ... Wink | ;-)
GeneralThanks Pin
omermedan14-Oct-05 3:03
memberomermedan14-Oct-05 3:03 
GeneralThank you very very much!! Pin
Anonymous7-Apr-05 3:54
sussAnonymous7-Apr-05 3:54 
GeneralCode Usage Pin
mail@pdjohnson.com28-Feb-05 19:18
membermail@pdjohnson.com28-Feb-05 19:18 
GeneralCool Pin
G Poulose25-Sep-04 7:14
memberG Poulose25-Sep-04 7:14 
Generalboost::operators Pin
Jonathan de Halleux27-May-03 21:32
memberJonathan de Halleux27-May-03 21:32 
GeneralCLargeNumber += doesn't work !!!!! Pin
danlemas25-May-03 23:53
memberdanlemas25-May-03 23:53 
GeneralRe: CLargeNumber += doesn't work !!!!! Pin
George Anescu27-May-03 6:20
memberGeorge Anescu27-May-03 6:20 
GeneralTwo Quick additions Pin
James Curran18-Jun-01 8:14
memberJames Curran18-Jun-01 8: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 27 May 2003
Article Copyright 2001 by George Anescu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid