[String] multiplication of large numbers (including floating-point numbers) and multiplication of large numbers

Source: Internet
Author: User

[String] multiplication of large numbers (including floating-point numbers) and multiplication of large numbers

I. Multiplication of large numbers

We know that to calculate the multiplication of two numbers, c and c ++ have special operators *. However, when two numbers exceed a certain range, using common operators will cause overflow and cannot produce correct results. How to perform operations?

First, it is not enough to use a normal integer or floating point type to save a large number. Therefore, we can store large numbers in the form of strings, and then write algorithms to simulate the multiplication process.

1. The first question is: how many digits can be used to save the two numeric computation results?

The maximum value is 9999*9999. The number of digits is smaller than 9 digits of 10000*10000. Therefore, the result of multiplying two digits cannot exceed the sum of two digits.

2. Second question: how to simulate the operation process?

For example, we can first perform multiplication and accumulation, save the result to the corresponding character, and then carry

1 2 3 4

1 2 3

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

3 6 9 12 // a class Multiplication

2 4 6 8

1 2 3 4

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

1 4 10 16 17 12 // accumulate

1 5 1 7 8 2 // carry


However, the problem here is that if the number of two digits is particularly large, the class multiplication is first accumulated, because the maximum value that can be saved by unsigned characters is only 255, during carry-in processing, it is possible that the character stored value has exceeded. Therefore, during processing, we need to perform carry after a class multiplication and then accumulate.


After processing the ideas, you can easily write the code.

Void bignum (char * num1, char * num2) {int length1 = strlen (num1); int lengh2 = strlen (num2); int I, l; char * res = (char *) malloc (sizeof (char) * (length1 + lengh2); // open the corresponding memory memset (res, 0, sizeof (char) * (length1 + leng22.); for (I = length1-1; I> = 0; I --) for (l = leng22-1; l> = 0; l --) {res [I + l + 1] + = (num1 [I]-'0') * (num2 [l]-'0 '); after adding a class, add res [I + l] + = res [I + l + 1]/10; // carry out carry res [I + l + 1] % = 10;} int count = 0; while (res [count] = 0) // because the saved results are from the right to the left, eliminate the 0; {count ++;} char * ret = (char *) on the left *) malloc (sizeof (char) * (length1 + leng2+ 2); memset (ret, 0, sizeof (char) * (length1 + leng22 )); for (l = 0, I = count; I <length1 + leng2; l ++, I ++) // The non-zero part is assigned to ret {ret [l] = res [I] + '0';} printf ("Ret = % s \ n", ret ); free (res); free (ret );}
Result


2. Implement floating point numbers.

To multiply a floating point, it is similar to an integer. You only need to convert the floating point number to an integer, record the two digits and the decimal point, and add the corresponding decimal point to the obtained integer.


Void bignum_float (char * str1, char * str2) {int length1 = strlen (str1); int lengh2 = strlen (str2); int flnum =-1; // int flnum2 =-1; // number of decimal places 2 char num1 [500] = {0}; char num2 [500] = {0 }; int I, l; for (I = 0, l = 0; l <length1; I ++, l ++) {if (str1 [l] = '. ') // after the decimal point is reached, add one {flnum ++; I --; continue;} if (flnum! =-1) flnum ++; // count the number of digits after the decimal point. Because the number is added once, the initial value is-1num1 [I] = str1 [l]. // save to the new array} for (I = 0, l = 0; l <leng2; 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, it is converted to the corresponding length after the integer-1if (flnum2! =-1) leng22-= 1; flnum = flnum + flnum2; char * res = (char *) malloc (sizeof (char) * (length1 + leng22 )); memset (res, 0, sizeof (char) * (length1 + length1); 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 + leng22); memset (ret, 0, sizeof (char) * (length1 + length1 + 2); for (int n = 0, l = 0, I = count; I <length1 + leng2; l ++, I ++, n ++) {if (n = length1 + leng22-count-flnum) // Add the decimal point {ret [l] = 'to the result location '. '; I --; continue;} ret [l] = res [I] + '0';} printf ("Ret = % s \ n", ret ); free (res); free (ret );}

Result:










Related Article

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.