Big integer algorithm [07] absolute value subtraction and absolute value Subtraction

Source: Internet
Author: User

Big integer algorithm [07] absolute value subtraction and absolute value Subtraction

★Introduction 

In the last two weeks, the host has been moved from Windows to Linux, and the working environment has been transferred from the physical machine to the virtual machine. Of course, there is only one purpose, that is, to give full play to the advantages of Linux and virtual machines to improve their work efficiency. As the saying goes: it is necessary to take some time to make upgrades. If you have time, you can talk about related experience. If you want to know more, we recommend a blog that you want to write programming, please search for a ladder by yourself ).

 

★Computing Principle

Okay. You don't have to talk about it. In the previous article, we talked about the implementation of absolute value addition. This time we will talk about how to do the absolute value subtraction. The method of absolute value Subtraction is still the pen calculation algorithm, which is reduced from the low position. It is not enough to borrow from the high bit until all the digits are processed. To facilitate the implementation of addition and subtraction of signed numbers in the future, it is stipulated 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 number.

 

★Implementation

Because the principle is relatively simple, I first paste the code and then introduce how it works.

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 the absolute value subtraction, sorting input is not important because | x |> = | y | has been specified before. Therefore, the x-> used is directly given to max, y-> used is given to min; t1 and t2 are temporary variables, and c is the borrow space.

Before calculation, check the absolute values of x and y. If the preceding conditions are not met, a negative number is returned.

If the size of the absolute values of x and y is normal, the calculation is normal. First, set the value of the borrow value to 0, and then set the pointer alias to improve the memory access efficiency.

The first cycle: offset. Assign each digit of x and y to the temporary variables t1 and t2, calculate the value of t1-t2-c, and store the values in the corresponding digits of z. If c = 0, this indicates that the low position is not used as the high position. After the subtraction is complete, determine whether the subtraction needs to take the high-bitwise bits. If the value of a certain digit in x is less than the value of the corresponding digit in y, the comparison result is 1, c = 1. Note that all calculations are mod 2 ^ n.

The second loop: return and assign values. If max> min, the number of digits of x is greater than that of y. Therefore, the return value must be calculated. If c = 0, no backspace exists. Assign the remainder of x to the corresponding number of z. If c = 1, there are also low-level bits. After a return calculation is completed, determine whether the next bits need to be returned. Because the value of c can only be 0 or 1, if the value of this digit is greater than 0 before this return calculation, the value of c is set to 0, otherwise the return value is 1. After the return value is calculated, the remainder of x is given to z to complete the subtraction.

The third cycle is high and zero. If there are digits not 0 in the upper position after the subtraction calculation, the digits must be cleared; otherwise, an error will occur.

After all cycles end, set the symbol to 1, because the final result of the absolute value Subtraction is still a non-negative integer, and compress the excess digits to complete the computation.

 

★Summary

The subtraction operation is simpler than the addition operation, mainly because it does not need to consider Single and Double Precision. As long as you know the pen algorithm and understand the binary complement operation in the computer, it is not difficult to implement it. In the next article, based on the comparison algorithm established previously, the absolute value addition and subtraction algorithm is used to construct a number of addition and subtraction algorithms.

 

 

[Back to the directory of this series]

 

Copyright Notice
Original blog, reproduced must contain this statement, to maintain the integrity of this article, and in the form of hyperlink to indicate the author Starrybird and the original address of this article: http://www.cnblogs.com/starrybird/p/4399652.html

 


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.