Brent Elementary
Brent Elementary
RICHARD P. BRENT
XI3STnXC'r. Let f(x) be one of the usual elementary functions (exp, log, artan, sin, cosh, etc.), and
let M(n) be the number of single-precision operations reqmred to multiply n-bit integers. It is shown
that f(x) can be evaluated, with relative error 0(2-'), m O(M(n)log (n)) operations as n --~ ~, for
any floating-point number x (with an n-bit fraction) in a suitable finite interval. From the Sehonbage-
Strassen bound on M(n), it follows that an n-bit approximation to f(x) may be evaluated
in O(n logS(n) log log(n)) operations. Special cases include the evaluation of constants such as
f, e, and e'. The algorithms depend on the theory of elhptic integrals, using the arithmetic-geometric
mean iteration and ascending Landen transformations.
Itsr wol~os Ar~oen~As~s : multiple-precision arithmetic, analytic complexity, arithmetic-geometric
mean, computational complexity, elementary function, elliptic integral, evaluation of x, exponentml,
Landen transformation, logarithm, trigonometric funetmn
CR CATEGORIES: 5.12, 5.15, 5.25
1. Introduction
We consider the number of operations required to evaluate the elementary functions
exp(x), log(x), ~ a r t a n ( x ) , sin(x), etc., with relative error O(2-n), for x in some
interval [a, b], and large n. Here, In, b] is a fixed, nontrivial interval on which the relevant
elementary function is defined. The results hold for computations performed on a multi-
tape Turing machine, but to simplify the exposition we assume t h a t a standard serial
computer with a random-access memory is used.
Let M ( x ) be the number of operations required to multiply two integers in the range
[0, 2~1). We assume the number representation is such t h a t addition can be performed in
0 ( M (n) ) operations, and t h a t M (n) satisfies the weak regularity condition
M ( a n ) <_ ~M(n), (1.1)
for some a and fl in (0; 1), and all sufficiently large n. Similar, b u t stronger, conditions
are usually assumed, either explicitly [11] or implicitly [15]. Our assumptions are cer-
tainly valid if the Sch6nhage-Strassen method [15, 19] is used to multiply n-bit integers
(in the usual binary representation) in 0 ( n log ( n ) log log (n) ) operation s.
The elementary function evaluations m a y be performed entirely in fixed point, using
integer arithmetic and some implicit scaling scheme. However, it is more convenient to
assume t h a t floating-point computation is used. For example, a sign and magnitude
representation could be used, with a fixed length binary exponent and an n-bit binary
fraction. Our results are independent of the particular floating-point number system
used, so long as the following conditions are satisfied.
Copyright © 1976, Association for Computing Machinery, Inc. General permission to republish,
b u t n o t for profit, all or part of this material is granted provided that ACM's copyright notice is
given and that reference is made to the publication, to its date of issue, and to the fact that reprinting
privileges were granted by permission of the A s s o c i a t i o n f o r Computing Machinery.
Author's address: Computer Centre, Australian National University, Box 4, Canberra, ACT 2600,
Australia.
Log(x) denotes the natural logarithm.
Journal of the A~ociataon for Computang Machinery, Vol. 23, No. 2, Aprd 1976, lap. 242-251.
Fast Multiple-Precision Evaluation o] Elementary Functions 243
1. Real numbers which are not too large or sma.ll can be approximated by floating-
point numbers, with a relative error 0(2-~).
2. Floating-point addition and multiplication can be performed in O(M(n)) opera-
tions, with a relative error 0 ( 2 -n) in the result.
3. The precision n is variable, and a floating-point number with precision n m a y be
approximated, with relative error 0 ( 2 -~) and in O(M(n)) operations, by a floating-
point number with precision m, for any positive m < n.
Throughout this paper, a floating-point number means a number in some representation .
satisfying conditions 1 to 3 above, not a single-precision number. We say that an opera-
tion is performed with precision n if the result is obtained with a relative error 0(2-~).
I t is assumed that the operands and result are approximated by floating-point numbers.
The main result of this paper, established in Sections 6 and 7, is that all the usual
elementary functions may be evaluated, with precision n, in O(M(n) log(n)) operations.
Note that O(M(n)n) operations are required if the Taylor series for log(1 -t- x) is
summed in the obvious way. Our result improves the bound O(M(n) logS(n)) given in
[4], although the algorithms described there may be faster for small n.
Preliminary results are given in Sections 2 to 5. In Section 2 we give, for completeness,
the known result that division and extraction of square roots to precision n require
O(M(n)) operations. Section 3 deals briefly with methods for approximating simple
zeros of nonlinear equations to precision n, and some results from the theory of elliptic
integrals are summarized in Section 4. Since our algorithms for elementary functions
require a knowledge of 7r to precision n, we show, in Section 5, how this may be obtained
in O(M(n) log(n)) operations. An amusing consequence of the results of Section 6 is
that e" may also be evaluated, to precision nn, in O ( M ( n ) log(n)) operations.
From [4, Th. 5.1], at least O(M(n)) operations are required to evaluate exp(x) or
sin(x) to precision n. I t is plausible to conjecture that O(M(n) log(n)) operations are
necessary.
Most of this paper is concerned with order of magnitude results, and multiplieative
constants are ignored. In Section 8, though, we give upper bounds on the constants.
From these bounds it is possible to estimate how large n needs to be before our algorithms
are faster than the conventional ones.
After this paper was submitted for publication, Bill Gosper drew my attention to
Salamin's paper [18], where an algorithm very similar to our algorithm for evaluating 7r
is described. A fast algorithm for evaluating log(x) was also found independently by
Salamin (see [2 or 5]).
Apparently similar algorithms for evaluating elementary functions are given by
Borchardt [3], Carlson [8, 9], and Thacher [23]. However, these algorithms require
O(M(n)n) or O(M(n)n ~) operations, so our algorithms are asymptotically faster.
We know how to evaluate certain other constants and functions almost as fast as
elementary functions. For example, Euler's constant ~ = 0.5772 . . . can be evaluated
with O(M(n)log s n) operations, using Sweeney's method [22] combined with binary
splitting [4]. Similarly for r ( a ) , where a is rational (or even algebraic): see Brent [7].
Related results are given by Gosper [13] and Schroeppel [20]. It is not known whether
any of these upper bounds are asymptotically the best possible.
In the following lemma, we assume that 1/c is in the allowable range for floating-point
numbers. Similar assumptions are implicit below.
LI~MA 2.2. I f c is a nonzero floating-point number, then 1/c can be evaluated, to pre-
cision n, in O( M ( n ) ) operations.
PROOF. The Newton iteration
x,+l -- x,(2 -- cx~) (2.1)
and
then
lim a , = 7 r / [ 2 F ( a ) ] . (4.18)
s-too
Also, if Co = s m a and
c,+i = a , - - a , + l , (4.19)
then
t--1 2
E(a)/F{a) = 1 -- 2 c,. (4.20)
z~0
follows from (4.18). (Another connection between (4.11) and the arithmetic-geometric
mean iteration is evident if so = (1 - bo2/ao~) ~. Assuming (4.11) holds for i < 0, it
follows t h a t s_, = (1 - b,2/a,2) ~ for all i > 0. This m a y be used to deduce (4.18) from
(4.m).)
5. Evaluation o f 7r
Since a, > bo > 0 for all ~ >_ 0, and C,+l = a, - a,+l = a,+l - b,, (4.17) gives b,+l
= [(a,+~ + c,+~)(a,+, - c,+,)] ~ = a,+~ - O ( , c+2~ ) , so c,+2 = O ( c ~ + Q . Thus, the process
converges with order at least 2, and logs n + 0 ( 1 ) iterations suffice to give an error
0 ( 2 -~) in t h e e s t i m a t e of (5.1). A m o r e detailed analysis shows t h a t a 2, ~ / t , < ~- < a , 2 / t ,
2
for all z > 0, and also a , 2 / t , - 7r ~-~ 8 ~ - e x p ( - 2 ' ~ - ) and 7r - a,+l/t,
7r~2'+4 exp( - - 2 ' + ~ ") as i --~ ~ . T h e speed of convergence is illustrated in T a b l e I.
F r o m the discussion above, it is clear t h a t the following algorithm, given in pseudo-
Algol, e v a l u a t e s ~" to precision n.
A l g o r i t h m f o r ~-
A ~-1, B ~---2-I; T ~ - ¼ ; X ~---1;
w h i l e A -- B > 2- ~ d o
b e g i n Y ~-- A; A ~'- (A + B)/2; B ~-- (BY)I;
T ~ T - X ( A -- Y ) ' ; X ~ 2 X
end;
r e t u r n A ~ / T [or, better, (A + B)~/(4T)].
TABLE I. CONVeRGeNCE OF
APPROXIMATIONS TO ~r
~ - aJ+l/t, a J / t , -- x
0 2.3'--1 8.6'--1
1 1.0'--3 4.6'--2
2 7.4P--9 8 8'--5
3 1.8"--19 3.1'--10
4 5.5'--41 3.7'--21
Fast Multiple-Preciszon Evaluatzon of Elementary Functions 247
Since so = sin ao = m t > 6t > 0, it follows from (4.11) t h a t 8, ~ 1 as i --~ ~¢. I n fact,
if 8, = 1 -- ~,, then ~,+1 = 1 - 8,+1 = 1 - 2(1 - e,)]/(2 -- e,) -~ e,2/8 + O(e,3), so
8, --~ 1 with order 2. Thus, after k ~ log2n iterations we have ~k = 0 ( 2 - " ) , so 1r/2 - a~
= 0 ( 2 -~/2) and, from (4.6) and (4.7),
F(~b~, ak) = log tan(Tr/4 + ~bk/2) + O(2-~). (6.2)
Assuming k > 0, the error is uniformly 0 ( 2 -~) for all m E [6, 1 -- 6], since ~bk _~ ~bl < lr/2.
Define the functions
and
T(m) = tan(q-/4 + ~b~/2), (6.4)
where ~® = l i m , ~ ~h,. Since 8, -~, 1 with order 2, the infinite product in (6.3) is con-
vergent, and U(m) is analytic for all m C (0, 1). Taking the limit in (6.1) and (6.2)
as n (and hence k) tends to ~ , we have the fundamental identity
U(m) = log T(m). (6.5)
Using (4.11)-(4.15), we can evaluate U(m) = III~:lo [(1 + s,)/2]}F(clo) + 0 ( 2 - n )
and T ( m ) -- (1 + vk)/(1 - Vk) + O(2-n), to precision n, in O(M(n) log(n)) opera-
tions. The algorithms are given below in pseudo-Algol.
AZgorithmfor U(m)
A ~ 1 ; B,,- (1-- m)~;
w h i l e A -- B > 2 -~/s d o
b e g i n C ~-- (A + B ) / 2 ; B ~- (AB)}; A ~-- C e n d ;
.4 ~ ~/(A + B ) ; S ~- rot;
w h i l e 1 -- E > 2 -"/I d o
b e g i n A ~-- A ( I -}- ~q)/2; ~q ~- 2S½/(1 q- S ) e n d ;
r e t u r n A (1 -[- ~q)/2.
Similarly, if x E [3, 9], the solution of T ( m ) = x lies in (0.16, 0.83), and the secant
method converges if m o = 0.2 and m~ = 0.8.
If x = 1 +4- e where e is small, and for domain reduction the relation
log(x) = log(Xx) -- log(),) (6.9)
is used, for some X E (3, 9), then log(Xx) and log(X) may be evaluated as above, but
cancellation in (6.9) will cause some loss of precision in the computed value of log(x). If
[el > 2-~, it is sufficient to evaluate log(Xx) and log(X) to precision 2n, for at most n
bits are lost through cancellation in (6.9). On the other hand, there is no difficulty if
[eI < 2-n, for then log(1 -4- e) = e(1 + O(2-n)). When evaluating exp(x), a similar
loss of precision never occurs, and it is sufficient to work with precision n + O(log log(n)),
as in the evaluation of ~" (see Section 5). To summarize, we have proved:
THEOREM 6.1. I f - ~ < a < b < ~ , then O ( M ( n ) log(n)) operations su~ce to evalu-
ate e x p ( x ) to precision n, uniformly for all floating-point numbers x E [a, b], as n ~ ~ ;
and simdarly for log(x) i f a > O.
After k iterations, Q ~ 2~, so at most 2 logan + O( 1 ) bits of precision are lost because V
is small. Thus it is sufficient to work with precision n + O (log (n)), and Lemma 2.4 justi-
fies our claim that O ( M ( n ) log (n)) operations are sufficient to obtain artan (x) to pre-
cision n.
If x is small, we may use the same idea as that described above for evalu-
ating log(1 + e) : work with precision 3 n / 2 + O(log(n)) if x > 2 -n/2, and use artan(x)
250 RICHARD P. BRENT
= x(1 + 0 ( 2 -~) ) if 0 <_ x ~_ 2 -~/2. (Actually, it is not necessary to increase the working
precision if log( (1 + V) / (1 - V) ) is evaluated carefully.)
Using the identity artan(x) = ~r/2 - artan(1/x) (x > 0), we can extend the do-
main to [0, ~ ). Also, since a t t a R ( - x ) = - a r t a n ( x ) , there is no difficulty with negative
x. To summarize, we have proved the following theorem.
THEOREM 7.1. O( M ( n) log(n)) operations su~ice to evaluate arran(x) to precision n,
unijbrmly for all floating-point numbers x, as n .-~ ~ .
Suppose O E [5, ~'/2 - 5]. From Lemma 3.1 and Theorem 7.1, we can solve the equa-
tion artan(x) = 0/2 to precision n in O ( M ( n ) log(n)) operations, and thus evaluate
x = tan(8/2). Now sin 0 = 2x/(1 + x 2) and cos 0 = (1 - x:)/(1 -b x ~) m a y easily be
evaluated. For arguments outside [5, ~'/2 - 5], domain reduction techniques like those
above m a y be used. Difficulties occur near certain integer multiples of ~'/2, but these
m a y be overcome (at least for the usual floating-point number representations) by in-
creasing the working precision. We state the following theorem for sin(x), but similar
results hold for the other trigonometric functions (and also, of course, for the elliptic
integrals and their inverse functions).
THEOREM 7.2. I f [a, b] ~ ( - - ~ , 7r), then O( M ( n) log(n)) operations su~ce to evaluate
sin(x) to precision n, uniformly for aU floating-point numbers x E [a, b], as n --~ ~ .
8. Asymptotic Constants
So far we have been concerned with order of magnitude results. In this section we give
upper bounds on the constants K such that w ( n ) ~ ( K W o ( 1 ) ) M ( n ) logan, where w ( n )
is the number of operations required to evaluate r, exp(x), etc., to precision n. The fol-
lowing two assumptions will be made.
1. For all ~ > 0 and , > 0, the inequality M(~/n) < (~/ + , ) M ( n ) holds for suffi-
ciently large n.
2. The number of operations required for floating-point addition, conversion between
representations of different precision (at most n), and multiplication or division of
floating-point numbers by small integers is o ( M ( n ) ) as n --~ ~ .
These assumptions certainly hold if a standard floating-point representation is used and
M ( n ) ~ n (log(n))" (log log(n)) a for some a > 0, provided j3 > 0 if a = 0.
The following result is proved in [4]. The algorithms used are similar to those of Sec-
tion 2, but slightly more efficient.
THEOREM 8.1. Preczsion-n diviswn of floating-point numbers may be performed in
(4 + o(1) ) M ( n ) operations as n --~ ~ , and square roots may be evaluated in (11/2 .4-
o ( I ) ) M (n) operations.
Using Theorem 8.1 and algorithms related to those of Sections 5-7, the following re-
sult is proved in [5].
THEOREM 8.2. ~r may be evaluated to precision n in (15/2 -]- o(1) ) M ( n) logsn opera-
tions as n --~ ¢~. I f ~r and log 2 are precomputed, the elementary function f ( x ) can be evalu-
ated to precision n in ( K + o ( 1 ) ) M ( n ) logan operations, where
K = ~13 ~.f /(a') = log(x) or exp(x),
( 34 if f ( x ) -~ arran(x), sin(x), etc.,
and x is a floating-point number ~n an interval on which f ( x ) is defined and bounded away
from 0 and ~ .
For purposes of comparison, note that evaluation of log(1 -I- x) or log((1 + x ) /
(1 - x)) by the usual series expansion requires (c + o ( 1 ) ) M ( n ) n operations, where c
is a constant of order unity (depending on the range of x and the precise method used).
Since 13 log2n < n for n ~ 83, the O ( M ( n ) log(n)) method for log(x) should be faster
than the O ( M ( n ) n ) method for n greater than a few hundred.
ACKNOWLEDGMENTS. This paper was written while the author was visiting the Com-
Fast M u l t i p l e - P r e c i s i o n Evaluation of Elementary Functions 251
p u t e r Science D e p a r t m e n t , S t a n f o r d U n i v e r s i t y . T h e c o m m e n t s of R . W . G o s p e r , D . E .
K n u t h , a n d D. S h a n k s o n v a r i o u s d r a f t s were v e r y useful.
REFERENCES
1 ABRAMOWITZ,M., AND STEGUN, I.A. Handbook of mathematical functions with formulas,
graphs, and mathematical tables. National Bureau of Standards, Washington, D.C., 1964;
Dover, 1965, Ch. 17.
2 BEELER, M , GoseEn, R.W, ANY SCHROEPPEL, R Hakmem Memo No. 239, M.I.T. Artfficial
Intelligence Lab., M I T., Cambridge, Mass., 1972, pp. 70--71
3. BORCHARDT,C W. Gesammelte Werke. Berhn, 1888, pp. 455-462
4. BRENT, R P. The complexity of multiple-precmlon arithmetic. Proc Seminar on Complexity of
Computational Problem Solving (held at Austrahan National U., Dee. 1974), Queensland U.
Press, Brisbane, Australia 1975, pp 126-165
5. BRENT, R P. Multiple-precision zero-finding methods and the complexity of elementary func-
tion evaluation Proc Symp. on Analytic Computational Complexity, J.F. Traub, Ed , Aca-
demm Press, New York, 1976, pp 151-176.
6. BRENT, R.P. Computer Solutwn of Nonhnear Equations. Academm Press, New York (to ap-
pear), Ch. 6.
7. BRENT, R P. A Fortran multiple-precision arithmetic package. Submitted to a technical jour-
nal.
8. CARLSON, B.C Algorithms involving arithmetic and geometric means. Amer. Math. Monthly
78 (May 1971), 496-505.
9 CARLSON, B C. An algorithm for computing logarithms and arctangents. Math. Comput. ~6
(Aprd 1972), 543-549.
10. FINKEL, R , GUIBAS, L , ANDSIMONY1, C Manuscript in preparation.
II. FISCHER, M J , A N n S T O C K M E Y E R , L J Fast on-line integer multipheation. J. Comput. System
Seis. 9 (Dec. 1974), 317-331
12. GAUSS, C.F Carl Fr~edmch Gauss Werke, Bd. 3 Gottingen, 1876, pp. 362--403.
13. GosP~R, R.W. Acceleration of serms. Memo No 304, M.I.T. Artificial Intelligence Lab.,
M I . T , Cambridge, M a s s , 1974.
14. GUILLOUO,J., AND BOUYER, M 1,000,000 decimals de pi. Unpublished manuscript
15. KNUTH, D.E. The Art of Computer Proqramm~nq, Vol. 2 Addison-Wesley, Reading, M a s s ,
1969. Errata and addenda: Rep. CS 194, Computer Scl Dep., Stanford U., Stanford, Calif., 1970.
16. LAGRANG~,J.L. Oeuvres de Lagrange, Tome ~ Gauthmr-Vlllars, Pans, 1868, pp. 267-272.
17. LEGENDRE,A.M Exerewes de CalculIntegral, Vol. 1. Parm, 1811, p. 61.
18. SALADIN, E. Computation of 7r using arithmetic-geometric mean. To appear in Math. Comput.
19 SCHOSHAGE, A., ANn STR^SSEN, V. Schnelle Multiphkation grosset Zahlen. Computing 7
(1971), 281-292.
20. SCHROEPPEL, R. Unpubhshed manuscript dated May 1975
21 SHANKS,D., ANn WR~NCU, J.W. Calculatmn of ~r to 100,000 decimals. Math Comput. 16 (1962),
76-99.
22 SWEENEY, D W. On the computatmn of Euler's constant Math. Comput. 17 (1963), 170--178.
23 THACHER,H.C. Iterated square root expansions for the inverse cosine and inverse hyperbolic
cosine. Math. Comput. 15 (1961), 399-403.
Journal of the Aaaociation for Computing Machmery, Vol. 23, No. 2, April 1976