# Thread: Calculating a BIG number

1. ## 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. can't you use a double, or if their that big a long double????
and just ignore the fractional part.

3. ## Thanks

Thanks ill try it and let yu know

4. 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();

It needs to create a function for each. I guess you have got the idea.

5. ## 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. Get all value as string..
and and do is as I have written earlier .. multiply/divide/add/substract single digit, do that recessively.

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. The only other way i can think of is getting into assembler and manipulation binary memory yourself.

9. 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_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_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. ## 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

11. ## 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.

12. Did anyone notice my post?
Have you got the idea?

You can calculate really big numbers in that way.

13. ## 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. >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. ## 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