Big integer algorithm [08] signed addition and subtraction, addition and subtraction
★Introduction
The previous articles introduced comparison operations, absolute value addition and absolute value subtraction. Now we can use these algorithms to create addition and subtraction algorithms for signed numbers.
★Addition of signed numbers
The addition of signed numbers is divided into two types: Same number and different numbers. 1. If two numbers have the same number, the absolute value addition is executed. If the two numbers are not negative, the result is non-negative. If both numbers are negative, the result is also negative. 2. If there are two different numbers, perform the absolute value subtraction and subtract the smaller absolute value from the number with a large absolute value. The final result is determined by the absolute values of x and y. If the absolute values of x are greater than or equal to y, the z symbol is the same as x (note that-0 may occur here); otherwise, the z symbol is opposite to x.
int bn_add_bn(bignum *z, const bignum *x, const bignum *y){ int ret; int sign; sign = x->sign; if(x->sign == y->sign) { BN_CHECK(bn_add_abs(z, x, y)); z->sign = sign; } else { if(bn_cmp_abs(x, y) >= 0) { BN_CHECK(bn_sub_abs(z, x, y)); z->sign = sign; } else { BN_CHECK(bn_sub_abs(z, y, x)); z->sign = -sign; } } if(BN_IS_ZERO(z)) z->sign = 1;clean: return ret;}
It should be noted that, if two different numbers have equal but absolute values, the phenomenon of-0 may occur (for example, x =-a, y = a), which is inconsistent with the previous rules, therefore, add a sentence at the end of the judgment. If z = 0, set the symbol bit to 1. BN_IS_ZERO is a macro definition:
# Define BN_IS_ZERO (x) (x-> used = 0 )? 1: 0)
★Subtraction of signed numbers
Similar to addition of a symbolic number, subtraction of a symbolic number is also divided into two situations:
1. Two different numbers: Execute the absolute value addition. The result z is determined by x. If x is not negative, z is positive. If x is negative, z is negative.
2. Two numbers with the same number: Execute the absolute value subtraction, and use a number with a large absolute value to subtract a smaller absolute value. Result The z symbol is determined by the absolute values of x and y. If the absolute values of x are greater than or equal to the absolute values of y, z and x are the same (-0 may occur ), otherwise, the z and x are different.
int bn_sub_bn(bignum *z, const bignum *x, const bignum *y){ int ret; int sign; sign = x->sign; if(x->sign != y->sign) { BN_CHECK(bn_add_abs(z, x, y)); z->sign = sign; } else { if(bn_cmp_abs(x, y) >= 0) { BN_CHECK(bn_sub_abs(z, x, y)); z->sign = sign; } else { BN_CHECK(bn_sub_abs(z, y, x)); z->sign = -sign; } } if(BN_IS_ZERO(z)) z->sign = 1;clean: return ret;}
Similarly, to avoid the occurrence of-0, add a judgment on whether z is equal to 0 at the end.
★Single-digit addition and subtraction
A single-digit algorithm is used to calculate a large integer and a single-precision number. These two algorithms are useful in processing small-scale data addition and subtraction. For single-digit addition and subtraction, the default input is a large integer and a signed Single-precision number, and the result is a large integer. In terms of processing, we do not re-Compile two algorithms. Instead, we assign the single-precision value to a temporary bignum variable and then use the above two signed number algorithms for calculation.
1. single-digit addition:
int bn_add_int(bignum *z, const bignum *x, const bn_sint y){ int ret; bignum t[1]; bn_digit p[1]; p[0] = (y >= 0) ? y : -y; t->used = (y != 0) ? 1 : 0; t->sign = (y >= 0) ? 1 : -1; t->dp = p; t->alloc = 1; BN_CHECK(bn_add_bn(z, x, t));clean: return ret;}
2. single-digit subtraction:
int bn_sub_int(bignum *z, const bignum *x, const bn_sint y){ int ret; bignum t[1]; bn_digit p[1]; p[0] = (y >= 0) ? y : -y; t->used = (y != 0) ? 1 : 0; t->sign = (y >= 0) ? 1 : -1; t->dp = p; t->alloc = 1; BN_CHECK(bn_sub_bn(z, x, t));clean: return ret;}
★Summary
At this point, the addition and subtraction algorithms of big integers are finished. The key to the addition and subtraction is the classification idea. In this way, we can simplify the complicated problems and break them apart. The next few articles will focus on multiplication calculation. Multiplication is more complex than addition and subtraction. In addition, multiplication is time-consuming During calculation, so we need to do a lot of optimization work, otherwise, the power multiplication will be very time-consuming.
[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/4401863.html