-
Notifications
You must be signed in to change notification settings - Fork 0
/
div.cc
177 lines (140 loc) · 3.82 KB
/
div.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
BigNumbers - Arbitrary precision arithmetic
Copyright 2000-2010, 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 <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include "bignum.h"
using namespace std;
// Computes the inverse.
// The problem is equivalent to find a root of the function f(x) = 1/x - 1/A,
// so we can solve it by Newton's method doing the iteration x = x*(2 - A*x),
// that doesn't need any division.
void inv(const BigNumber &A, BigNumber &B) {
static BigNumber x1, x2;
static BigNumber two = 2.0; // TODO: remove
if (!matchDimensions(A, B)) {
throw std::string("dimensions mismatch");
}
if (!matchDimensions(x1, A)) {
x1.resize(A);
x2.resize(A);
two.resize(A);
two.fromDouble(2.0);
}
# ifdef DEBUG
cout << "INV";
cout.flush();
# endif
double a;
long int aexp;
A.toDouble(a, aexp);
B.fromDouble(1 / a, -aexp);
// int ipc = A.firstNonZeroDigitIndex();
//
// B.clear(); // cleaning the result
// B.isPositive = A.isPositive;
//
//
// // TODO: improve first guess
// // if A has order n, 1/A has order -n, so we choose 10^(-n) as a starting
// // point.
// B.digits[BigNumber::N_FRAC_DIGITS - (ipc - BigNumber::N_FRAC_DIGITS) - 1] =
// 1;
for (int k = 0;; k++) {
mul(A, B, x1);
sub(two, x1, x2);
mul(B, x2, x1);
# ifdef DEBUG
cout << '.';
cout.flush();
# endif
if (B == x1) // loop until convergence
break;
B = x1;
}
# ifdef DEBUG
cout << endl;
# endif
}
// Computes the division by the classical Long Division Algorithm.
// A is overlappable with B or C.
void divLDA(const BigNumber &A, const BigNumber &B, BigNumber &C) {
int i, j, n;
int na = 0, nb = 0, nab;
static std::vector<bcd_t> AA;
if (!matchDimensions(A, B) || !matchDimensions(A, C)) {
throw std::string("dimensions mismatch");
}
if ((long int) AA.size() != A.nDigits + A.nFracDigits) {
AA.resize(A.nDigits + A.nFracDigits);
}
// AA is a shifted copy of A.
copy(A.digits.begin(), A.digits.end(),
AA.begin() + A.nFracDigits);
fill(AA.begin(), AA.begin() + A.nFracDigits - 1, 0);
for (i = A.nDigits - 1; i >= 0; i--) {
if ((!na) && (A.digits[i]))
na = i;
if ((!nb) && (B.digits[i]))
nb = i;
}
// AA is shifted BigNumber::N_FRAC_DIGITS digits.
na += A.nFracDigits;
if (!nb && !B.digits[0]) {
printf("ERROR: division by 0\n");
exit(255);
}
C.clear();
C.positive = !(A.positive ^ B.positive);
if (nb > na)
return;
nab = na - nb;
bool menor;
char r, c;
i = 1;
for (i = 0; i <= nab; i++) {
// Tests if AA is lower than a shifted version of B.
for (n = 0;; n++) {
menor = false;
for (j = nb + 1; j >= 0; j--) {
if (AA[j + nab - i] == B.digits[j])
continue;
if (AA[j + nab - i] < B.digits[j])
menor = true;
break;
}
if (menor)
break;
// Substracts a shifted version of B from AA.
for (c = 0, j = 0; j <= nb + 1; j++) {
r = AA[j + nab - i] - (B.digits[j] + c);
c = (r < 0) ? 1 : 0;
AA[j + nab - i] = (r + 10 * c); // r % 10
}
}
C.digits[nab - i] = n;
}
}
void divInv(const BigNumber &A, const BigNumber &B, BigNumber &C) {
inv(B, C);
mul(A, C, C);
}
void div(const BigNumber &A, const BigNumber &B, BigNumber &C) {
#ifdef INVERSE_DIV_ALGORITHM
divInv(A, B, C);
#else
divLDA(A, B, C);
#endif
}