Extreme programming of large / small numbers

I am trying to make a very accurate mathematical calculation of a very large / small number. A very large number can have 10 to 50 digits, and a very small number can have 10 to 50 decimal places. Can C ++ do this? If there is no other programming language that can handle such a number?

+3
source share
6 answers

C ++ can do this using a library, such as the GNU Multipoint Arithmetic Library .

+10
source

, . , .

, double ++, . wikipedia, .

: . , 50 , . , , . , , .

+1

, ++ , Mathematica, Maple , ​​ Maxima, Sage , , .

+1

, ( ) Haskell, Icon, Python, Scheme Smalltalk, . , , . , , - , . , , . , C ++ GNU GMP Dave Hanson C .

++, Haskell, , QuickCheck, , .

+1

, python (http://python.org) ruby ​​(http://ruby-lang.org), , , . , ++.

, .

0

You can use strings that are slow but neat. Here is the add 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
source

Source: https://habr.com/ru/post/1726790/


All Articles