views:

298

answers:

5

I am trying to do some extreme precise maths calculation of very big/small number. The very big number may have 10 - 50 digits and the very small number may have 10 - 50 decimal places. Can C++ do this? If not is there any other programming language that can handle this kind of number?

+7  A: 

C++ can do it with a library, for example the GNU Multiple Precision Arithmetic library.

Yacoby
excellent pointer, +1
Johannes Rudolph
I have downloaded GNU Multiple Precision Arithmetic Library(4.3.1) from the site. How do I install that? I am using Visual Studio 2008.
yihang
@yihang answered here. http://stackoverflow.com/questions/1017058/building-gmp-library-with-visual-studio
Shmoopty
+1  A: 

Every language can handle such numbers if you implement the datatype for them correctly. I think you want a language that has such a datatype built-in.

Generally speaking, for the funcitioning of the double datatype in C++, as well as in most other languages precision is very good for very small and very large numbers. I recommend to consult wikipedia on this, it is very well explained there.

The only question that remains is, if a double has enough precision for you. 50 digits would be too much for it to handle I guess, but you can still implement your own datatype. Since you now know how double works, this should be possible. However, you could still try searching for a math library that has such a datatype already implemented.

Johannes Rudolph
As a kludge before resorting to an arbitrary-precision library, the `long double` datatype might work for the OP if it's large enough.
Chris Lutz
A: 

I think python (http://python.org) and ruby (http://ruby-lang.org) both have support for very big and very small numbers like that, you might give them a look. Also, both are interoperable with C++.

Hope this helps.

cjstehno
+1  A: 

Hi

If you want to do very precise arithmetic, as opposed to writing C++ code to do very precise arithmetic, you should look at Mathematica, Maple, and their open-source competition such as Maxima, Sage, and probably others.

Regards

Mark

High Performance Mark
This is a good point.
Charles Eli Cheese
+1  A: 

Many languages have support for arbitrarily large numbers, including (in alphabetical order) Haskell, Icon, Python, Scheme, and Smalltalk, among others. Support for very small numbers is rarer, unless what you want is exact rational arithmetic. If your computations on very small numbers must be exact, then I think exact rational arithmetic is your only alternative, but it gets expensive quickly. Your other option is to code your own floating-point arithmetic with many more digits of precision than are provided by a hardware floating-point unit. For this you need large-integer computation, which in C or C++ can be provided by the GNU GMP library or by Dave Hanson's C Interfaces and Implementations library.

If you are willing to try another language besides C++, I would recommend using Haskell, because it has very good facilities for creating new numeric types, and you can use QuickCheck to test your numeric types automatically, which is a tremendous time-saver and bug-finder.

Norman Ramsey