large_number Class Reference

`#include <large_number.h>`

Collaboration diagram for large_number:
[legend]

## Public Member Functions

| large_number ()
| large_number (int n)
| large_number (const large_number &a)
| large_number (std::vector< unsigned char > &vec)
| large_number (char const *number_str)
void | add_digit (unsigned int value)
size_t | num_digits () const
unsigned char & | operator[] (size_t n)
const unsigned char & | operator[] (size_t n) const
large_number & | operator++ ()
large_number & | operator++ (int)
large_number & | operator+= (large_number n)
large_number & | operator+= (int n)
large_number & | operator= (const large_number &b)
template<class T >
large_number & | operator*= (const T n)
char | digit_char (size_t i) const

## Static Public Member Functions

static bool | test ()

## Private Member Functions

template<class T >
void | multiply (const T n)

## Private Attributes

std::vector< unsigned char > | _digits

## Friends

std::ostream & | operator<< (std::ostream &out, const large_number &a)
bool | operator== (large_number const &a, large_number const &b)
bool | operator!= (large_number const &a, large_number const &b)
template<class T >
large_number & | operator+ (const large_number &a, const T &b)

## Detailed Description

Store large unsigned numbers as a C++ vector The class provides convenience functions to add a digit to the number, perform multiplication of large number with long unsigned integers.

## Constructor & Destructor Documentation

## large_number() [1/4]

large_number::large_number | ( |  | ) |

inline

< initializer with value = 1

{ _digits.push_back(1); }
large_number::_digits
std::vector< unsigned char > _digits
Definition: large_number.h:285
std::vector::push_back
T push_back(T... args)
Here is the call graph for this function:

## large_number() [2/4]

large_number::large_number | ( | int | n | ) |

inlineexplicit

< initializer from an integer initializer from another large_number

Here is the call graph for this function:

## large_number() [3/4]

large_number::large_number | ( | const large_number & | a | ) |

inline

initializer from a vector

## large_number() [4/4]

large_number::large_number | ( | std::vector< unsigned char > & | vec | ) |

inlineexplicit

initializer from a string

## Member Function Documentation

## add_digit()

void large_number::add_digit | ( | unsigned int | value | ) |

inline

add a digit at MSB to the large number

{
if (value > 9) {
std::cerr << "digit > 9!!\n";
exit(EXIT_FAILURE);
}
_digits.push_back(value);
}
std::cerr
std::exit
T exit(T... args)
Here is the call graph for this function:

## digit_char()

char large_number::digit_char | ( | size_t | i | ) | const

inline

returns i^th digit as an ASCII character

{
return _digits[num_digits() - i - 1] + '0';
}
large_number::num_digits
size_t num_digits() const
Definition: large_number.h:130
Here is the call graph for this function:

## multiply()

template<class T >

void large_number::multiply | ( | const T | n | ) |

inlineprivate

multiply large number with another integer and store the result in the same large number

{
static_assert(std::is_integral<T>::value,
"Can only have integer types.");
// assert(!(std::is_signed<T>::value)); //, "Implemented only for
// unsigned integer types.");
size_t i;
uint64_t carry = 0, temp;
for (i = 0; i < this->num_digits(); i++) {
temp = static_cast<uint64_t>((*this)[i]) * n;
temp += carry;
if (temp < 10) {
carry = 0;
} else {
carry = temp / 10;
temp = temp % 10;
}
(*this)[i] = temp;
}
while (carry != 0) {
this->add_digit(carry % 10);
carry /= 10;
}
}
large_number::add_digit
void add_digit(unsigned int value)
Definition: large_number.h:118
std::is_integral
Here is the call graph for this function:

## num_digits()

size_t large_number::num_digits | ( |  | ) | const

inline

Get number of digits in the number

{ return _digits.size(); }
std::vector::size
T size(T... args)
Here is the call graph for this function:

## operator*=()

template<class T >

large_number& large_number::operator*= | ( | const T | n | ) |

inline

operator overload to increment

{
static_assert(std::is_integral<T>::value,
"Must be integer addition unsigned integer types.");
this->multiply(n);
return *this;
}
large_number::multiply
void multiply(const T n)
Definition: large_number.h:258
Here is the call graph for this function:

## operator++() [1/2]

large_number& large_number::operator++ | ( |  | ) |

inline

operator overload to increment (prefix)

{
(*this) += 1;
return *this;
}

## operator++() [2/2]

large_number& large_number::operator++ | ( | int |  | ) |

inline

operator overload to increment (postfix)

{
static large_number tmp(_digits);
++(*this);
return tmp;
}
large_number
Definition: large_number.h:24

## operator+=()

large_number& large_number::operator+= | ( | large_number | n | ) |

inline

operator overload to add

{
// if adding with another large_number
large_number *b = reinterpret_cast< large_number *>(&n);
const size_t max_L = std::max(this->num_digits(), b->num_digits());
unsigned int carry = 0;
size_t i;
for (i = 0; i < max_L || carry != 0; i++) {
if (i < b->num_digits())
carry += (*b)[i];
if (i < this->num_digits())
carry += (*this)[i];
if (i < this->num_digits())
(*this)[i] = carry % 10;
else
this->add_digit(carry % 10);
carry /= 10;
}
return *this;
}
std::max
T max(T... args)
Here is the call graph for this function:

## operator=()

large_number& large_number::operator= | ( | const large_number & | b | ) |

inline

assignment operator

{
this->_digits = b._digits;
return *this;
}

## operator[]()

unsigned char& large_number::operator[] | ( | size_t | n | ) |

inline

operator over load to access the i^th digit conveniently and also assign value to it

{ return this->_digits[n]; }

## test()

static bool large_number::test | ( |  | ) |

inlinestatic

Function to check implementation

{
std::cout << "------ Checking `large_number` class implementations\t"
<< std::endl;
large_number a(40);
// 1. test multiplication
a *= 10;
if (a != large_number(400)) {
std::cerr << "\tFailed 1/6 (" << a << "!=400)" << std::endl;
return false;
}
std::cout << "\tPassed 1/6...";
// 2. test compound addition with integer
a += 120;
if (a != large_number(520)) {
std::cerr << "\tFailed 2/6 (" << a << "!=520)" << std::endl;
return false;
}
std::cout << "\tPassed 2/6...";
// 3. test compound multiplication again
a *= 10;
if (a != large_number(5200)) {
std::cerr << "\tFailed 3/6 (" << a << "!=5200)" << std::endl;
return false;
}
std::cout << "\tPassed 3/6...";
// 4. test increment (prefix)
++a;
if (a != large_number(5201)) {
std::cerr << "\tFailed 4/6 (" << a << "!=5201)" << std::endl;
return false;
}
std::cout << "\tPassed 4/6...";
// 5. test increment (postfix)
a++;
if (a != large_number(5202)) {
std::cerr << "\tFailed 5/6 (" << a << "!=5202)" << std::endl;
return false;
}
std::cout << "\tPassed 5/6...";
// 6. test addition with another large number
a = a + large_number("7000000000000000000000000000000");
if (a != large_number("7000000000000000000000000005202")) {
std::cerr << "\tFailed 6/6 (" << a
<< "!=7000000000000000000000000005202)" << std::endl;
return false;
}
std::cout << "\tPassed 6/6..." << std::endl;
return true;
}
large_number::large_number
large_number()
Definition: large_number.h:27
std::endl
T endl(T... args)
Here is the call graph for this function:

## Friends And Related Function Documentation

## operator!=

bool operator!= | ( | large_number const & | a,
|  | large_number const & | b
| ) |  |

friend

operator overload to compare two numbers

{
return !(a == b);
}

## operator+

template<class T >

large_number& operator+ | ( | const large_number & | a,
|  | const T & | b
| ) |  |

friend

operator overload to perform addition

{
static large_number c = a;
c += b;
return c;
}

## operator<<

std::ostream& operator<< | ( | std::ostream & | out,
|  | const large_number & | a
| ) |  |

friend

operator overload to compare two numbers

{
for (size_t i = a.num_digits(); i > 0; i--)
out << static_cast<int>(a[i - 1]);
return out;
}

## operator==

bool operator== | ( | large_number const & | a,
|  | large_number const & | b
| ) |  |

friend

operator overload to compare two numbers

{
size_t N = a.num_digits();
if (N != b.num_digits())
return false;
for (size_t i = 0; i < N; i++)
if (a[i] != b[i])
return false;
return true;
}

## Member Data Documentation

## _digits

std::vector<unsigned char> large_number::_digits

private

where individual digits are stored

The documentation for this class was generated from the following file:

- math/large_number.h