3

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?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
yihangho
  • 2,185
  • 4
  • 25
  • 37
  • somewhat related: http://stackoverflow.com/questions/1653131/what-programming-language-will-enable-me-to-enter-a-very-long-number-without-conv – jldupont Dec 29 '09 at 13:23
  • 1
    "Extreme precise" and "floating point" do not necessarily go together. http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems – Stan Graves Mar 15 '10 at 19:47

6 Answers6

10

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

Yacoby
  • 54,544
  • 15
  • 116
  • 120
  • 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. – yihangho Dec 29 '09 at 13:00
  • 2
    @yihang answered here. http://stackoverflow.com/questions/1017058/building-gmp-library-with-visual-studio – Drew Dormann Dec 29 '09 at 15:08
1

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
  • 35,298
  • 14
  • 114
  • 172
  • 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 Jan 01 '10 at 03:55
1

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
1

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
  • 198,648
  • 61
  • 360
  • 533
0

You can use strings, it's slow but precice. Here, have this addition code i made...

    string add(string a, string b){
       if(b[0]-'0' == 0 && b.length() == 1){
           return a;       
       }
       int len = max(a.length(), b.length())+1;
       int ar[len];
       int br[len];
       int buffer[len];

       for(int i = 0; i < len; i++){
           ar[i] = 0;
           br[i] = 0;  
           buffer[i] = 0;  
       }

       for(int i = 0; i < a.length(); i++){
           ar[i] = a[a.length()-i-1]-'0';   
       }


       for(int i = 0; i < b.length(); i++){
           br[i] = b[b.length()-i-1]-'0';    
       }

       // Now we need to add the numbers and add them to the buffer:

       for(int i = 0; i < len-1; i++){
           buffer[i] = ar[i]+br[i];   
       }

       for(int i = 0; i < len-1; i++){
           if(buffer[i] > 9){
               string temp = convertInt(buffer[i]);
               int first_int = temp[0]-'0';
               int second_int = temp[1]-'0';
               buffer[i+1] = buffer[i+1]+first_int;
               buffer[i] = second_int;      
           }    
       }


       stringstream r;
       bool num_before = false;
       for(int i = len-1; i >= 0; i--){
           if(buffer[i] == 0 && num_before){
               r << buffer[i];
           } else if(buffer[i] != 0 && num_before == false){
               r << buffer[i];
               num_before = true;   
           } else if(buffer[i] != 0 && num_before){
               r << buffer[i];   
           }
       }

       return r.str();
    }

string convertInt(int number)
{
   stringstream ss;//create a stringstream
   ss << number;//add number to the stream
   return ss.str();//return a string with the contents of the stream
}
0

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
  • 13,468
  • 4
  • 44
  • 56