/
addsub.cc
146 lines (110 loc) · 3.26 KB
/
addsub.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
BigNumbers - Arbitrary precision arithmetic
Copyright 2000-2009, Ibán Cereijo Graña <ibancg at gmail dot com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "bignum.h"
// Addition. Simple implementation.
void add(BigNumber &A, BigNumber &B, BigNumber &C, bool sign) {
register int i;
char r;
char carry = 0;
if (A.isPositive == B.isPositive) {
// same sign case
for (i = 0; i < N_DIGITS; i++) {
r = carry + A.digits[i] + B.digits[i];
carry = (r > 9) ? 1 : 0;
C.digits[i] = (r - 10 * carry); // r % 10
}
C.isPositive = A.isPositive;
} else {
// different sign case
BigNumber* M; // higher module BN
BigNumber* m; // lower module BN
M = NULL;
for (i = N_DIGITS - 1; i >= 0; i--) {
if (A.digits[i] == B.digits[i])
continue;
if (A.digits[i] > B.digits[i]) {
M = &A;
m = &B;
} else {
M = &B;
m = &A;
}
break;
}
if (!M) { // both numbers have the same module, so the result is 0
memset(C.digits, 0, N_DIGITS * sizeof(bcd_t));
C.isPositive = sign;
return;
}
// substracts the lower module number from the higher module one
for (i = 0; i < N_DIGITS; i++) {
r = M->digits[i] - (m->digits[i] + carry);
carry = (r < 0) ? 1 : 0;
C.digits[i] = (r + 10 * carry);
}
// if the number with higher module is positive, then the result is also
// positive.
C.isPositive = ((A.isPositive) && (M == &A)) || ((B.isPositive) && (M
== &B));
}
}
// Substraction. Simple implementation.
void sub(BigNumber &A, BigNumber &B, BigNumber &C, bool piz) {
register int i;
char r;
char carry = 0;
if (A.isPositive != B.isPositive) {
// different sign case
for (i = 0; i < N_DIGITS; i++) {
r = carry + A.digits[i] + B.digits[i];
carry = (r > 9) ? 1 : 0;
C.digits[i] = (r - 10 * carry); // r % 10
}
C.isPositive = A.isPositive;
} else {
// same sign case
BigNumber* M; // higher module BN
BigNumber* m; // lower module BN
M = NULL;
for (i = N_DIGITS - 1; i >= 0; i--) {
if (A.digits[i] == B.digits[i])
continue;
if (A.digits[i] > B.digits[i]) {
M = &A;
m = &B;
} else {
M = &B;
m = &A;
}
break;
}
if (!M) { // both numbers have the same module, so the result is 0
memset(C.digits, 0, N_DIGITS * sizeof(bcd_t));
C.isPositive = piz;
return;
}
// substracts the lower module number from the higher module one
for (i = 0; i < N_DIGITS; i++) {
r = M->digits[i] - (m->digits[i] + carry);
carry = (r < 0) ? 1 : 0;
C.digits[i] = (r + 10 * carry);
}
// if the number with higher module is positive, then the result is also
// positive
C.isPositive = ((A.isPositive) && (M == &A)) || ((!B.isPositive) && (M
== &B));
}
}