Large integer algorithm [07] Absolute Subtraction

Source: Internet
Author: User

★ Intro

For the last two weeks, the mainframe has switched from Windows to Linux, and the work environment has moved from the physical machine to the virtual machine. Of course, there is only one goal, which is to give full play to the advantages of Linux and virtual machines to improve their productivity. As the saying goes: Ax, spend some time to toss upgrades or necessary, if you want to talk about the relevant experience, if you would like to be anxious to know, recommend programming random blog (blog Outside the wall, please search the ladder yourself).

        ★ Calculation principle

Well, don't say much nonsense. In the previous article, we talked about the implementation of absolute value addition, which is how to do absolute subtraction. Absolute subtraction is still the written calculation algorithm, starting from the low, not enough to the high borrow, until all the digits have been processed. In order to facilitate the implementation of symbolic number addition and subtraction in the future, it is stated here that the algorithm calculates z = x-y, and the absolute value of x is greater than or equal to Y, otherwise the algorithm returns a negative error.

★ Achieve

Because the principle is relatively simple, so I first put out the code, and then introduced his way of working.

int bn_sub_abs (bignum *z, const bignum *x, const bignum *y) {int ret;    Bn_digit *px, *py, *pz;    size_t i, Min, Max, olduse, T1, T2, C;    max = x->used;    Min = y->used;    if (Bn_cmp_abs (x, Y) < 0) return bn_negative_value_error;    Olduse = z->used;    z->used = max;    Bn_check (Bn_grow (z, z->used));    c = 0;    PX = x->dp;    PY = y->dp;    PZ = z->dp;        for (i = 0; i < min; i++) {t1 = *px++;        t2 = *py++;        *pz++ = t1-t2-c;    if (t1! = t2) c = (T1 < T2);        } for (; i < Max; i++) {t1 = *px++;        *pz++ = t1-c;    if (c! = 0 && T1! = 0) c = 0;    } for (i = max; I < olduse; i++) *pz++ = 0;    Z->sign = 1; Bn_clamp (z); clean:return ret;} 

in absolute subtraction, it is not important to sort the input, because |x| >= |y| is already specified, so x->used is given to Max, y->used to min;t1 and T2 are temporary variables, and C is borrow.

The absolute value of x and Y is checked before the calculation, and a negative error is returned if the conditions specified above are not met.

If the absolute size of x and y check is OK, then the calculation will work, first set the value of borrow to 0, and then set the pointer alias to improve memory access efficiency.

First loop: minus the phase. Assigns each digit of x and y to the temporary variable t1 and T2, calculates the value of the t1-t2-c, and then stores it in the corresponding digit of z, if C = 0, indicating that the low level is not borrow to the high.
After subtracting, judge whether this subtraction need to high-borrow, if the original x in the value of a digit is less than the corresponding digit in Y, then the result of the comparison is 1,c = 1. Note that all calculations are mod 2^n.

Second loop: Abdication and assignment. If Max > min indicates that the number of x is more than Y, then the abdication calculation is required. If C = 0, then there is no abdication, and the remaining digits of X are assigned to the corresponding digits of Z. If C = 1, then there are borrow from the low, in the completion of a abdication calculation, the next one to determine whether the need to abdicate, because the value of C can only be 0 or 1, if the value of this time before the abdication is greater than 0, then the number of digits will not need to be abdicated, so the value of C is set to 0, otherwise, the value of After the abdication calculation is completed, the remaining digits of X are given to Z, which completes the subtraction calculation.

Third loop: High level zeroing. If the subtraction calculation is complete, the high position has no 0 digits and must be emptied, otherwise the result will be wrong.

At the end of all loops, set the symbol to 1, because the final result of absolute subtraction is still a non-negative integer, and finally the extra bit is finished.

         ★ Summary

              subtraction operation is simpler than addition, it is not necessary to consider the problem of single and double precision, as long as you until the written calculation algorithm and understand the computer under the binary complement operation, it is not difficult to write. In the next article, we will construct the algorithm of adding and subtraction of signed numbers based on the comparison algorithm, the absolute value plus minus algorithm.

"Back to this series of catalogs"

Copyright notice
Original blog post, reproduced must contain this statement, to keep this article complete, and in the form of hyperlinks annotated author Starrybird and this article original address: http://www.cnblogs.com/starrybird/p/4399652.html

Large integer algorithm [07] Absolute Subtraction

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.