xref: /aosp_15_r20/external/bc/manuals/algorithms.md (revision 5a6e848804d15c18a0125914844ee4eb0bda4fcf)
1*5a6e8488SAndroid Build Coastguard Worker# Algorithms
2*5a6e8488SAndroid Build Coastguard Worker
3*5a6e8488SAndroid Build Coastguard WorkerThis `bc` uses the math algorithms below:
4*5a6e8488SAndroid Build Coastguard Worker
5*5a6e8488SAndroid Build Coastguard Worker### Addition
6*5a6e8488SAndroid Build Coastguard Worker
7*5a6e8488SAndroid Build Coastguard WorkerThis `bc` uses brute force addition, which is linear (`O(n)`) in the number of
8*5a6e8488SAndroid Build Coastguard Workerdigits.
9*5a6e8488SAndroid Build Coastguard Worker
10*5a6e8488SAndroid Build Coastguard Worker### Subtraction
11*5a6e8488SAndroid Build Coastguard Worker
12*5a6e8488SAndroid Build Coastguard WorkerThis `bc` uses brute force subtraction, which is linear (`O(n)`) in the number
13*5a6e8488SAndroid Build Coastguard Workerof digits.
14*5a6e8488SAndroid Build Coastguard Worker
15*5a6e8488SAndroid Build Coastguard Worker### Multiplication
16*5a6e8488SAndroid Build Coastguard Worker
17*5a6e8488SAndroid Build Coastguard WorkerThis `bc` uses two algorithms: [Karatsuba][1] and brute force.
18*5a6e8488SAndroid Build Coastguard Worker
19*5a6e8488SAndroid Build Coastguard WorkerKaratsuba is used for "large" numbers. ("Large" numbers are defined as any
20*5a6e8488SAndroid Build Coastguard Workernumber with `BC_NUM_KARATSUBA_LEN` digits or larger. `BC_NUM_KARATSUBA_LEN` has
21*5a6e8488SAndroid Build Coastguard Workera sane default, but may be configured by the user.) Karatsuba, as implemented in
22*5a6e8488SAndroid Build Coastguard Workerthis `bc`, is superlinear but subpolynomial (bounded by `O(n^log_2(3))`).
23*5a6e8488SAndroid Build Coastguard Worker
24*5a6e8488SAndroid Build Coastguard WorkerBrute force multiplication is used below `BC_NUM_KARATSUBA_LEN` digits. It is
25*5a6e8488SAndroid Build Coastguard Workerpolynomial (`O(n^2)`), but since Karatsuba requires both more intermediate
26*5a6e8488SAndroid Build Coastguard Workervalues (which translate to memory allocations) and a few more additions, there
27*5a6e8488SAndroid Build Coastguard Workeris a "break even" point in the number of digits where brute force multiplication
28*5a6e8488SAndroid Build Coastguard Workeris faster than Karatsuba. There is a script (`$ROOT/scripts/karatsuba.py`) that
29*5a6e8488SAndroid Build Coastguard Workerwill find the break even point on a particular machine.
30*5a6e8488SAndroid Build Coastguard Worker
31*5a6e8488SAndroid Build Coastguard Worker***WARNING: The Karatsuba script requires Python 3.***
32*5a6e8488SAndroid Build Coastguard Worker
33*5a6e8488SAndroid Build Coastguard Worker### Division
34*5a6e8488SAndroid Build Coastguard Worker
35*5a6e8488SAndroid Build Coastguard WorkerThis `bc` uses Algorithm D ([long division][2]). Long division is polynomial
36*5a6e8488SAndroid Build Coastguard Worker(`O(n^2)`), but unlike Karatsuba, any division "divide and conquer" algorithm
37*5a6e8488SAndroid Build Coastguard Workerreaches its "break even" point with significantly larger numbers. "Fast"
38*5a6e8488SAndroid Build Coastguard Workeralgorithms become less attractive with division as this operation typically
39*5a6e8488SAndroid Build Coastguard Workerreduces the problem size.
40*5a6e8488SAndroid Build Coastguard Worker
41*5a6e8488SAndroid Build Coastguard WorkerWhile the implementation of long division may appear to use the subtractive
42*5a6e8488SAndroid Build Coastguard Workerchunking method, it only uses subtraction to find a quotient digit. It avoids
43*5a6e8488SAndroid Build Coastguard Workerunnecessary work by aligning digits prior to performing subtraction and finding
44*5a6e8488SAndroid Build Coastguard Workera starting guess for the quotient.
45*5a6e8488SAndroid Build Coastguard Worker
46*5a6e8488SAndroid Build Coastguard WorkerSubtraction was used instead of multiplication for two reasons:
47*5a6e8488SAndroid Build Coastguard Worker
48*5a6e8488SAndroid Build Coastguard Worker1.	Division and subtraction can share code (one of the less important goals of
49*5a6e8488SAndroid Build Coastguard Worker	this `bc` is small code).
50*5a6e8488SAndroid Build Coastguard Worker2.	It minimizes algorithmic complexity.
51*5a6e8488SAndroid Build Coastguard Worker
52*5a6e8488SAndroid Build Coastguard WorkerUsing multiplication would make division have the even worse algorithmic
53*5a6e8488SAndroid Build Coastguard Workercomplexity of `O(n^(2*log_2(3)))` (best case) and `O(n^3)` (worst case).
54*5a6e8488SAndroid Build Coastguard Worker
55*5a6e8488SAndroid Build Coastguard Worker### Power
56*5a6e8488SAndroid Build Coastguard Worker
57*5a6e8488SAndroid Build Coastguard WorkerThis `bc` implements [Exponentiation by Squaring][3], which (via Karatsuba) has
58*5a6e8488SAndroid Build Coastguard Workera complexity of `O((n*log(n))^log_2(3))` which is favorable to the
59*5a6e8488SAndroid Build Coastguard Worker`O((n*log(n))^2)` without Karatsuba.
60*5a6e8488SAndroid Build Coastguard Worker
61*5a6e8488SAndroid Build Coastguard Worker### Square Root
62*5a6e8488SAndroid Build Coastguard Worker
63*5a6e8488SAndroid Build Coastguard WorkerThis `bc` implements the fast algorithm [Newton's Method][4] (also known as the
64*5a6e8488SAndroid Build Coastguard WorkerNewton-Raphson Method, or the [Babylonian Method][5]) to perform the square root
65*5a6e8488SAndroid Build Coastguard Workeroperation.
66*5a6e8488SAndroid Build Coastguard Worker
67*5a6e8488SAndroid Build Coastguard WorkerIts complexity is `O(log(n)*n^2)` as it requires one division per iteration, and
68*5a6e8488SAndroid Build Coastguard Workerit doubles the amount of correct digits per iteration.
69*5a6e8488SAndroid Build Coastguard Worker
70*5a6e8488SAndroid Build Coastguard Worker### Sine and Cosine (`bc` Math Library Only)
71*5a6e8488SAndroid Build Coastguard Worker
72*5a6e8488SAndroid Build Coastguard WorkerThis `bc` uses the series
73*5a6e8488SAndroid Build Coastguard Worker
74*5a6e8488SAndroid Build Coastguard Worker```
75*5a6e8488SAndroid Build Coastguard Workerx - x^3/3! + x^5/5! - x^7/7! + ...
76*5a6e8488SAndroid Build Coastguard Worker```
77*5a6e8488SAndroid Build Coastguard Worker
78*5a6e8488SAndroid Build Coastguard Workerto calculate `sin(x)` and `cos(x)`. It also uses the relation
79*5a6e8488SAndroid Build Coastguard Worker
80*5a6e8488SAndroid Build Coastguard Worker```
81*5a6e8488SAndroid Build Coastguard Workercos(x) = sin(x + pi/2)
82*5a6e8488SAndroid Build Coastguard Worker```
83*5a6e8488SAndroid Build Coastguard Worker
84*5a6e8488SAndroid Build Coastguard Workerto calculate `cos(x)`. It has a complexity of `O(n^3)`.
85*5a6e8488SAndroid Build Coastguard Worker
86*5a6e8488SAndroid Build Coastguard Worker**Note**: this series has a tendency to *occasionally* produce an error of 1
87*5a6e8488SAndroid Build Coastguard Worker[ULP][6]. (It is an unfortunate side effect of the algorithm, and there isn't
88*5a6e8488SAndroid Build Coastguard Workerany way around it; [this article][7] explains why calculating sine and cosine,
89*5a6e8488SAndroid Build Coastguard Workerand the other transcendental functions below, within less than 1 ULP is nearly
90*5a6e8488SAndroid Build Coastguard Workerimpossible and unnecessary.) Therefore, I recommend that users do their
91*5a6e8488SAndroid Build Coastguard Workercalculations with the precision (`scale`) set to at least 1 greater than is
92*5a6e8488SAndroid Build Coastguard Workerneeded.
93*5a6e8488SAndroid Build Coastguard Worker
94*5a6e8488SAndroid Build Coastguard Worker### Exponentiation (`bc` Math Library Only)
95*5a6e8488SAndroid Build Coastguard Worker
96*5a6e8488SAndroid Build Coastguard WorkerThis `bc` uses the series
97*5a6e8488SAndroid Build Coastguard Worker
98*5a6e8488SAndroid Build Coastguard Worker```
99*5a6e8488SAndroid Build Coastguard Worker1 + x + x^2/2! + x^3/3! + ...
100*5a6e8488SAndroid Build Coastguard Worker```
101*5a6e8488SAndroid Build Coastguard Worker
102*5a6e8488SAndroid Build Coastguard Workerto calculate `e^x`. Since this only works when `x` is small, it uses
103*5a6e8488SAndroid Build Coastguard Worker
104*5a6e8488SAndroid Build Coastguard Worker```
105*5a6e8488SAndroid Build Coastguard Workere^x = (e^(x/2))^2
106*5a6e8488SAndroid Build Coastguard Worker```
107*5a6e8488SAndroid Build Coastguard Worker
108*5a6e8488SAndroid Build Coastguard Workerto reduce `x`.
109*5a6e8488SAndroid Build Coastguard Worker
110*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n^3)`.
111*5a6e8488SAndroid Build Coastguard Worker
112*5a6e8488SAndroid Build Coastguard Worker**Note**: this series can also produce errors of 1 ULP, so I recommend users do
113*5a6e8488SAndroid Build Coastguard Workertheir calculations with the precision (`scale`) set to at least 1 greater than
114*5a6e8488SAndroid Build Coastguard Workeris needed.
115*5a6e8488SAndroid Build Coastguard Worker
116*5a6e8488SAndroid Build Coastguard Worker### Natural Logarithm (`bc` Math Library Only)
117*5a6e8488SAndroid Build Coastguard Worker
118*5a6e8488SAndroid Build Coastguard WorkerThis `bc` uses the series
119*5a6e8488SAndroid Build Coastguard Worker
120*5a6e8488SAndroid Build Coastguard Worker```
121*5a6e8488SAndroid Build Coastguard Workera + a^3/3 + a^5/5 + ...
122*5a6e8488SAndroid Build Coastguard Worker```
123*5a6e8488SAndroid Build Coastguard Worker
124*5a6e8488SAndroid Build Coastguard Worker(where `a` is equal to `(x - 1)/(x + 1)`) to calculate `ln(x)` when `x` is small
125*5a6e8488SAndroid Build Coastguard Workerand uses the relation
126*5a6e8488SAndroid Build Coastguard Worker
127*5a6e8488SAndroid Build Coastguard Worker```
128*5a6e8488SAndroid Build Coastguard Workerln(x^2) = 2 * ln(x)
129*5a6e8488SAndroid Build Coastguard Worker```
130*5a6e8488SAndroid Build Coastguard Worker
131*5a6e8488SAndroid Build Coastguard Workerto sufficiently reduce `x`.
132*5a6e8488SAndroid Build Coastguard Worker
133*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n^3)`.
134*5a6e8488SAndroid Build Coastguard Worker
135*5a6e8488SAndroid Build Coastguard Worker**Note**: this series can also produce errors of 1 ULP, so I recommend users do
136*5a6e8488SAndroid Build Coastguard Workertheir calculations with the precision (`scale`) set to at least 1 greater than
137*5a6e8488SAndroid Build Coastguard Workeris needed.
138*5a6e8488SAndroid Build Coastguard Worker
139*5a6e8488SAndroid Build Coastguard Worker### Arctangent (`bc` Math Library Only)
140*5a6e8488SAndroid Build Coastguard Worker
141*5a6e8488SAndroid Build Coastguard WorkerThis `bc` uses the series
142*5a6e8488SAndroid Build Coastguard Worker
143*5a6e8488SAndroid Build Coastguard Worker```
144*5a6e8488SAndroid Build Coastguard Workerx - x^3/3 + x^5/5 - x^7/7 + ...
145*5a6e8488SAndroid Build Coastguard Worker```
146*5a6e8488SAndroid Build Coastguard Worker
147*5a6e8488SAndroid Build Coastguard Workerto calculate `atan(x)` for small `x` and the relation
148*5a6e8488SAndroid Build Coastguard Worker
149*5a6e8488SAndroid Build Coastguard Worker```
150*5a6e8488SAndroid Build Coastguard Workeratan(x) = atan(c) + atan((x - c)/(1 + x * c))
151*5a6e8488SAndroid Build Coastguard Worker```
152*5a6e8488SAndroid Build Coastguard Worker
153*5a6e8488SAndroid Build Coastguard Workerto reduce `x` to small enough. It has a complexity of `O(n^3)`.
154*5a6e8488SAndroid Build Coastguard Worker
155*5a6e8488SAndroid Build Coastguard Worker**Note**: this series can also produce errors of 1 ULP, so I recommend users do
156*5a6e8488SAndroid Build Coastguard Workertheir calculations with the precision (`scale`) set to at least 1 greater than
157*5a6e8488SAndroid Build Coastguard Workeris needed.
158*5a6e8488SAndroid Build Coastguard Worker
159*5a6e8488SAndroid Build Coastguard Worker### Bessel (`bc` Math Library Only)
160*5a6e8488SAndroid Build Coastguard Worker
161*5a6e8488SAndroid Build Coastguard WorkerThis `bc` uses the series
162*5a6e8488SAndroid Build Coastguard Worker
163*5a6e8488SAndroid Build Coastguard Worker```
164*5a6e8488SAndroid Build Coastguard Workerx^n/(2^n * n!) * (1 - x^2 * 2 * 1! * (n + 1)) + x^4/(2^4 * 2! * (n + 1) * (n + 2)) - ...
165*5a6e8488SAndroid Build Coastguard Worker```
166*5a6e8488SAndroid Build Coastguard Worker
167*5a6e8488SAndroid Build Coastguard Workerto calculate the bessel function (integer order only).
168*5a6e8488SAndroid Build Coastguard Worker
169*5a6e8488SAndroid Build Coastguard WorkerIt also uses the relation
170*5a6e8488SAndroid Build Coastguard Worker
171*5a6e8488SAndroid Build Coastguard Worker```
172*5a6e8488SAndroid Build Coastguard Workerj(-n,x) = (-1)^n * j(n,x)
173*5a6e8488SAndroid Build Coastguard Worker```
174*5a6e8488SAndroid Build Coastguard Worker
175*5a6e8488SAndroid Build Coastguard Workerto calculate the bessel when `x < 0`, It has a complexity of `O(n^3)`.
176*5a6e8488SAndroid Build Coastguard Worker
177*5a6e8488SAndroid Build Coastguard Worker**Note**: this series can also produce errors of 1 ULP, so I recommend users do
178*5a6e8488SAndroid Build Coastguard Workertheir calculations with the precision (`scale`) set to at least 1 greater than
179*5a6e8488SAndroid Build Coastguard Workeris needed.
180*5a6e8488SAndroid Build Coastguard Worker
181*5a6e8488SAndroid Build Coastguard Worker### Modular Exponentiation
182*5a6e8488SAndroid Build Coastguard Worker
183*5a6e8488SAndroid Build Coastguard WorkerThis `dc` uses the [Memory-efficient method][8] to compute modular
184*5a6e8488SAndroid Build Coastguard Workerexponentiation. The complexity is `O(e*n^2)`, which may initially seem
185*5a6e8488SAndroid Build Coastguard Workerinefficient, but `n` is kept small by maintaining small numbers. In practice, it
186*5a6e8488SAndroid Build Coastguard Workeris extremely fast.
187*5a6e8488SAndroid Build Coastguard Worker
188*5a6e8488SAndroid Build Coastguard Worker### Non-Integer Exponentiation (`bc` Math Library 2 Only)
189*5a6e8488SAndroid Build Coastguard Worker
190*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `p(x,y)`.
191*5a6e8488SAndroid Build Coastguard Worker
192*5a6e8488SAndroid Build Coastguard WorkerThe algorithm used is to use the formula `e(y*l(x))`.
193*5a6e8488SAndroid Build Coastguard Worker
194*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n^3)` because both `e()` and `l()` do.
195*5a6e8488SAndroid Build Coastguard Worker
196*5a6e8488SAndroid Build Coastguard WorkerHowever, there are details to this algorithm, described by the author,
197*5a6e8488SAndroid Build Coastguard WorkerTediusTimmy, in GitHub issue [#69][12].
198*5a6e8488SAndroid Build Coastguard Worker
199*5a6e8488SAndroid Build Coastguard WorkerFirst, check if the exponent is 0. If it is, return 1 at the appropriate
200*5a6e8488SAndroid Build Coastguard Worker`scale`.
201*5a6e8488SAndroid Build Coastguard Worker
202*5a6e8488SAndroid Build Coastguard WorkerNext, check if the number is 0. If so, check if the exponent is greater than
203*5a6e8488SAndroid Build Coastguard Workerzero; if it is, return 0. If the exponent is less than 0, error (with a divide
204*5a6e8488SAndroid Build Coastguard Workerby 0) because that is undefined.
205*5a6e8488SAndroid Build Coastguard Worker
206*5a6e8488SAndroid Build Coastguard WorkerNext, check if the exponent is actually an integer, and if it is, use the
207*5a6e8488SAndroid Build Coastguard Workerexponentiation operator.
208*5a6e8488SAndroid Build Coastguard Worker
209*5a6e8488SAndroid Build Coastguard WorkerAt the `z=0` line is the start of the meat of the new code.
210*5a6e8488SAndroid Build Coastguard Worker
211*5a6e8488SAndroid Build Coastguard Worker`z` is set to zero as a flag and as a value. What I mean by that will be clear
212*5a6e8488SAndroid Build Coastguard Workerlater.
213*5a6e8488SAndroid Build Coastguard Worker
214*5a6e8488SAndroid Build Coastguard WorkerThen we check if the number is less than 0. If it is, we negate the exponent
215*5a6e8488SAndroid Build Coastguard Worker(and the integer version of the exponent, which we calculated earlier to check
216*5a6e8488SAndroid Build Coastguard Workerif it was an integer). We also save the number in `z`; being non-zero is a flag
217*5a6e8488SAndroid Build Coastguard Workerfor later and a value to be used. Then we store the reciprocal of the number in
218*5a6e8488SAndroid Build Coastguard Workeritself.
219*5a6e8488SAndroid Build Coastguard Worker
220*5a6e8488SAndroid Build Coastguard WorkerAll of the above paragraph will not make sense unless you remember the
221*5a6e8488SAndroid Build Coastguard Workerrelationship `l(x) == -l(1/x)`; we negated the exponent, which is equivalent to
222*5a6e8488SAndroid Build Coastguard Workerthe negative sign in that relationship, and we took the reciprocal of the
223*5a6e8488SAndroid Build Coastguard Workernumber, which is equivalent to the reciprocal in the relationship.
224*5a6e8488SAndroid Build Coastguard Worker
225*5a6e8488SAndroid Build Coastguard WorkerBut what if the number is negative? We ignore that for now because we eventually
226*5a6e8488SAndroid Build Coastguard Workercall `l(x)`, which will raise an error if `x` is negative.
227*5a6e8488SAndroid Build Coastguard Worker
228*5a6e8488SAndroid Build Coastguard WorkerNow, we can keep going.
229*5a6e8488SAndroid Build Coastguard Worker
230*5a6e8488SAndroid Build Coastguard WorkerIf at this point, the exponent is negative, we need to use the original formula
231*5a6e8488SAndroid Build Coastguard Worker(`e(y * l(x))`) and return that result because the result will go to zero
232*5a6e8488SAndroid Build Coastguard Workeranyway.
233*5a6e8488SAndroid Build Coastguard Worker
234*5a6e8488SAndroid Build Coastguard WorkerBut if we did *not* return, we know the exponent is *not* negative, so we can
235*5a6e8488SAndroid Build Coastguard Workerget clever.
236*5a6e8488SAndroid Build Coastguard Worker
237*5a6e8488SAndroid Build Coastguard WorkerWe then compute the integral portion of the power by computing the number to
238*5a6e8488SAndroid Build Coastguard Workerpower of the integral portion of the exponent.
239*5a6e8488SAndroid Build Coastguard Worker
240*5a6e8488SAndroid Build Coastguard WorkerThen we have the most clever trick: we add the length of that integer power (and
241*5a6e8488SAndroid Build Coastguard Workera little extra) to the `scale`. Why? Because this will ensure that the next part
242*5a6e8488SAndroid Build Coastguard Workeris calculated to at least as many digits as should be in the integer *plus* any
243*5a6e8488SAndroid Build Coastguard Workerextra `scale` that was wanted.
244*5a6e8488SAndroid Build Coastguard Worker
245*5a6e8488SAndroid Build Coastguard WorkerThen we check `z`, which, if it is not zero, is the original value of the
246*5a6e8488SAndroid Build Coastguard Workernumber. If it is not zero, we need to take the take the reciprocal *again*
247*5a6e8488SAndroid Build Coastguard Workerbecause now we have the correct `scale`. And we *also* have to calculate the
248*5a6e8488SAndroid Build Coastguard Workerinteger portion of the power again.
249*5a6e8488SAndroid Build Coastguard Worker
250*5a6e8488SAndroid Build Coastguard WorkerThen we need to calculate the fractional portion of the number. We do this by
251*5a6e8488SAndroid Build Coastguard Workerusing the original formula, but we instead of calculating `e(y * l(x))`, we
252*5a6e8488SAndroid Build Coastguard Workercalculate `e((y - a) * l(x))`, where `a` is the integer portion of `y`. It's
253*5a6e8488SAndroid Build Coastguard Workereasy to see that `y - a` will be just the fractional portion of `y` (the
254*5a6e8488SAndroid Build Coastguard Workerexponent), so this makes sense.
255*5a6e8488SAndroid Build Coastguard Worker
256*5a6e8488SAndroid Build Coastguard WorkerBut then we *multiply* it into the integer portion of the power. Why? Because
257*5a6e8488SAndroid Build Coastguard Workerremember: we're dealing with an exponent and a power; the relationship is
258*5a6e8488SAndroid Build Coastguard Worker`x^(y+z) == (x^y)*(x^z)`.
259*5a6e8488SAndroid Build Coastguard Worker
260*5a6e8488SAndroid Build Coastguard WorkerSo we multiply it into the integer portion of the power.
261*5a6e8488SAndroid Build Coastguard Worker
262*5a6e8488SAndroid Build Coastguard WorkerFinally, we set the result to the `scale`.
263*5a6e8488SAndroid Build Coastguard Worker
264*5a6e8488SAndroid Build Coastguard Worker### Rounding (`bc` Math Library 2 Only)
265*5a6e8488SAndroid Build Coastguard Worker
266*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `r(x,p)`.
267*5a6e8488SAndroid Build Coastguard Worker
268*5a6e8488SAndroid Build Coastguard WorkerThe algorithm is a simple method to check if rounding away from zero is
269*5a6e8488SAndroid Build Coastguard Workernecessary, and if so, adds `1e10^p`.
270*5a6e8488SAndroid Build Coastguard Worker
271*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n)` because of add.
272*5a6e8488SAndroid Build Coastguard Worker
273*5a6e8488SAndroid Build Coastguard Worker### Ceiling (`bc` Math Library 2 Only)
274*5a6e8488SAndroid Build Coastguard Worker
275*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `ceil(x,p)`.
276*5a6e8488SAndroid Build Coastguard Worker
277*5a6e8488SAndroid Build Coastguard WorkerThe algorithm is a simple add of one less decimal place than `p`.
278*5a6e8488SAndroid Build Coastguard Worker
279*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n)` because of add.
280*5a6e8488SAndroid Build Coastguard Worker
281*5a6e8488SAndroid Build Coastguard Worker### Factorial (`bc` Math Library 2 Only)
282*5a6e8488SAndroid Build Coastguard Worker
283*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `f(n)`.
284*5a6e8488SAndroid Build Coastguard Worker
285*5a6e8488SAndroid Build Coastguard WorkerThe algorithm is a simple multiplication loop.
286*5a6e8488SAndroid Build Coastguard Worker
287*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n^3)` because of linear amount of `O(n^2)`
288*5a6e8488SAndroid Build Coastguard Workermultiplications.
289*5a6e8488SAndroid Build Coastguard Worker
290*5a6e8488SAndroid Build Coastguard Worker### Permutations (`bc` Math Library 2 Only)
291*5a6e8488SAndroid Build Coastguard Worker
292*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `perm(n,k)`.
293*5a6e8488SAndroid Build Coastguard Worker
294*5a6e8488SAndroid Build Coastguard WorkerThe algorithm is to use the formula `n!/(n-k)!`.
295*5a6e8488SAndroid Build Coastguard Worker
296*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n^3)` because of the division and factorials.
297*5a6e8488SAndroid Build Coastguard Worker
298*5a6e8488SAndroid Build Coastguard Worker### Combinations (`bc` Math Library 2 Only)
299*5a6e8488SAndroid Build Coastguard Worker
300*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `comb(n,r)`.
301*5a6e8488SAndroid Build Coastguard Worker
302*5a6e8488SAndroid Build Coastguard WorkerThe algorithm is to use the formula `n!/r!*(n-r)!`.
303*5a6e8488SAndroid Build Coastguard Worker
304*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n^3)` because of the division and factorials.
305*5a6e8488SAndroid Build Coastguard Worker
306*5a6e8488SAndroid Build Coastguard Worker### Logarithm of Any Base (`bc` Math Library 2 Only)
307*5a6e8488SAndroid Build Coastguard Worker
308*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `log(x,b)`.
309*5a6e8488SAndroid Build Coastguard Worker
310*5a6e8488SAndroid Build Coastguard WorkerThe algorithm is to use the formula `l(x)/l(b)` with double the `scale` because
311*5a6e8488SAndroid Build Coastguard Workerthere is no good way of knowing how many digits of precision are needed when
312*5a6e8488SAndroid Build Coastguard Workerswitching bases.
313*5a6e8488SAndroid Build Coastguard Worker
314*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n^3)` because of the division and `l()`.
315*5a6e8488SAndroid Build Coastguard Worker
316*5a6e8488SAndroid Build Coastguard Worker### Logarithm of Base 2 (`bc` Math Library 2 Only)
317*5a6e8488SAndroid Build Coastguard Worker
318*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `l2(x)`.
319*5a6e8488SAndroid Build Coastguard Worker
320*5a6e8488SAndroid Build Coastguard WorkerThis is a convenience wrapper around `log(x,2)`.
321*5a6e8488SAndroid Build Coastguard Worker
322*5a6e8488SAndroid Build Coastguard Worker### Logarithm of Base 10 (`bc` Math Library 2 Only)
323*5a6e8488SAndroid Build Coastguard Worker
324*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `l10(x)`.
325*5a6e8488SAndroid Build Coastguard Worker
326*5a6e8488SAndroid Build Coastguard WorkerThis is a convenience wrapper around `log(x,10)`.
327*5a6e8488SAndroid Build Coastguard Worker
328*5a6e8488SAndroid Build Coastguard Worker### Root (`bc` Math Library 2 Only)
329*5a6e8488SAndroid Build Coastguard Worker
330*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `root(x,n)`.
331*5a6e8488SAndroid Build Coastguard Worker
332*5a6e8488SAndroid Build Coastguard WorkerThe algorithm is [Newton's method][9]. The initial guess is calculated as
333*5a6e8488SAndroid Build Coastguard Worker`10^ceil(length(x)/n)`.
334*5a6e8488SAndroid Build Coastguard Worker
335*5a6e8488SAndroid Build Coastguard WorkerLike square root, its complexity is `O(log(n)*n^2)` as it requires one division
336*5a6e8488SAndroid Build Coastguard Workerper iteration, and it doubles the amount of correct digits per iteration.
337*5a6e8488SAndroid Build Coastguard Worker
338*5a6e8488SAndroid Build Coastguard Worker### Cube Root (`bc` Math Library 2 Only)
339*5a6e8488SAndroid Build Coastguard Worker
340*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `cbrt(x)`.
341*5a6e8488SAndroid Build Coastguard Worker
342*5a6e8488SAndroid Build Coastguard WorkerThis is a convenience wrapper around `root(x,3)`.
343*5a6e8488SAndroid Build Coastguard Worker
344*5a6e8488SAndroid Build Coastguard Worker### Greatest Common Divisor (`bc` Math Library 2 Only)
345*5a6e8488SAndroid Build Coastguard Worker
346*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `gcd(a,b)`.
347*5a6e8488SAndroid Build Coastguard Worker
348*5a6e8488SAndroid Build Coastguard WorkerThe algorithm is an iterative version of the [Euclidean Algorithm][10].
349*5a6e8488SAndroid Build Coastguard Worker
350*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n^4)` because it has a linear number of divisions.
351*5a6e8488SAndroid Build Coastguard Worker
352*5a6e8488SAndroid Build Coastguard WorkerThis function ensures that `a` is always bigger than `b` before starting the
353*5a6e8488SAndroid Build Coastguard Workeralgorithm.
354*5a6e8488SAndroid Build Coastguard Worker
355*5a6e8488SAndroid Build Coastguard Worker### Least Common Multiple (`bc` Math Library 2 Only)
356*5a6e8488SAndroid Build Coastguard Worker
357*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `lcm(a,b)`.
358*5a6e8488SAndroid Build Coastguard Worker
359*5a6e8488SAndroid Build Coastguard WorkerThe algorithm uses the formula `a*b/gcd(a,b)`.
360*5a6e8488SAndroid Build Coastguard Worker
361*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n^4)` because of `gcd()`.
362*5a6e8488SAndroid Build Coastguard Worker
363*5a6e8488SAndroid Build Coastguard Worker### Pi (`bc` Math Library 2 Only)
364*5a6e8488SAndroid Build Coastguard Worker
365*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `pi(s)`.
366*5a6e8488SAndroid Build Coastguard Worker
367*5a6e8488SAndroid Build Coastguard WorkerThe algorithm uses the formula `4*a(1)`.
368*5a6e8488SAndroid Build Coastguard Worker
369*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n^3)` because of arctangent.
370*5a6e8488SAndroid Build Coastguard Worker
371*5a6e8488SAndroid Build Coastguard Worker### Tangent (`bc` Math Library 2 Only)
372*5a6e8488SAndroid Build Coastguard Worker
373*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `t(x)`.
374*5a6e8488SAndroid Build Coastguard Worker
375*5a6e8488SAndroid Build Coastguard WorkerThe algorithm uses the formula `s(x)/c(x)`.
376*5a6e8488SAndroid Build Coastguard Worker
377*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n^3)` because of sine, cosine, and division.
378*5a6e8488SAndroid Build Coastguard Worker
379*5a6e8488SAndroid Build Coastguard Worker### Atan2 (`bc` Math Library 2 Only)
380*5a6e8488SAndroid Build Coastguard Worker
381*5a6e8488SAndroid Build Coastguard WorkerThis is implemented in the function `a2(y,x)`.
382*5a6e8488SAndroid Build Coastguard Worker
383*5a6e8488SAndroid Build Coastguard WorkerThe algorithm uses the [standard formulas][11].
384*5a6e8488SAndroid Build Coastguard Worker
385*5a6e8488SAndroid Build Coastguard WorkerIt has a complexity of `O(n^3)` because of arctangent.
386*5a6e8488SAndroid Build Coastguard Worker
387*5a6e8488SAndroid Build Coastguard Worker[1]: https://en.wikipedia.org/wiki/Karatsuba_algorithm
388*5a6e8488SAndroid Build Coastguard Worker[2]: https://en.wikipedia.org/wiki/Long_division
389*5a6e8488SAndroid Build Coastguard Worker[3]: https://en.wikipedia.org/wiki/Exponentiation_by_squaring
390*5a6e8488SAndroid Build Coastguard Worker[4]: https://en.wikipedia.org/wiki/Newton%27s_method#Square_root_of_a_number
391*5a6e8488SAndroid Build Coastguard Worker[5]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
392*5a6e8488SAndroid Build Coastguard Worker[6]: https://en.wikipedia.org/wiki/Unit_in_the_last_place
393*5a6e8488SAndroid Build Coastguard Worker[7]: https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT
394*5a6e8488SAndroid Build Coastguard Worker[8]: https://en.wikipedia.org/wiki/Modular_exponentiation#Memory-efficient_method
395*5a6e8488SAndroid Build Coastguard Worker[9]: https://en.wikipedia.org/wiki/Root-finding_algorithms#Newton's_method_(and_similar_derivative-based_methods)
396*5a6e8488SAndroid Build Coastguard Worker[10]: https://en.wikipedia.org/wiki/Euclidean_algorithm
397*5a6e8488SAndroid Build Coastguard Worker[11]: https://en.wikipedia.org/wiki/Atan2#Definition_and_computation
398*5a6e8488SAndroid Build Coastguard Worker[12]: https://github.com/gavinhoward/bc/issues/69
399