Thread: multiply arbitrarily large real numbers

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Unhappy multiply arbitrarily large real numbers

    Please, Help me with my project.

    Write a program that will multiply arbitrarily large real numbers (stored in the chain). Numbers can be positive or negative and the program will accept a comma as decimal point.
    Example output:
    15321548949451231684.12654894856 x 15,658,213,548,949.5684516 =239908085351191302633721198032836,254474415727249 696

    Classic calculator Is not problem for me, but this program is too hard for me.
    So anyone who know how I can do this please help. Thank you

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's certainly no built-in mechanism, so you'll have to (1) pick an algorithm for multiplying numbers together and then (2) implement it for the data structure you are using (I'm assuming "chain" refers to a linked list.) That assumes you've already done (3) input and (4) output.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you had sufficient time and a large enough piece of paper, could you solve the problem on paper? (assuming you never got bored and never made mistakes)
    If so, all you need to do is follow the "algorithm" you used to do is and then translate that algorithm into pseudocode and then finally into C code.
    Can you read in a large number character by character into the "chain" (I assume this is a linked list)?
    Which parts do you find hard and what have you done so far?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    3
    sorry for translate mistake. I mean string, not chain (In czech language nearly same words)
    I have this program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <locale.h>
    
    
    
    double string2double(char *s)
    
    {
    
          double d;
    
          char *p = strchr(s, ','), *end;
    
    
    
          if (p) *p = '.';
    
    
    
          d = strtod(s, &end);
    
          if (*end != 0) d = 0;
    
    
    
          return d;
    
    }
    
    
    
    
    int main(int argc, char * argv[])
    
    {
        
          char s1[1000], s2[1000], s3[1000];
          char rep;
          double d1, d2, d3;
    
    	  do{
          scanf("%s\n",s1);
          scanf("%s",s2); 
    
          setlocale(LC_NUMERIC, "C");
          
    
          d1 = string2double(s1);
    
          d2 = string2double(s2);
           
          d3=d1*d2;
                
           d3=sprintf (s3, "%d * %d = %d", d1, d2, d3);
           printf ("%s",s3);
           
          
          
            
          
          printf("\n\n Repeat? y/n:");                          
                         
                  scanf("\n%c", &rep);
                  
                  printf("\n");
                  
                   }while(rep=='y' || rep=='Y');
    
              
          return 0;
    
    }
    I need make d3(double) to s3(string) with a large total number of cells (like in example output )

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The catch is that it is physically impossible to make d3 into s3 with a large total number of cells. Doubles store their contents in a scientific-notation-y number-plus-exponent fashion, so you will only ever get 18 or so digits back (it's 53 bits if I recall correctly). If you want more you're going to have to work a lot harder. (Maybe a linked list is actually in your future after all.)

    Also your string2double function would fail so hard on the sample input -- those commas are thousands separators, not decimal points.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    142
    Well, few years ago I had to do something similar and the problem is a bit more complicated than it seems at first. If we put the issue with the decimal point to the side, for "arbitrarily large real numbers" you can use LibTom for example. I used LibTomMath for very long integer values and it took some time to grasp the whole thing, but in the end everything just worked as it should.

    I actually bought a book to learn how to use the library and wrote several functions on my own to simplify everything as it is rather complex.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I doubt the solution to the stated problem is to find a library. Unfortunately too many people here suggest libraries as opposed to letting the student develop their own solutions, which was undoubtedly the point of the exercise. You don't learn anything by linking some package.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Step 1. Figure out how to add arbitrarily large integer numbers.
    Step 2. Figure out how to multiply arbitrarily large integer numbers.
    Step 3. Figure out how to add arbitrarily large real numbers.
    Step 4. Figure out how to multiply arbitrarily large real numbers.

    Tim S.
    Last edited by stahta01; 05-17-2011 at 02:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 01-24-2008, 09:40 PM
  2. open file with numbers and multiply them
    By autopilot in forum C Programming
    Replies: 30
    Last Post: 09-10-2007, 04:48 PM
  3. real random numbers
    By mewatC in forum C Programming
    Replies: 7
    Last Post: 09-01-2006, 01:39 PM
  4. How to multiply and Divide Huge int numbers
    By Dewayne in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 08:41 PM
  5. Real and Imaginary numbers
    By tetra in forum C++ Programming
    Replies: 13
    Last Post: 02-03-2003, 11:49 AM

Tags for this Thread