Question:
C++ Adding/Subtracting Large Numbers?
David
2011-03-03 13:53:26 UTC
I'm trying to write a program that will let me add/subtract large numbers (like, 20+ digits). For some reason, it won't return the full value for the addition, and even returns a negative value for the subtraction (even if the second number isn't larger than the first). I've tried defining the variables as float and long integers, but it's not working.

#include
#include
#include
#include

int main() {

float myIntA,myIntB;

printf("Enter a whole number: \n");
scanf("%f*c",&myIntA);
printf("Enter a whole number: \n");
scanf("%f*c",&myIntB);

printf("The sum is: %f \n",myIntA+myIntB);
printf("The difference is: %f \n",myIntA-myIntB);

return 0;
}

Can anyone point out what I'm doing wrong?
Six answers:
siamese_scythe
2011-03-03 14:22:12 UTC
you should be more clear. you've tried float and long. these hold fundamentally different types of numbers. are we guaranteed to have integer input, or not? the range of signed 64-bit integers is -2^63 to 2^63-1 inclusive. 2^63-1 has 19 digits. c++ is machine-specific, so you should test out the range for example using bit shifts. note that there is not just long, but also long long. you might want to use a typedef to make this more manageable. you may very well need to look into bignum libraries / roll your own, unless you are fine with the rounding that occurs using (long) doubles to store large integers. good luck.
green meklar
2011-03-03 22:28:24 UTC
Even floats and longs have limits. There is a higher type of float called double which has the same basic form as float, but is 64-bit (much like a long is a 64-bit int). Of course, double is subject to the same inaccuracies as float, it sometimes takes longer for the inaccuracies to crop up but double doesn't completely get rid of them. There's a limit to how complicated of a number you can store in 64 bits of memory. Furthermore, 32-bit systems will take somewhat longer to run operations on 64-bit numbers (both double and long) than on the 32-bit counterparts.



It IS possible to have completely accurate integer operations for integers of arbitrary length. However, in order to do it, you need to set up a whole new data structure to store large integers, and a set of functions that can perform operations on them. Some publicly available libraries probably feature data structures of this kind, you can search online and see if you can find a satisfactory one. Otherwise, you can always write your own. Again, keep in mind that operations performed on larger data structures like this will take much longer even than 64-bit primitive operations, and may be prohibitively slow for many purposes.
The Phlebob
2011-03-03 22:20:50 UTC
Floats only have 6-7 significant digits; doubles have 15-16. Neither will be able to handle 20 digits. Long ints go to only 10 significant digits, and even long longs fall short by a digit or so.



Good luck with this.
?
2011-03-03 22:24:29 UTC
Since nobody gave you this info in the answer I will.

The minimum & maximum values for a:

signed 32 bit integer -2,147,483,648 to +2,147,483,647

unsigned 32 bit integer 0 to 4,294,967,295

signed 64 bit integer -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

unsigned 64 bit integer 0 to 18,446,774,073,709,551,615



Nice stuff to know.
2011-03-03 21:59:34 UTC
Try defining them as doubles. The returned values are negative because you are overflowing the capacity of a float. When that happens, they wrap around to negative values.
mark_a_l@sbcglobal.net
2011-03-03 21:54:55 UTC
Perhaps try to use a double or long double?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...