3 This `bc` uses the math algorithms below:
7 This `bc` uses brute force addition, which is linear (`O(n)`) in the number of
12 This `bc` uses brute force subtraction, which is linear (`O(n)`) in the number
17 This `bc` uses two algorithms: [Karatsuba][1] and brute force.
19 Karatsuba is used for "large" numbers. ("Large" numbers are defined as any
20 number with `BC_NUM_KARATSUBA_LEN` digits or larger. `BC_NUM_KARATSUBA_LEN` has
21 a sane default, but may be configured by the user.) Karatsuba, as implemented in
22 this `bc`, is superlinear but subpolynomial (bounded by `O(n^log_2(3))`).
24 Brute force multiplication is used below `BC_NUM_KARATSUBA_LEN` digits. It is
25 polynomial (`O(n^2)`), but since Karatsuba requires both more intermediate
26 values (which translate to memory allocations) and a few more additions, there
27 is a "break even" point in the number of digits where brute force multiplication
28 is faster than Karatsuba. There is a script (`$ROOT/scripts/karatsuba.py`) that
29 will find the break even point on a particular machine.
31 ***WARNING: The Karatsuba script requires Python 3.***
35 This `bc` uses Algorithm D ([long division][2]). Long division is polynomial
36 (`O(n^2)`), but unlike Karatsuba, any division "divide and conquer" algorithm
37 reaches its "break even" point with significantly larger numbers. "Fast"
38 algorithms become less attractive with division as this operation typically
39 reduces the problem size.
41 While the implementation of long division may appear to use the subtractive
42 chunking method, it only uses subtraction to find a quotient digit. It avoids
43 unnecessary work by aligning digits prior to performing subtraction and finding
44 a starting guess for the quotient.
46 Subtraction was used instead of multiplication for two reasons:
48 1. Division and subtraction can share code (one of the less important goals of
49 this `bc` is small code).
50 2. It minimizes algorithmic complexity.
52 Using multiplication would make division have the even worse algorithmic
53 complexity of `O(n^(2*log_2(3)))` (best case) and `O(n^3)` (worst case).
57 This `bc` implements [Exponentiation by Squaring][3], which (via Karatsuba) has
58 a complexity of `O((n*log(n))^log_2(3))` which is favorable to the
59 `O((n*log(n))^2)` without Karatsuba.
63 This `bc` implements the fast algorithm [Newton's Method][4] (also known as the
64 Newton-Raphson Method, or the [Babylonian Method][5]) to perform the square root
67 Its complexity is `O(log(n)*n^2)` as it requires one division per iteration, and
68 it doubles the amount of correct digits per iteration.
70 ### Sine and Cosine (`bc` Math Library Only)
72 This `bc` uses the series
75 x - x^3/3! + x^5/5! - x^7/7! + ...
78 to calculate `sin(x)` and `cos(x)`. It also uses the relation
81 cos(x) = sin(x + pi/2)
84 to calculate `cos(x)`. It has a complexity of `O(n^3)`.
86 **Note**: this series has a tendency to *occasionally* produce an error of 1
87 [ULP][6]. (It is an unfortunate side effect of the algorithm, and there isn't
88 any way around it; [this article][7] explains why calculating sine and cosine,
89 and the other transcendental functions below, within less than 1 ULP is nearly
90 impossible and unnecessary.) Therefore, I recommend that users do their
91 calculations with the precision (`scale`) set to at least 1 greater than is
94 ### Exponentiation (`bc` Math Library Only)
96 This `bc` uses the series
99 1 + x + x^2/2! + x^3/3! + ...
102 to calculate `e^x`. Since this only works when `x` is small, it uses
110 It has a complexity of `O(n^3)`.
112 **Note**: this series can also produce errors of 1 ULP, so I recommend users do
113 their calculations with the precision (`scale`) set to at least 1 greater than
116 ### Natural Logarithm (`bc` Math Library Only)
118 This `bc` uses the series
121 a + a^3/3 + a^5/5 + ...
124 (where `a` is equal to `(x - 1)/(x + 1)`) to calculate `ln(x)` when `x` is small
125 and uses the relation
131 to sufficiently reduce `x`.
133 It has a complexity of `O(n^3)`.
135 **Note**: this series can also produce errors of 1 ULP, so I recommend users do
136 their calculations with the precision (`scale`) set to at least 1 greater than
139 ### Arctangent (`bc` Math Library Only)
141 This `bc` uses the series
144 x - x^3/3 + x^5/5 - x^7/7 + ...
147 to calculate `atan(x)` for small `x` and the relation
150 atan(x) = atan(c) + atan((x - c)/(1 + x * c))
153 to reduce `x` to small enough. It has a complexity of `O(n^3)`.
155 **Note**: this series can also produce errors of 1 ULP, so I recommend users do
156 their calculations with the precision (`scale`) set to at least 1 greater than
159 ### Bessel (`bc` Math Library Only)
161 This `bc` uses the series
164 x^n/(2^n * n!) * (1 - x^2 * 2 * 1! * (n + 1)) + x^4/(2^4 * 2! * (n + 1) * (n + 2)) - ...
167 to calculate the bessel function (integer order only).
169 It also uses the relation
172 j(-n,x) = (-1)^n * j(n,x)
175 to calculate the bessel when `x < 0`, It has a complexity of `O(n^3)`.
177 **Note**: this series can also produce errors of 1 ULP, so I recommend users do
178 their calculations with the precision (`scale`) set to at least 1 greater than
181 ### Modular Exponentiation (`dc` Only)
183 This `dc` uses the [Memory-efficient method][8] to compute modular
184 exponentiation. The complexity is `O(e*n^2)`, which may initially seem
185 inefficient, but `n` is kept small by maintaining small numbers. In practice, it
188 ### Non-Integer Exponentiation (`bc` Math Library 2 Only)
190 This is implemented in the function `p(x,y)`.
192 The algorithm used is to use the formula `e(y*l(x))`.
194 It has a complexity of `O(n^3)` because both `e()` and `l()` do.
196 ### Rounding (`bc` Math Library 2 Only)
198 This is implemented in the function `r(x,p)`.
200 The algorithm is a simple method to check if rounding away from zero is
201 necessary, and if so, adds `1e10^p`.
203 It has a complexity of `O(n)` because of add.
205 ### Ceiling (`bc` Math Library 2 Only)
207 This is implemented in the function `ceil(x,p)`.
209 The algorithm is a simple add of one less decimal place than `p`.
211 It has a complexity of `O(n)` because of add.
213 ### Factorial (`bc` Math Library 2 Only)
215 This is implemented in the function `f(n)`.
217 The algorithm is a simple multiplication loop.
219 It has a complexity of `O(n^3)` because of linear amount of `O(n^2)`
222 ### Permutations (`bc` Math Library 2 Only)
224 This is implemented in the function `perm(n,k)`.
226 The algorithm is to use the formula `n!/(n-k)!`.
228 It has a complexity of `O(n^3)` because of the division and factorials.
230 ### Combinations (`bc` Math Library 2 Only)
232 This is implemented in the function `comb(n,r)`.
234 The algorithm is to use the formula `n!/r!*(n-r)!`.
236 It has a complexity of `O(n^3)` because of the division and factorials.
238 ### Logarithm of Any Base (`bc` Math Library 2 Only)
240 This is implemented in the function `log(x,b)`.
242 The algorithm is to use the formula `l(x)/l(b)` with double the `scale` because
243 there is no good way of knowing how many digits of precision are needed when
246 It has a complexity of `O(n^3)` because of the division and `l()`.
248 ### Logarithm of Base 2 (`bc` Math Library 2 Only)
250 This is implemented in the function `l2(x)`.
252 This is a convenience wrapper around `log(x,2)`.
254 ### Logarithm of Base 10 (`bc` Math Library 2 Only)
256 This is implemented in the function `l10(x)`.
258 This is a convenience wrapper around `log(x,10)`.
260 ### Root (`bc` Math Library 2 Only)
262 This is implemented in the function `root(x,n)`.
264 The algorithm is [Newton's method][9]. The initial guess is calculated as
265 `10^ceil(length(x)/n)`.
267 Like square root, its complexity is `O(log(n)*n^2)` as it requires one division
268 per iteration, and it doubles the amount of correct digits per iteration.
270 ### Cube Root (`bc` Math Library 2 Only)
272 This is implemented in the function `cbrt(x)`.
274 This is a convenience wrapper around `root(x,3)`.
276 ### Greatest Common Divisor (`bc` Math Library 2 Only)
278 This is implemented in the function `gcd(a,b)`.
280 The algorithm is an iterative version of the [Euclidean Algorithm][10].
282 It has a complexity of `O(n^4)` because it has a linear number of divisions.
284 This function ensures that `a` is always bigger than `b` before starting the
287 ### Least Common Multiple (`bc` Math Library 2 Only)
289 This is implemented in the function `lcm(a,b)`.
291 The algorithm uses the formula `a*b/gcd(a,b)`.
293 It has a complexity of `O(n^4)` because of `gcd()`.
295 ### Pi (`bc` Math Library 2 Only)
297 This is implemented in the function `pi(s)`.
299 The algorithm uses the formula `4*a(1)`.
301 It has a complexity of `O(n^3)` because of arctangent.
303 ### Tangent (`bc` Math Library 2 Only)
305 This is implemented in the function `t(x)`.
307 The algorithm uses the formula `s(x)/c(x)`.
309 It has a complexity of `O(n^3)` because of sine, cosine, and division.
311 ### Atan2 (`bc` Math Library 2 Only)
313 This is implemented in the function `a2(y,x)`.
315 The algorithm uses the [standard formulas][11].
317 It has a complexity of `O(n^3)` because of arctangent.
319 [1]: https://en.wikipedia.org/wiki/Karatsuba_algorithm
320 [2]: https://en.wikipedia.org/wiki/Long_division
321 [3]: https://en.wikipedia.org/wiki/Exponentiation_by_squaring
322 [4]: https://en.wikipedia.org/wiki/Newton%27s_method#Square_root_of_a_number
323 [5]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
324 [6]: https://en.wikipedia.org/wiki/Unit_in_the_last_place
325 [7]: https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT
326 [8]: https://en.wikipedia.org/wiki/Modular_exponentiation#Memory-efficient_method
327 [9]: https://en.wikipedia.org/wiki/Root-finding_algorithms#Newton's_method_(and_similar_derivative-based_methods)
328 [10]: https://en.wikipedia.org/wiki/Euclidean_algorithm
329 [11]: https://en.wikipedia.org/wiki/Atan2#Definition_and_computation