High-Precision computing-subtraction (continued)

Source: Internet
Author: User

Based on the previous post, the modification and subtraction operations are suitable for high-precision floating point computing.

Because the subtraction method is more difficult than the addition method, more considerations are required, and the code may be lacking.

Operation description:

1. The subtraction function remains unchanged, including the size function used to determine the subtrahend and subtravers.

2. added two functions: the decimal point function and the result processing (regression decimal point) function.

3. Compared with addition floating-point high-precision operations, the result processing function is much changed here. After addition, the number of digits is not decreased and the number is increased at most. Subtraction will disappear many places. For the floating point type, that is, the number after the decimal point, we have to make up the zero. Therefore, the function adds several judgments. For details, see the comments of the Code.

The program test results are as follows:

The following code includes four function functions with comments:

# Include "stdio. H "<br/> # include" string. H "</P> <p> /****************************** **************************************** **/<br/>/* high-precision subtraction (this function requires a []> = B []) */<br/>/* A [] subtrahend array with unlimited digits */<br/>/* B [] subtrahend array, unlimited number of digits */<br/>/* back [] result array, the number of digits is not greater than the */<br/>/************************* of the [] array /************************* **************************************** * ******/<br/> void sub (char a [], char B [], char back []) <br/>{< br/> int I, K, L1, L2; <br/> L1 = Strlen (a); <br/> L2 = strlen (B); <br/> back [L1] = '/0 '; <br/> for (I = l2-1, L1 --; I> = 0; L1 --, I --) // The number of subtrahend is less than or equal to the subtrahend, therefore, the number of reductions depends on the number of reductions <br/>{< br/> if (a [L1]-B [I]> = 0) // do not use the offset <br/> {<br/> back [L1] = A [L1]-B [I] + '0 '; <br/>}< br/> else // borrow 1 from the first digit <br/> {<br/> back [L1] = 10 + A [L1]-B [i] + '0 '; <br/> if (a [l1-1]! = '0') // the current one is not 0, can be borrowed directly when, <br/>{< br/> A [l1-1]-= 1; <br/>}< br/> else // if the previous digit is 0, you cannot borrow it. Continue to borrow the previous one from the previous one, loop <br/> {<br/> K = l1-1; <br/> while (A [k] = '0 ') <br/> {<br/> A [k] = '9'; // 0 of the first digit is changed to 10, and the last digit is lent, there are also 9 <br/> K --; <br/>}< br/> A [k]-= 1; // reach the available borrow location <br/>}< br/> while (L1> = 0) // The number of digits that are more than the number of the subtrahend is directly assigned to the result array <br/>{< br/> back [L1] = A [L1]; <br/> L1 --; <br/>}< br/> while (back [0] = '0') // move the result array forward, convenience for future operations <br/>{< br/> L1 = s Trlen (a); <br/> for (I = 0; I <l1-1; I ++) <br/>{< br/> back [I] = back [I + 1]; <br/>}< br/> back [l1-1] = '/0 '; <br/>}< br/> If (strlen (back) = 0) // when the number of subtrahend is equal to 0, <br/>{< br/> back [0] = '0 '; <br/> back [1] = '/0 '; <br/>}</P> <p> /********************** **************************************** * ********/<br/>/* judgment function (the size of the subtrahend and subtrahend is compared) */<br/>/* you can use the strcmp function to compare strings */<br/>/* After comparison, if the value is smaller than the subtraction, it is replaced with the high-precision subtraction function, add '-' to output */<br/> /******* **************************************** * **********************/<Br/> bool isbigger (char a [], char B []) <br/>{< br/> int L1, L2; <br/> L1 = strlen (); <br/> L2 = strlen (B); <br/> If (L1> l2) <br/>{< br/> return true; <br/>}< br/> else if (L1 <l2) <br/>{< br/> return false; <br/>}< br/> else <br/> {<br/> If (strcmp (a, B)> = 0) <br/>{< br/> return true; <br/>}< br/> else <br/>{< br/> return false; <br/>}</ P> <p> /********************************** ***********************************/< br/>/* function functions: return the number of decimal points, and modify array A and array B to be a positive integer. */<br/>/* First, locate the decimal point and compare the number of decimal points. A relatively large number is returned, fill in the missing 0 */<br/> /**************************** **************************************** * ***/<br/> int decimal (char a [], char B []) <br/>{< br/> int I, J, K, L1, L2; <br/> L1 = strlen (); <br/> L2 = strlen (B); <br/> for (I = 0; I <L1; I ++) <br/> {<br/> if (a [I] = '. ') // Locate the decimal point and delete the decimal point <br/>{< br/> for (k = I; k <L1; k ++) <br/>{< br/> A [k] = A [k + 1]; <br/>}< br/> L1 --; // reduce the length <br/> break; <br/>}< br/> for (j = 0; j <L2; j ++) <br/> {<br/> If (B [J] = '. ') <br/> {<br/> for (k = J; k <L2; k ++) <br/>{< br/> B [k] = B [k + 1]; <br/>}< br/> L2 --; <br/> break; <br/>}< br/> K = (l1-i)-(l2-j ); // compare the decimal places of two arrays. <br/> if (I = L1 & J = l2) // if no decimal point exists, return 0 <br/>{< br/> return 0; <br/>}< br/> else if (k> = 0) // subtrahend more, B [] After the array alignment fill 0 <br/>{< br/> while (K! = 0) <br/>{< br/> B [L2 ++] = '0'; <br/> K --; <br/>}< br/> return l1-i; // returns the decimal places of a [] <br/>}< br/> else if (k <0) // For more subtraction, 0 is added after the array a [] <br/>{< br/> K = K *-1; // remember to change K to positive <br/> while (K! = 0) <br/>{< br/> A [L1 ++] = '0'; <br/> K --; <br/>}< br/> return l2-j; <br/>}</P> <p> /********************** **************************************** * *********/<br/>/* processing result array */<br/>/* move the decimal point behind the array, insert the decimal point */<br/>/* When the decimal point is 0, remove it. If the decimal point does not contain any number, remove the decimal point */<br/> /***************************** **************************************** * **/<br/> void result (char C [], int N) <br/>{< br/> int I, J, K, length; <br/> Length = strlen (c); <br/> If (length = 1 & C [0] = '0') // if the result is 0, it will not be processed, exit directly <br/>{< br/> return; <br/>}< br/> If (length <n) // The result length is smaller than the decimal places, then, you must add 0 in addition to the decimal point. <br/>{< br/> J = n + 2; <br/> for (I = length-1; i> = 0; I --) <br/> {<br/> C [-- J] = C [I]; <br/>}< br/> C [0] = '0'; <br/> C [1] = '. '; <br/> for (I = 2; I <j; I ++) <br/>{< br/> C [I] = '0 '; <br/>}< br/> else // The result length is greater than or equal to the decimal point. You can insert the decimal point. (If the value is equal to, add 0) <br/> {<br/> for (I = length; I> length-N; I --) // Array move length-n size <br/>{< br/> C [I] = C [I-1]; <br/>}< br/> C [length-N] = '. '; // insert decimal point <br/> for (I = length; I> = length-N; I --) <br/>{< br/> If (C [I]! = '0' & I! = Length-N) // exit without 0 when it is not on the decimal point <br/>{< br/> break; <br/>}< br/> else // set 0 to the Terminator, excluding the decimal point in specific cases <br/>{< br/> C [I] = '/0 '; <br/>}< br/> length = strlen (c); <br/> If (C [0] = '. ') // If the decimal point is in the first place, move it back and add 0 to the left <br/>{< br/> for (I = length; I> 0; I --) <br/>{< br/> C [I] = C [I-1]; <br/>}< br/> C [0] = '0 '; <br/>}</P> <p> /***************** **************************************** * *************/<br/>/* main function call */<br/> /*** **************************************** * ***************************/<Br/> int main () <br/>{< br/> int N, K; <br/> char a [1000], B [1000], C [1000]; <br/> printf ("% s", "computing times:"); <br/> scanf ("% d", & N ); <br/> while (n --) <br/> {<br/> memset (A, '/0', sizeof ()); <br/> memset (B, '/0', sizeof (B); <br/> memset (C,'/0', sizeof (c )); <br/> printf ("% s", "subtrahend:"); <br/> scanf ("% s", & ); <br/> printf ("% s", "minor:"); <br/> scanf ("% s ",& B); <br/> K = decimal (a, B); // process the decimal point <br/> If (isbigger (a, B )) // The size of the processed subtrahend and subtrahend <br/>{< br/> sub (A, B, C); // subtraction operation <br/> If (K! = 0) <br/>{< br/> result (C, k); // when it is a floating point number, process the result array <br/>}< br/> printf ("% S % s/n", "Result:", C ); <br/>}< br/> else <br/>{< br/> sub (B, A, C); <br/> If (K! = 0) <br/>{< br/> result (C, k ); <br/>}< br/> printf ("% S % C % s/n", "Result:", '-', C ); <br/>}< br/> return 0; <br/>}

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.