views:

4706

answers:

11

What's the best way to handle large numeric inputs in c++ (eg 10^100)?

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

Any other good methods?

A: 

assuming you are talking about inputting numbers, double precision would get you up to 1.7976931348623157 x 10^308

shoosh
+1  A: 

Are you looking for how to perform operations on the large inputs you receive? There is a big integer C++ library (similar to Java) that allows you to perform arithmetic operations...

Swati
+2  A: 

You might want to have a look to gmplib, an arbitrary precision number handling library for C and C++

Sergio Acosta
+7  A: 

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

hometoast
+1  A: 

If you want it to be accurate, you need a library made to deal with big numbers. Java has BigInt that will always be accurate no matter how many digits you want to take it to, and provides math operations on them. All the source code is included, you could transfer it, but this really isn't the kind of thing C++ is best at--I'd use a JVM based language and use one of the Big libraries.

I don't think I'd use ruby for this unless you wanted it to be slow, and I'm assuming that since you are talking about C++, speed is somewhat of a design consideration.

Bill K
+1  A: 

As others have already pointed out, there are various bignum/arbitrary precision libraries in C++ that you would likely find useful. If speed isn't necessary, I'm under the impression that Python and Lisp both use bignums by default.

donair
That is correct for Liso.If I am doing bignum stuff, I roll with Lisp. :)
Paul Nathan
@Paul Nathan> That is correct for Liso. Do you mean Lisp? or is Liso some library that I'm not aware of?
chollida
A: 

You can input big numbers using scientific notation, or an abbreviation of it that C/C++ understands.

First, a sample program for reference:

#include <iostream>
using namespace std;

int main()
{
   double d;
   cout << "Enter large number: ";

   cin >> d;

   cout << d << endl;     
   return 0;
}

Now, after compiling and running the program, when you get to the prompt, type the string "10e100" without the quotes. The program should output "1e+101". So this is scientific notation where the "e" means x 10^x where x is the value following "e" and x can be either positive or negative indicated by a + or - respectively.

Hope this helps.

ceretullis
A: 
Abhishek Mishra
A: 

What about operations with big numbers? Does anyone know a beginers code for that?

Razvan
A: 

This is a great way for adding huge numbers but what about using strings for divide? What would the code be to accomplish this task?

Jon
A: 

Well I think the best way to do such arithmetic calculation is by using strings.give input as command line arguments and then manipulate the whole logic using string functions like atoi() and itoa() !! But, hey can this be done for multiplication and Division?? I think in this way strlen of strings entered doesn't matter for programming for compiler until the logic is fine..

Ashik