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?
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?
You might want to have a look to gmplib, an arbitrary precision number handling library for C and C++
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()
anditoa()
! But, hey can this be done for multiplication and Division? I think in this waystrlen
of strings entered doesn't matter for programming for compiler until the logic is fine.It sounds like you're looking for a way to enter Arbitrary Precision numbers. here are two libraries you could use: GMP and MAPM
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>
.Then all operations related to the big int, including
<<, >>, +, -, *, ==, <, !=, >, etc.
, can be done based on operations on thischar array
.Taste of the code: Here is the header file, you can find its cpp with codes in the pdf file.
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...
If you wish to make your own code for the purpose try using strings to store big numbers... you can then create basic ops like + - / * on them... for example -