Multiplication of "string" large numbers (including floating-point numbers)

Source: Internet
Author: User

I. Large-Number multiplication

We know that to calculate the multiplication of two numbers, the C, C + + language has a special operator *. But when two numbers exceed a certain range, an overflow is generated with a normal operator, and the correct result is not obtained. How do you do the math?

First, to save a large number, it is not enough to use normal shaping or floating-point types. So we can save the large number in the form of a string, and then write the algorithm to simulate the multiplication process.

1. The first question: How many bits of string are stored for the two numeric operations results?

Two 4-digit multiplication with a maximum value of 9999*9999 the number of bits is less than 9 bits of 10000*10000, so the result of multiplying two numbers is not more than two digits in number and

2. Second question: How is the process of the operation simulated?

For example: we can do class multiplication and accumulation first, save the result to the corresponding character character, and carry it.

1 2 3 4

1 2 3

--------------------

3 6 9 12//One-time class multiply

2 4 6 8

1 2 3 4

--------------------------

1 4 10 16 17 12//Cumulative

1 5 1 7 8 2//rounding


However, the problem here is that, if the number of two digits is particularly large, the first class multiplication accumulation, because the unsigned character can save the maximum value is only 255, the last bit processing, it is possible that the character saved value has overflowed. Therefore, when the processing is done, the class multiply after the rounding, and then accumulate.


When you're done with your ideas, it's easier to write code.

void Bignum (char *num1, char *num2) {int length1 = strlen (NUM1); int length2 = strlen (num2); int i, L;char *res = (char *) MAL LOC (sizeof (char) * (length1 + length2)); Open the corresponding memory memset (res, 0, sizeof (char) * (length1 + length2)), for (i = length1-1; I >= 0; i--) for (L = length2-1; L > = 0; l--) {res[i + L + 1] + = (num1[i]-' 0 ') * (Num2[l]-' 0 '); class multiply cumulative res[i + l] + = res[i + L + 1]/ten;    Carry Res[i + L + 1]%= 10; int count = 0;while (Res[count] = = 0)  //Because the saved result is right-to-left, remove the 0;{count++ from the left part;} char* ret = (char *) malloc (sizeof (char) * (length1 + length2 + 2)) memset (ret, 0, sizeof (char) * (length1 + length2 + 2)); for (L = 0, i = count; I < length1 + length2; l++, i++)  Not 0 parts assigned to ret{ret[l] = res[i] + ' 0 ';} printf ("ret=%s\n", ret); free (res); free (ret);}
Results


Two: Realize floating-point number.

To multiply a floating-point number, it is almost the same as an integer. You just have to convert the float to an integer, and record the number of digits in the two-digit decimal point, and then add the corresponding decimal point to the resulting integer.


void Bignum_float (char *str1, char *str2) {int length1 = strlen (str1); int length2 = strlen (str2); int flnum =-1;   The number of decimal places in large numbers 1 int flnum2 =-1; Number 2 decimal places char num1[500] = {0};char num2[500] = {0};int i, l;for (i=0,l=0; l < length1;i++,l++) {if (str1[l] = = '. ') )//After encountering a decimal point, add a {flnum++;i--;continue;} if (flnum!=-1) flnum++;  Statistics the number of digits after the decimal point, due to the extra time, so the initial value is -1num1[i] = str1[l]; Save to the new array}for (i = 0,l = 0; l < length2; i++, l++) {if (str2[l] = = '. ') {flnum2++;i--;continue;} if (flnum2!=-1) flnum2++;num2[i] = str2[l];}   if (flnum!=-1) length1-= 1; If the number has a decimal point, the corresponding length is converted to an integer -1if (flnum2!=-1) length2-= 1;flnum = Flnum + Flnum2;char *res = (char *) malloc (sizeof (char) * (len Gth1 + length2)) memset (res, 0, sizeof (char) * (length1 + length2)); for (i = length1-1; i>=0;i--) for (l=length2-1; l > = 0; l--) {res[i+l+1]+= (num1[i]-' 0 ') * (Num2[l]-' 0 '); Res[i + L] + = res[i + L + 1]/10;res[i + L + 1]%= 10;} int count = 0;while (Res[count] = = 0) {count++;} char* ret = (char *) malloc (sizeof (char) * (Length1 + length2+2)); memset (ret, 0, sizeof (char) * (length1 + length2 + 2)); for (int n=0,l = 0,i=count; I < length1 + length2; l++, i++,n++) {if (n = = length1 + length2-count-flnum)//in the corresponding position of the result plus the decimal point {ret[l] = '. '; I--;continue;} RET[L] = res[i] + ' 0 ';} printf ("ret=%s\n", ret); free (res); free (ret);}

Results:










Multiplication of "string" large numbers (including floating-point numbers)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.