1252884aeSStefan Eßer# Algorithms 2252884aeSStefan Eßer 3252884aeSStefan EßerThis `bc` uses the math algorithms below: 4252884aeSStefan Eßer 5252884aeSStefan Eßer### Addition 6252884aeSStefan Eßer 7252884aeSStefan EßerThis `bc` uses brute force addition, which is linear (`O(n)`) in the number of 8252884aeSStefan Eßerdigits. 9252884aeSStefan Eßer 10252884aeSStefan Eßer### Subtraction 11252884aeSStefan Eßer 12252884aeSStefan EßerThis `bc` uses brute force subtraction, which is linear (`O(n)`) in the number 13252884aeSStefan Eßerof digits. 14252884aeSStefan Eßer 15252884aeSStefan Eßer### Multiplication 16252884aeSStefan Eßer 17252884aeSStefan EßerThis `bc` uses two algorithms: [Karatsuba][1] and brute force. 18252884aeSStefan Eßer 19252884aeSStefan EßerKaratsuba is used for "large" numbers. ("Large" numbers are defined as any 20252884aeSStefan Eßernumber with `BC_NUM_KARATSUBA_LEN` digits or larger. `BC_NUM_KARATSUBA_LEN` has 21252884aeSStefan Eßera sane default, but may be configured by the user.) Karatsuba, as implemented in 22252884aeSStefan Eßerthis `bc`, is superlinear but subpolynomial (bounded by `O(n^log_2(3))`). 23252884aeSStefan Eßer 24252884aeSStefan EßerBrute force multiplication is used below `BC_NUM_KARATSUBA_LEN` digits. It is 25252884aeSStefan Eßerpolynomial (`O(n^2)`), but since Karatsuba requires both more intermediate 26252884aeSStefan Eßervalues (which translate to memory allocations) and a few more additions, there 27252884aeSStefan Eßeris a "break even" point in the number of digits where brute force multiplication 2844d4804dSStefan Eßeris faster than Karatsuba. There is a script (`$ROOT/scripts/karatsuba.py`) that 2944d4804dSStefan Eßerwill find the break even point on a particular machine. 30252884aeSStefan Eßer 31252884aeSStefan Eßer***WARNING: The Karatsuba script requires Python 3.*** 32252884aeSStefan Eßer 33252884aeSStefan Eßer### Division 34252884aeSStefan Eßer 35252884aeSStefan EßerThis `bc` uses Algorithm D ([long division][2]). Long division is polynomial 36252884aeSStefan Eßer(`O(n^2)`), but unlike Karatsuba, any division "divide and conquer" algorithm 37252884aeSStefan Eßerreaches its "break even" point with significantly larger numbers. "Fast" 38252884aeSStefan Eßeralgorithms become less attractive with division as this operation typically 39252884aeSStefan Eßerreduces the problem size. 40252884aeSStefan Eßer 41252884aeSStefan EßerWhile the implementation of long division may appear to use the subtractive 42252884aeSStefan Eßerchunking method, it only uses subtraction to find a quotient digit. It avoids 43252884aeSStefan Eßerunnecessary work by aligning digits prior to performing subtraction and finding 44252884aeSStefan Eßera starting guess for the quotient. 45252884aeSStefan Eßer 46252884aeSStefan EßerSubtraction was used instead of multiplication for two reasons: 47252884aeSStefan Eßer 48252884aeSStefan Eßer1. Division and subtraction can share code (one of the less important goals of 49252884aeSStefan Eßer this `bc` is small code). 50252884aeSStefan Eßer2. It minimizes algorithmic complexity. 51252884aeSStefan Eßer 52252884aeSStefan EßerUsing multiplication would make division have the even worse algorithmic 53252884aeSStefan Eßercomplexity of `O(n^(2*log_2(3)))` (best case) and `O(n^3)` (worst case). 54252884aeSStefan Eßer 55252884aeSStefan Eßer### Power 56252884aeSStefan Eßer 57252884aeSStefan EßerThis `bc` implements [Exponentiation by Squaring][3], which (via Karatsuba) has 58252884aeSStefan Eßera complexity of `O((n*log(n))^log_2(3))` which is favorable to the 59252884aeSStefan Eßer`O((n*log(n))^2)` without Karatsuba. 60252884aeSStefan Eßer 61252884aeSStefan Eßer### Square Root 62252884aeSStefan Eßer 63252884aeSStefan EßerThis `bc` implements the fast algorithm [Newton's Method][4] (also known as the 64252884aeSStefan EßerNewton-Raphson Method, or the [Babylonian Method][5]) to perform the square root 6544d4804dSStefan Eßeroperation. 66252884aeSStefan Eßer 6744d4804dSStefan EßerIts complexity is `O(log(n)*n^2)` as it requires one division per iteration, and 6844d4804dSStefan Eßerit doubles the amount of correct digits per iteration. 6944d4804dSStefan Eßer 7044d4804dSStefan Eßer### Sine and Cosine (`bc` Math Library Only) 71252884aeSStefan Eßer 72252884aeSStefan EßerThis `bc` uses the series 73252884aeSStefan Eßer 74252884aeSStefan Eßer``` 75252884aeSStefan Eßerx - x^3/3! + x^5/5! - x^7/7! + ... 76252884aeSStefan Eßer``` 77252884aeSStefan Eßer 78252884aeSStefan Eßerto calculate `sin(x)` and `cos(x)`. It also uses the relation 79252884aeSStefan Eßer 80252884aeSStefan Eßer``` 81252884aeSStefan Eßercos(x) = sin(x + pi/2) 82252884aeSStefan Eßer``` 83252884aeSStefan Eßer 84252884aeSStefan Eßerto calculate `cos(x)`. It has a complexity of `O(n^3)`. 85252884aeSStefan Eßer 86252884aeSStefan Eßer**Note**: this series has a tendency to *occasionally* produce an error of 1 87252884aeSStefan Eßer[ULP][6]. (It is an unfortunate side effect of the algorithm, and there isn't 88252884aeSStefan Eßerany way around it; [this article][7] explains why calculating sine and cosine, 89252884aeSStefan Eßerand the other transcendental functions below, within less than 1 ULP is nearly 90252884aeSStefan Eßerimpossible and unnecessary.) Therefore, I recommend that users do their 91252884aeSStefan Eßercalculations with the precision (`scale`) set to at least 1 greater than is 92252884aeSStefan Eßerneeded. 93252884aeSStefan Eßer 9444d4804dSStefan Eßer### Exponentiation (`bc` Math Library Only) 95252884aeSStefan Eßer 96252884aeSStefan EßerThis `bc` uses the series 97252884aeSStefan Eßer 98252884aeSStefan Eßer``` 99252884aeSStefan Eßer1 + x + x^2/2! + x^3/3! + ... 100252884aeSStefan Eßer``` 101252884aeSStefan Eßer 102252884aeSStefan Eßerto calculate `e^x`. Since this only works when `x` is small, it uses 103252884aeSStefan Eßer 104252884aeSStefan Eßer``` 105252884aeSStefan Eßere^x = (e^(x/2))^2 106252884aeSStefan Eßer``` 107252884aeSStefan Eßer 10844d4804dSStefan Eßerto reduce `x`. 10944d4804dSStefan Eßer 11044d4804dSStefan EßerIt has a complexity of `O(n^3)`. 111252884aeSStefan Eßer 112252884aeSStefan Eßer**Note**: this series can also produce errors of 1 ULP, so I recommend users do 113252884aeSStefan Eßertheir calculations with the precision (`scale`) set to at least 1 greater than 114252884aeSStefan Eßeris needed. 115252884aeSStefan Eßer 11644d4804dSStefan Eßer### Natural Logarithm (`bc` Math Library Only) 117252884aeSStefan Eßer 118252884aeSStefan EßerThis `bc` uses the series 119252884aeSStefan Eßer 120252884aeSStefan Eßer``` 121252884aeSStefan Eßera + a^3/3 + a^5/5 + ... 122252884aeSStefan Eßer``` 123252884aeSStefan Eßer 124252884aeSStefan Eßer(where `a` is equal to `(x - 1)/(x + 1)`) to calculate `ln(x)` when `x` is small 125252884aeSStefan Eßerand uses the relation 126252884aeSStefan Eßer 127252884aeSStefan Eßer``` 128252884aeSStefan Eßerln(x^2) = 2 * ln(x) 129252884aeSStefan Eßer``` 130252884aeSStefan Eßer 13144d4804dSStefan Eßerto sufficiently reduce `x`. 13244d4804dSStefan Eßer 13344d4804dSStefan EßerIt has a complexity of `O(n^3)`. 134252884aeSStefan Eßer 135252884aeSStefan Eßer**Note**: this series can also produce errors of 1 ULP, so I recommend users do 136252884aeSStefan Eßertheir calculations with the precision (`scale`) set to at least 1 greater than 137252884aeSStefan Eßeris needed. 138252884aeSStefan Eßer 13944d4804dSStefan Eßer### Arctangent (`bc` Math Library Only) 140252884aeSStefan Eßer 141252884aeSStefan EßerThis `bc` uses the series 142252884aeSStefan Eßer 143252884aeSStefan Eßer``` 144252884aeSStefan Eßerx - x^3/3 + x^5/5 - x^7/7 + ... 145252884aeSStefan Eßer``` 146252884aeSStefan Eßer 147252884aeSStefan Eßerto calculate `atan(x)` for small `x` and the relation 148252884aeSStefan Eßer 149252884aeSStefan Eßer``` 150252884aeSStefan Eßeratan(x) = atan(c) + atan((x - c)/(1 + x * c)) 151252884aeSStefan Eßer``` 152252884aeSStefan Eßer 153252884aeSStefan Eßerto reduce `x` to small enough. It has a complexity of `O(n^3)`. 154252884aeSStefan Eßer 155252884aeSStefan Eßer**Note**: this series can also produce errors of 1 ULP, so I recommend users do 156252884aeSStefan Eßertheir calculations with the precision (`scale`) set to at least 1 greater than 157252884aeSStefan Eßeris needed. 158252884aeSStefan Eßer 15944d4804dSStefan Eßer### Bessel (`bc` Math Library Only) 160252884aeSStefan Eßer 161252884aeSStefan EßerThis `bc` uses the series 162252884aeSStefan Eßer 163252884aeSStefan Eßer``` 164252884aeSStefan Eßerx^n/(2^n * n!) * (1 - x^2 * 2 * 1! * (n + 1)) + x^4/(2^4 * 2! * (n + 1) * (n + 2)) - ... 165252884aeSStefan Eßer``` 166252884aeSStefan Eßer 167252884aeSStefan Eßerto calculate the bessel function (integer order only). 168252884aeSStefan Eßer 169252884aeSStefan EßerIt also uses the relation 170252884aeSStefan Eßer 171252884aeSStefan Eßer``` 172252884aeSStefan Eßerj(-n,x) = (-1)^n * j(n,x) 173252884aeSStefan Eßer``` 174252884aeSStefan Eßer 175252884aeSStefan Eßerto calculate the bessel when `x < 0`, It has a complexity of `O(n^3)`. 176252884aeSStefan Eßer 177252884aeSStefan Eßer**Note**: this series can also produce errors of 1 ULP, so I recommend users do 178252884aeSStefan Eßertheir calculations with the precision (`scale`) set to at least 1 greater than 179252884aeSStefan Eßeris needed. 180252884aeSStefan Eßer 181d101cdd6SStefan Eßer### Modular Exponentiation 182252884aeSStefan Eßer 183252884aeSStefan EßerThis `dc` uses the [Memory-efficient method][8] to compute modular 184252884aeSStefan Eßerexponentiation. The complexity is `O(e*n^2)`, which may initially seem 185252884aeSStefan Eßerinefficient, but `n` is kept small by maintaining small numbers. In practice, it 186252884aeSStefan Eßeris extremely fast. 187252884aeSStefan Eßer 18844d4804dSStefan Eßer### Non-Integer Exponentiation (`bc` Math Library 2 Only) 18944d4804dSStefan Eßer 19044d4804dSStefan EßerThis is implemented in the function `p(x,y)`. 19144d4804dSStefan Eßer 19244d4804dSStefan EßerThe algorithm used is to use the formula `e(y*l(x))`. 19344d4804dSStefan Eßer 19444d4804dSStefan EßerIt has a complexity of `O(n^3)` because both `e()` and `l()` do. 19544d4804dSStefan Eßer 196*aa339f1dSStefan EßerHowever, there are details to this algorithm, described by the author, 197*aa339f1dSStefan EßerTediusTimmy, in GitHub issue [#69][12]. 198*aa339f1dSStefan Eßer 199*aa339f1dSStefan EßerFirst, check if the exponent is 0. If it is, return 1 at the appropriate 200*aa339f1dSStefan Eßer`scale`. 201*aa339f1dSStefan Eßer 202*aa339f1dSStefan EßerNext, check if the number is 0. If so, check if the exponent is greater than 203*aa339f1dSStefan Eßerzero; if it is, return 0. If the exponent is less than 0, error (with a divide 204*aa339f1dSStefan Eßerby 0) because that is undefined. 205*aa339f1dSStefan Eßer 206*aa339f1dSStefan EßerNext, check if the exponent is actually an integer, and if it is, use the 207*aa339f1dSStefan Eßerexponentiation operator. 208*aa339f1dSStefan Eßer 209*aa339f1dSStefan EßerAt the `z=0` line is the start of the meat of the new code. 210*aa339f1dSStefan Eßer 211*aa339f1dSStefan Eßer`z` is set to zero as a flag and as a value. What I mean by that will be clear 212*aa339f1dSStefan Eßerlater. 213*aa339f1dSStefan Eßer 214*aa339f1dSStefan EßerThen we check if the number is less than 0. If it is, we negate the exponent 215*aa339f1dSStefan Eßer(and the integer version of the exponent, which we calculated earlier to check 216*aa339f1dSStefan Eßerif it was an integer). We also save the number in `z`; being non-zero is a flag 217*aa339f1dSStefan Eßerfor later and a value to be used. Then we store the reciprocal of the number in 218*aa339f1dSStefan Eßeritself. 219*aa339f1dSStefan Eßer 220*aa339f1dSStefan EßerAll of the above paragraph will not make sense unless you remember the 221*aa339f1dSStefan Eßerrelationship `l(x) == -l(1/x)`; we negated the exponent, which is equivalent to 222*aa339f1dSStefan Eßerthe negative sign in that relationship, and we took the reciprocal of the 223*aa339f1dSStefan Eßernumber, which is equivalent to the reciprocal in the relationship. 224*aa339f1dSStefan Eßer 225*aa339f1dSStefan EßerBut what if the number is negative? We ignore that for now because we eventually 226*aa339f1dSStefan Eßercall `l(x)`, which will raise an error if `x` is negative. 227*aa339f1dSStefan Eßer 228*aa339f1dSStefan EßerNow, we can keep going. 229*aa339f1dSStefan Eßer 230*aa339f1dSStefan EßerIf at this point, the exponent is negative, we need to use the original formula 231*aa339f1dSStefan Eßer(`e(y * l(x))`) and return that result because the result will go to zero 232*aa339f1dSStefan Eßeranyway. 233*aa339f1dSStefan Eßer 234*aa339f1dSStefan EßerBut if we did *not* return, we know the exponent is *not* negative, so we can 235*aa339f1dSStefan Eßerget clever. 236*aa339f1dSStefan Eßer 237*aa339f1dSStefan EßerWe then compute the integral portion of the power by computing the number to 238*aa339f1dSStefan Eßerpower of the integral portion of the exponent. 239*aa339f1dSStefan Eßer 240*aa339f1dSStefan EßerThen we have the most clever trick: we add the length of that integer power (and 241*aa339f1dSStefan Eßera little extra) to the `scale`. Why? Because this will ensure that the next part 242*aa339f1dSStefan Eßeris calculated to at least as many digits as should be in the integer *plus* any 243*aa339f1dSStefan Eßerextra `scale` that was wanted. 244*aa339f1dSStefan Eßer 245*aa339f1dSStefan EßerThen we check `z`, which, if it is not zero, is the original value of the 246*aa339f1dSStefan Eßernumber. If it is not zero, we need to take the take the reciprocal *again* 247*aa339f1dSStefan Eßerbecause now we have the correct `scale`. And we *also* have to calculate the 248*aa339f1dSStefan Eßerinteger portion of the power again. 249*aa339f1dSStefan Eßer 250*aa339f1dSStefan EßerThen we need to calculate the fractional portion of the number. We do this by 251*aa339f1dSStefan Eßerusing the original formula, but we instead of calculating `e(y * l(x))`, we 252*aa339f1dSStefan Eßercalculate `e((y - a) * l(x))`, where `a` is the integer portion of `y`. It's 253*aa339f1dSStefan Eßereasy to see that `y - a` will be just the fractional portion of `y` (the 254*aa339f1dSStefan Eßerexponent), so this makes sense. 255*aa339f1dSStefan Eßer 256*aa339f1dSStefan EßerBut then we *multiply* it into the integer portion of the power. Why? Because 257*aa339f1dSStefan Eßerremember: we're dealing with an exponent and a power; the relationship is 258*aa339f1dSStefan Eßer`x^(y+z) == (x^y)*(x^z)`. 259*aa339f1dSStefan Eßer 260*aa339f1dSStefan EßerSo we multiply it into the integer portion of the power. 261*aa339f1dSStefan Eßer 262*aa339f1dSStefan EßerFinally, we set the result to the `scale`. 263*aa339f1dSStefan Eßer 26444d4804dSStefan Eßer### Rounding (`bc` Math Library 2 Only) 26544d4804dSStefan Eßer 26644d4804dSStefan EßerThis is implemented in the function `r(x,p)`. 26744d4804dSStefan Eßer 26844d4804dSStefan EßerThe algorithm is a simple method to check if rounding away from zero is 26944d4804dSStefan Eßernecessary, and if so, adds `1e10^p`. 27044d4804dSStefan Eßer 27144d4804dSStefan EßerIt has a complexity of `O(n)` because of add. 27244d4804dSStefan Eßer 27344d4804dSStefan Eßer### Ceiling (`bc` Math Library 2 Only) 27444d4804dSStefan Eßer 27544d4804dSStefan EßerThis is implemented in the function `ceil(x,p)`. 27644d4804dSStefan Eßer 27744d4804dSStefan EßerThe algorithm is a simple add of one less decimal place than `p`. 27844d4804dSStefan Eßer 27944d4804dSStefan EßerIt has a complexity of `O(n)` because of add. 28044d4804dSStefan Eßer 28144d4804dSStefan Eßer### Factorial (`bc` Math Library 2 Only) 28244d4804dSStefan Eßer 28344d4804dSStefan EßerThis is implemented in the function `f(n)`. 28444d4804dSStefan Eßer 28544d4804dSStefan EßerThe algorithm is a simple multiplication loop. 28644d4804dSStefan Eßer 28744d4804dSStefan EßerIt has a complexity of `O(n^3)` because of linear amount of `O(n^2)` 28844d4804dSStefan Eßermultiplications. 28944d4804dSStefan Eßer 29044d4804dSStefan Eßer### Permutations (`bc` Math Library 2 Only) 29144d4804dSStefan Eßer 29244d4804dSStefan EßerThis is implemented in the function `perm(n,k)`. 29344d4804dSStefan Eßer 29444d4804dSStefan EßerThe algorithm is to use the formula `n!/(n-k)!`. 29544d4804dSStefan Eßer 29644d4804dSStefan EßerIt has a complexity of `O(n^3)` because of the division and factorials. 29744d4804dSStefan Eßer 29844d4804dSStefan Eßer### Combinations (`bc` Math Library 2 Only) 29944d4804dSStefan Eßer 30044d4804dSStefan EßerThis is implemented in the function `comb(n,r)`. 30144d4804dSStefan Eßer 30244d4804dSStefan EßerThe algorithm is to use the formula `n!/r!*(n-r)!`. 30344d4804dSStefan Eßer 30444d4804dSStefan EßerIt has a complexity of `O(n^3)` because of the division and factorials. 30544d4804dSStefan Eßer 30644d4804dSStefan Eßer### Logarithm of Any Base (`bc` Math Library 2 Only) 30744d4804dSStefan Eßer 30844d4804dSStefan EßerThis is implemented in the function `log(x,b)`. 30944d4804dSStefan Eßer 31044d4804dSStefan EßerThe algorithm is to use the formula `l(x)/l(b)` with double the `scale` because 31144d4804dSStefan Eßerthere is no good way of knowing how many digits of precision are needed when 31244d4804dSStefan Eßerswitching bases. 31344d4804dSStefan Eßer 31444d4804dSStefan EßerIt has a complexity of `O(n^3)` because of the division and `l()`. 31544d4804dSStefan Eßer 31644d4804dSStefan Eßer### Logarithm of Base 2 (`bc` Math Library 2 Only) 31744d4804dSStefan Eßer 31844d4804dSStefan EßerThis is implemented in the function `l2(x)`. 31944d4804dSStefan Eßer 32044d4804dSStefan EßerThis is a convenience wrapper around `log(x,2)`. 32144d4804dSStefan Eßer 32244d4804dSStefan Eßer### Logarithm of Base 10 (`bc` Math Library 2 Only) 32344d4804dSStefan Eßer 32444d4804dSStefan EßerThis is implemented in the function `l10(x)`. 32544d4804dSStefan Eßer 32644d4804dSStefan EßerThis is a convenience wrapper around `log(x,10)`. 32744d4804dSStefan Eßer 32844d4804dSStefan Eßer### Root (`bc` Math Library 2 Only) 32944d4804dSStefan Eßer 33044d4804dSStefan EßerThis is implemented in the function `root(x,n)`. 33144d4804dSStefan Eßer 33244d4804dSStefan EßerThe algorithm is [Newton's method][9]. The initial guess is calculated as 33344d4804dSStefan Eßer`10^ceil(length(x)/n)`. 33444d4804dSStefan Eßer 33544d4804dSStefan EßerLike square root, its complexity is `O(log(n)*n^2)` as it requires one division 33644d4804dSStefan Eßerper iteration, and it doubles the amount of correct digits per iteration. 33744d4804dSStefan Eßer 33844d4804dSStefan Eßer### Cube Root (`bc` Math Library 2 Only) 33944d4804dSStefan Eßer 34044d4804dSStefan EßerThis is implemented in the function `cbrt(x)`. 34144d4804dSStefan Eßer 34244d4804dSStefan EßerThis is a convenience wrapper around `root(x,3)`. 34344d4804dSStefan Eßer 34444d4804dSStefan Eßer### Greatest Common Divisor (`bc` Math Library 2 Only) 34544d4804dSStefan Eßer 34644d4804dSStefan EßerThis is implemented in the function `gcd(a,b)`. 34744d4804dSStefan Eßer 34844d4804dSStefan EßerThe algorithm is an iterative version of the [Euclidean Algorithm][10]. 34944d4804dSStefan Eßer 35044d4804dSStefan EßerIt has a complexity of `O(n^4)` because it has a linear number of divisions. 35144d4804dSStefan Eßer 35244d4804dSStefan EßerThis function ensures that `a` is always bigger than `b` before starting the 35344d4804dSStefan Eßeralgorithm. 35444d4804dSStefan Eßer 35544d4804dSStefan Eßer### Least Common Multiple (`bc` Math Library 2 Only) 35644d4804dSStefan Eßer 35744d4804dSStefan EßerThis is implemented in the function `lcm(a,b)`. 35844d4804dSStefan Eßer 35944d4804dSStefan EßerThe algorithm uses the formula `a*b/gcd(a,b)`. 36044d4804dSStefan Eßer 36144d4804dSStefan EßerIt has a complexity of `O(n^4)` because of `gcd()`. 36244d4804dSStefan Eßer 36344d4804dSStefan Eßer### Pi (`bc` Math Library 2 Only) 36444d4804dSStefan Eßer 36544d4804dSStefan EßerThis is implemented in the function `pi(s)`. 36644d4804dSStefan Eßer 36744d4804dSStefan EßerThe algorithm uses the formula `4*a(1)`. 36844d4804dSStefan Eßer 36944d4804dSStefan EßerIt has a complexity of `O(n^3)` because of arctangent. 37044d4804dSStefan Eßer 37144d4804dSStefan Eßer### Tangent (`bc` Math Library 2 Only) 37244d4804dSStefan Eßer 37344d4804dSStefan EßerThis is implemented in the function `t(x)`. 37444d4804dSStefan Eßer 37544d4804dSStefan EßerThe algorithm uses the formula `s(x)/c(x)`. 37644d4804dSStefan Eßer 37744d4804dSStefan EßerIt has a complexity of `O(n^3)` because of sine, cosine, and division. 37844d4804dSStefan Eßer 37944d4804dSStefan Eßer### Atan2 (`bc` Math Library 2 Only) 38044d4804dSStefan Eßer 38144d4804dSStefan EßerThis is implemented in the function `a2(y,x)`. 38244d4804dSStefan Eßer 38344d4804dSStefan EßerThe algorithm uses the [standard formulas][11]. 38444d4804dSStefan Eßer 38544d4804dSStefan EßerIt has a complexity of `O(n^3)` because of arctangent. 38644d4804dSStefan Eßer 387252884aeSStefan Eßer[1]: https://en.wikipedia.org/wiki/Karatsuba_algorithm 388252884aeSStefan Eßer[2]: https://en.wikipedia.org/wiki/Long_division 389252884aeSStefan Eßer[3]: https://en.wikipedia.org/wiki/Exponentiation_by_squaring 390252884aeSStefan Eßer[4]: https://en.wikipedia.org/wiki/Newton%27s_method#Square_root_of_a_number 391252884aeSStefan Eßer[5]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method 392252884aeSStefan Eßer[6]: https://en.wikipedia.org/wiki/Unit_in_the_last_place 393252884aeSStefan Eßer[7]: https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT 394252884aeSStefan Eßer[8]: https://en.wikipedia.org/wiki/Modular_exponentiation#Memory-efficient_method 39544d4804dSStefan Eßer[9]: https://en.wikipedia.org/wiki/Root-finding_algorithms#Newton's_method_(and_similar_derivative-based_methods) 39644d4804dSStefan Eßer[10]: https://en.wikipedia.org/wiki/Euclidean_algorithm 39744d4804dSStefan Eßer[11]: https://en.wikipedia.org/wiki/Atan2#Definition_and_computation 398*aa339f1dSStefan Eßer[12]: https://github.com/gavinhoward/bc/issues/69 399