I'm trying to implement a BigInteger Class in C++. But, first of all, I've a base question, how the "base data" can be represented? For example, the most stupid way is to have a fixed (or dynamic) array of char and store each single number of an integer in a char. But, ok, this is a very stupid way, and I'm here for your suggestions.
The BIGINT data type is a machine-independent method for representing numbers in the range of -2 63-1 to 2 63-1. ESQL/C provides routines that facilitate the conversion from the BIGINT data type to other data types in the C language. The BIGINT data type is internally represented with the ifx_int8_t structure.
A big integer is a binary integer with a precision of 63 bits. The range of big integers is -9223372036854775808 to +9223372036854775807.
Below are the steps: Take the large number as input and store it in a string. Create an integer array arr[] of length same as the string size. Iterate over all characters (digits) of string str one by one and store that digits in the corresponding index of the array arr.
In C++, we can use large numbers by using the boost library. This C++ boost library is widely used library. This is used for different sections. It has large domain of applications.
There are a bunch of suggestions here for existing implementations: C++ handling very large integers
If you have to implement your own (e.g. for homework), then you have to decide the best way, and how "big" you need to handle. You could use an array of DWORDs, and handle overflowing from one to the next.
Although, for some of the Project Euler stuff, I actually implemented a BigNumber class built on a string. It turned out to be the simplest to implement for +-*/, and scaled to significantly longer numbers than I could get with a few unsigned long long
s. And the performance was perfectly adequate for solving those puzzles.
So, you face a tradeoff between ease of implementation and optimal performance. Have fun ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With