Thread: Calculating a BIG number

  1. #1
    Registered User Robert_Ingleby's Avatar
    Join Date
    Oct 2001
    Posts
    57

    Calculating a BIG number

    The user inputs 3 numbers which when multiplied together then by 512 then divided by 1000000 to work out HDD size from Clylinders, Heads and Sectors.

    The problem is that i cannot find a variable to store such a big number.

    I used long int, but it wouldnt have it.

    What variable declaration for HDD size do I use.

    I can supply my source code if u want to see it.

    Thanks,
    Rob.

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    can't you use a double, or if their that big a long double????
    and just ignore the fractional part.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User Robert_Ingleby's Avatar
    Join Date
    Oct 2001
    Posts
    57

    Thanks

    Thanks ill try it and let yu know

  4. #4
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531

    Thumbs up

    You can create struct. And have own data type.
    You can try with string..

    char * function_to_multiply( char *)
    {
    _____________1234
    X____________1234
    ====================
    _____________4936
    +___________3702X
    +__________2468XX
    +_________1234XXX
    ====================
    Result_____1522756

    return Result;
    }

    function_to_divide();
    function_to_deduct();
    function_to_add();

    It needs to create a function for each. I guess you have got the idea.
    Last edited by zahid; 12-31-2001 at 12:48 AM.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  5. #5
    Registered User Robert_Ingleby's Avatar
    Join Date
    Oct 2001
    Posts
    57

    Problem multiplying big numbers

    The problem is I cant get the correct answer to the product of 3 numbers each over 1000. The answer is always wrong.

    My compiler is Borland C++ 3.1 and my code is supplied.

    What can I do to get the answer of 3375000000 when 1500 is multiplyed by 1500 and then multiplyed by 1500 again.

    Here is the code

    #include <stdio.h>

    int main (void)
    {
    long int a,b,c,d;

    printf("Enter 1st number over 1000?");
    scanf("%d",&a);

    printf("Enter 2nd number over 1000?");
    scanf("%d",&b);

    printf("Enter 3rd number over 1000?");
    scanf("%d",&c);

    d = (a*b*c);

    printf("The product of the 3 numbers is %d",d);

    return 0;
    }

  6. #6
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Get all value as string..
    and and do is as I have written earlier .. multiply/divide/add/substract single digit, do that recessively.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No, I was asking Robert_Ingleby, but he's already answered in a previous post

    This works
    Code:
    #include <stdio.h>
    
    int main(void) {
        unsigned long int a,b,c,d;
    
        printf( "Enter 1st number over 1000?" );
        scanf( "%lu",&a );
    
        printf( "Enter 2nd number over 1000?" );
        scanf( "%lu",&b );
    
        printf( "Enter 3rd number over 1000?" );
        scanf( "%lu",&c );
    
        d = (a*b*c);
    
        printf( "The product of the 3 numbers is %lu\n",d );
    
        return 0;
    }
    But only because the values are unsigned, and the result just fits. If the numbers get much bigger, even unsigned ints will be too small.

    > The user inputs 3 numbers which when multiplied together then by 512 then divided by 1000000
    Perhaps you could rearrange the multiplies and divides.

    d = a * b * c ;
    d /= 1000; // one part of the divide by 1000000
    d *= 512;
    d /= 1000; // the other part of the divide by 1000000

    The key is knowing how many trailing 0 bits there are in the a*b*c product, because this will tell you the maximum size of the first division without causing any loss of information.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The only other way i can think of is getting into assembler and manipulation binary memory yourself.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    As someone else has responded... use a floating point number!

    Floating point numbers under C are all of the form:

    sign * integer * base ** exponent

    (this is simplified, but is essentially true.) Generally, the integer part of the number is greater in doubles than unsigned longs because accuracy in floating point is reasonably important.

    In the header <float.h> there are some macros you should look at:

    FLT_RADIX and DBL_RADIX are the base value (expect 2) for floats and doubles.

    FLT_MANT_DIG and DBL_MANT_DIG are the number of digits (if the base is 2, this means bits) of precision in whatever base is used by your compiler. This means that you can represent an integer value from 0 to FLT_RADIX ** FLT_MANT_DIG in a float without losing integer precision (so, you get an exact answer).

    These values can and do change from compiler to compiler and from version to version... but gcc on my platform gives...

    FLT_RADIX 2
    FLT_MANT_DIG 24

    So that can represent a number from -(2**24) to 2**24 ... which isn't big enough for you. It also gives me...

    DBL_RADIX 2
    DBL_MANT_DIG 53

    Now, this is more like it. That's -(2**53) to 2**53... which should be plenty! (It's something near 9,007,199,254,740,992).

    See what your compiler can handle. It will probably be enough for rock and roll.

    Ian Woods

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    197

    gmp is what you want

    http://www.swox.com/gmp
    Itīs a library which does arithmetic operations with every value that fits in your computerīs memory.

    klausi
    When I close my eyes nobody can see me...

  11. #11
    Unregistered
    Guest

    Tried GMP

    Downloaded GMP. There are so many files to study it would take months to complete the project.

    Surely theres an easier way of calculating a big number.

    PLEASE HELP, THIS CALCULATION IS DRIVING ME CRAZY!!!

  12. #12
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    531
    Did anyone notice my post?
    Have you got the idea?

    You can calculate really big numbers in that way.
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  13. #13
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    Not that complicated!

    Your code looks basically correct using long ints... but when you print the result out you are using %d as your format specifier... this is not the correct format specifier for a long int. Try using %ld (That's the letter lowercase 'L' not a one) instead.

    You should also be able to use doubles, like this:

    double a=1500,b=1500,c=1500,d;
    d = a * b * c;
    printf("Product = %.1lf\n", d );

    The %.1lf means print out a double, but only show 1 place after the decimal.

    This gives:
    Product = 3375000000.0

  14. #14
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >Did anyone notice my post?

    Yep. At least one person did. And I must say that you're idea is a good one. Using strings is an easy way to use big numbers. In this way basic calculations can easily be implemented using basic school techniques. And more sophisticated calculations can be performed with numerical math techniques.

  15. #15
    Unregistered
    Guest

    Ill have a go

    ok ill try using strings or structs but im not sure how to implement it.

    Ill go and learn how and post my code when im done.

    thankyou again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Big Number Storage
    By mrafcho001 in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2005, 07:24 PM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM