Handling large integers in c++ [duplicate] - c++

This question already has answers here:
Using extremely large integer holding 3001 digits
(2 answers)
Closed 9 years ago.
How can i handle very large integers like 2^100000000 in c++?
I found no solution for this on internet that gives an exact answer.
Is there any mechanism that gives correct value in c++ for such large integers?

What you are looking for is called arbitrary precision arithmetic, you will find numerous libraries and educational resources with some googling.

You can represent given number as a string and convert it to array with integer digits. But the simplest way to google by keywords "long arithmetic c++ library" or something.

Maybe you want to use a Computer Algebra System (CAS), which would represent your expression like this:
class Pow : public Expr {
Number base;
Number exp;
};
Pow expr = new Pow(2, 100*1000*1000);
A CAS then allows you to manipulate these expressions structurally instead of the concrete values.

Related

Converting float to int in C++ [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I am talking about converting floats that already store integer values (like 10.0)
I need to do this because I want to create a simple function that reverses a given integer, and it uses a line of code that looks something like this: ans += x % (int)pow(10, i); (the % operator needs both arguments to be integer, and pow returns a double)
C++ is not very happy doing this conversion properly. For pow(10, 4), I get a value of 9999, which is very irritating. Why this error occurs is obvious, 10^4 must have been stored as 9999.999999... or something like that. I could possibly fix this error by using the lround() function, but that would be a less than optimal solution because what if I overlook a similar error like this?
Is there a way to do mathematics in C++ without worrying about such trivialties? If not, what language should I choose to do computation like this? I have briefly used the bigInt library in python. Any suggestions regarding which language and library to use to tackle this issue would be very helpful.
In other languages you have types for doing accurate math operations like in C# you have the decimal and in Java the BigDecimal type for example. So that problems like this:
printf (" %.20f \n", 3.6);
--> 3.60000000000000008882
or
0.1 + 0.2
--> 0.30000000000000004
does not happen.
In C++ you can use for example: The GNU Multiple Precision arithmetic library or Boost.Multiprecision or DecimalForCPP (header only). Surely there are more libraries available.
If you use such types, remember that they are slower than the inaccurate floating point types. But if you work with money for example, it's a must!

How would I find 100! accurately? [Programming challenge] [duplicate]

This question already has answers here:
Calculate the factorial of an arbitrarily large number, showing all the digits
(11 answers)
Closed 9 years ago.
I tried using double but it would give me scientific answers like 3.2e+12. I need proper answer. How would I do that??
My code so far:
int n, x;
double fact;
cin>>n;
while(n--)
{
fact=1;
cin>>x;
for(;x>1;x--)
fact*=x;
cout<<fact<<endl;
}
First things first, using floating point formats such as double and float will always introduce rounding error, if you want to reduce the error with large numbers, use long or long long, however these will not be able to represent values as large as double or long double (note that the behavior and support for long long and long double varies between compilers). You might want to look into BigNums like bigint or bigdouble, though you will sacrifice preformace.
That said, this issue might also be one of setting the formatting: the number is large enough that it is outputted in scientific notation, to change this you can use
cout<<std::fixed;
possible duplicate of How to make C++ cout not use scientific notation
double is a fixed-size type, typically 64 bits, with 53 bits of precision; so it can't accurately represent any integer with more than about 16 digits. The largest standard integer type typically has 64 bits, and can represent integers up to about 19 digits. 100! is much larger than that, so can't be represented accurately by any built-in type.
You'll need a large integer type, representing a number as an array of smaller numbers. There's no standard type; you could use a library like Boost.Multiprecision or GMP or, since this is a programming challenge, implement it yourself. To calculate factorials, you'll need to implement multiplication; the easiest way to do that is with the "long multiplication" algorithm you learnt in school.
There's no data type can store such a big number as (100!) so far.
You should finish something like a BigIntenger class to calculate 100!;
And usually,such big number can be stored by strings.

Algorithm for division. [duplicate]

This question already has answers here:
Algorithm for dividing very large numbers
(5 answers)
Closed 10 years ago.
May be I am already reinventing the wheel.
Normally in C, if we have a=34 and b=5 we get a/b=6. But I need the same thing for 100 digit numbers. I wrote a class with name Int. which does a+b and a-b and a*b. (a,b,c are type Int)
I overloaded the operators << , >>, which will return number divided or multiplied with 10.
What is the best algorithm for division? (assuming I store numbers as strings with base 10".
Thanks.
Perhaps you should consult https://en.wikipedia.org/wiki/Division_%28digital%29, specifically the section on large integer methods.
You're reinventing the wheel. See: http://gmplib.org/

how to sum two or three big number? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
how to sum a large number of float number?
“BigInt” in C?
handling integer having large number of digits
I want to sum two different number .
think we have two different number that length of anyone is more than 20 number , how can I sum both ? as far as I know , I can not do this with int .
like these :
26565468416574156465651652464234245645945643526 + 6264635454513215421541412154121541544455412315
There is a bunch of libraries that can do this as well as you may implement it yourself. Check this Wikipedia article.
Take a look at this C++ Big Integer lib
You must use some BigInteger implementation. Either search for a C++ library that does that or implement it yourself.
Most common implementations store the "big integer" in an array of bytes. To add two of those, do a byte-wise addition and take care of carry (both generate and properate).
EDIT: Not necessarily bytes. Any unsigned storage like int32, int64 or whatever your machine can handle.
Miracl is a great solution and pretty standard solution :
http://www.shamus.ie/index.php?page=Downloads

multiplication of string [ containing integer], output also stored in string, How? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Inputting large numbers in c++?
Arbitrary-precision arithmetic Explanation
I need to multiply two huge huge integers, like:
a=1212121212121212121212121212121212121212121212121212;
b=1212121212121212121212121212121212121212121212121212;
I think there are no data types in C and C++ to hold this huge an integer, so I thought to keep it as a string format like:-
char *number1="1212121212121212121212121212121212121212121212121212";
char *number2="1212121212121212121212121212121212121212121212121212";
during the time of multiplication I convert it into string with help of atoi() function like:
atoi(number1)*atoi(number2);
As usual the output of this multiplication will be obviously huge, so I need to change the output in string format.
I know there is an itoa() function which converts an integer to a string but it is not compatible with all compilers. Can any body tell me what I should do in this scenario?
I am using Ubuntu-10.04 and the g++ compiler.
Since C and C++ do not offer a native type that supports big numbers, it makes no sense to call atoi() to parse such numbers. atoi() returns a native int which is capped at 2,147,483,647 on 32-bit platforms.
You can use one of the numerous bignum libraries, like GMP for instance.
I think, the best variant besides using some math libraries is to split those numbers into int arrays with some fixed limit. Then just perform multiplication using basic math multiplication methods. And do not forget about overflows.
Multiplying the large numbers is very
difficult, however we can do it by
applying the logarithm of
multiplication of two numbers formula
and now we are going know how we
derived the product of two numbers’
logarithm.
Let us consider a, m and n are positive real numbers but a does not equal to 1 which means ‘a’ belongs to R+ – {1}. Logarithm of m and n to base a are x and y respectively by satisfying ax is equal to m and ay is equal to n condition.
loga (m.n) = x + y
As we already know x = loga m and y = loga n.
loga (m.n) = loga m + loga n
logarithm of multiplication of two values is equal to summation of the same values’ logarithms. The same logarithmic fundamental can now help us in multiplying the two large numbers by adding the logarithm of those values. If you don’t have a calculator, just take the logarithmic table help to perform this.
Using atoi() is also not helpful since the number itself won't fit in integer data type.
You have to simulate the method you did in elementary school.
121
*23
----
363
242*
----
2783
The implementation is left as an exercise. You would also need to know how to add big numbers.