This is a normal implementation of BigInteger for C++, support the operation of much more than 2^64 or 1000 width integer. But please notice that the author@ChiZuo will not take any responsibility for any lost caused by any bugs in this respository. This implementation has a not bad performance in add and sub operations, but low performance in multiply and divide operations between very large numbers compared with BigInteger class in Java11.
Works well in C++11.
Compiler | OS | Support |
GCC-Clang 8.1.0 | Windows | pass |
MSVC-Clang 8.0.1 | Windows | pass |
MSVC 10.0 | Windows | pass |
Support and Finished:
- comparison ==, !=, <, <=, >, >=
- add +, ++, +=
- sub -, --, -=
- multiply *, *= (Karatsuba Algorithm)
- divide /, /=
- mod %, %=
- power (QuickPower Algorithm)
Unfinished but planned:
- support BigDecimal
- clone or download this project
- include the header file (make sure at the same level as your source code file)
#include <BigInt.h>
- create BigInt object
- input long long int
long long int x = 123456789; BigInt temp(x);
- input string object
string pos_x = "123456789"; //or "+123456789", both means positive number BigInt pos_temp(pos_x); string neg_x = "-123456789"; //means negative number BigInt neg_temp(neg_x);
- input long long int
- operation
For example, we want
$[(x+1)^2 \times 123 \div y] \mod y$ , which$x=-123456789, y=88888888$ BigInt x("-123456789"); BigInt y("88888888"); ++x; x = x.power(2); x *= 123; x /= y; x %= y;
- output
- to string
BigInt temp("-123456789"); string temp_str = temp.to_string();
- cout
BigInt temp("-123456789"); cout << temp; terminal will show: -123456789
My work referenced:
- Karatsuba in Wiki https://en.wikipedia.org/wiki/Karatsuba_algorithm (this page in zh has wrong in example codes)
- An implementation of BigInteger for C++ https://github.com/panks/BigInteger
Under Apache-2.0 license.