Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling large numbers in C++?

Tags:

What is the best way to handle large numeric inputs in C++ (for example 10^100)?

For algorithms I usually switch over to ruby and I sometimes use strings.

Any other good methods?

like image 922
kasperasky Avatar asked Sep 22 '08 20:09

kasperasky


People also ask

How do you deal with big numbers in C?

Usually, you would choose a base that is half the largest representable type so that there are no overflows. For instance, in modern C, you would choose uint32_t and do all the digit arithmetic in uint64_t . And don't forget: make the digit type unsigned so that there are no surprises.

How do you deal with large numbers?

It is often easier to deal with very large numbers when they are written as decimals. Notice how the decimal is placed after the whole millions or billions. Hint: A billion is a thousand million.

How do you store large numbers in an array?

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.

What is BigInteger in C?

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.


2 Answers

It sounds like you're looking for a way to enter Arbitrary Precision numbers. here are two libraries you could use: GMP and MAPM

like image 99
hometoast Avatar answered Oct 10 '22 11:10

hometoast


Check out The Large Integer Case Study in C++.pdf by Owen Astrachan. I found this file extremely useful with detail introduction and code implementation. It doesn't use any 3rd-party library. I have used this to handle huge numbers (as long as you have enough memory to store vector<char>) with no problems.


Idea: It implements an arbitrary precision integer class by storing big int in a vector<char>.

vector<char> myDigits; // stores all digits of number 

Then all operations related to the big int, including <<, >>, +, -, *, ==, <, !=, >, etc., can be done based on operations on this char array.


Taste of the code: Here is the header file, you can find its cpp with codes in the pdf file.

#include <iostream> #include <string> // for strings #include <vector> // for sequence of digits using namespace std;  class BigInt { public:     BigInt(); // default constructor, value = 0     BigInt(int); // assign an integer value     BigInt(const string &); // assign a string     // may need these in alternative implementation     // BigInt(const BigInt &); // copy constructor     // ~BigInt(); // destructor     // const BigInt & operator = (const BigInt &);     // assignment operator     // operators: arithmetic, relational     const BigInt & operator += (const BigInt &);     const BigInt & operator -= (const BigInt &);     const BigInt & operator *= (const BigInt &);     const BigInt & operator *= (int num);     string ToString() const; // convert to string     int ToInt() const; // convert to int     double ToDouble() const; // convert to double     // facilitate operators ==, <, << without friends     bool Equal(const BigInt & rhs) const;     bool LessThan(const BigInt & rhs) const;     void Print(ostream & os) const; private:     // other helper functions     bool IsNegative() const; // return true iff number is negative     bool IsPositive() const; // return true iff number is positive     int NumDigits() const; // return # digits in number     int GetDigit(int k) const;     void AddSigDigit(int value);     void ChangeDigit(int k, int value);     void Normalize();     // private state/instance variables     enum Sign{positive,negative};     Sign mySign; // is number positive or negative     vector<char> myDigits; // stores all digits of number     int myNumDigits; // stores # of digits of number };  // free functions ostream & operator <<(ostream &, const BigInt &); istream & operator >>(istream &, BigInt &); BigInt operator +(const BigInt & lhs, const BigInt & rhs); BigInt operator -(const BigInt & lhs, const BigInt & rhs); BigInt operator *(const BigInt & lhs, const BigInt & rhs); BigInt operator *(const BigInt & lhs, int num); BigInt operator *(int num, const BigInt & rhs); bool operator == (const BigInt & lhs, const BigInt & rhs); bool operator < (const BigInt & lhs, const BigInt & rhs); bool operator != (const BigInt & lhs, const BigInt & rhs); bool operator > (const BigInt & lhs, const BigInt & rhs); bool operator >= (const BigInt & lhs, const BigInt & rhs); bool operator <= (const BigInt & lhs, const BigInt & rhs); 
like image 32
herohuyongtao Avatar answered Oct 10 '22 11:10

herohuyongtao