0% found this document useful (0 votes)
51 views10 pages

Brent Elementary

Mathematics

Uploaded by

Lovinf Florin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views10 pages

Brent Elementary

Mathematics

Uploaded by

Lovinf Florin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Fast Multiple-Precision Evaluation of Elementary Functions

RICHARD P. BRENT

Australian National University, Canberra, Australia

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.

2. Reciprocals and Square Roots


In this section we show that reciprocals and square roots of floating-point numbers may
be evaluated, to precision n, in O(M(n)) operations. To simplify the statement of the
following lemma, we assume that M(x) = 0 for all x < 1.
LEMMA 2.1. IrA' C (0, 1),then ~ , ~¢0 M ( T 3n) = O(M(n) ) as n--~ ~.
PI~OOF. If ~ and ~ are as in (1.1), there exists k such that k < a. Thus, ~ 7 ~ M('Y ~n)
_< k ~ - o M(c~n) < kM(n)/(1 - f~) + 0 ( 1 ) , by repeated application of (1.1). Since
M(n) ~ ¢o as n ~ ~ , the result follows.
244 RICHARD P. B R E N T

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)

converges to 1/c with order 2. In fact, if x, = (1 - ~,)/c, substitution in (2.1) gives


*,+l = ,2. Thus, assuming I*01 < ½, we have t*,1 < 2-2' for all i >_ 0, and x~ is a suffi-
ciently good approximation to 1/c if k ~_ log2n. This assumes that (2.1) is satisfied
exactly, but it is easy to show that it is sufficient to use precision n at the last iteration
(~ = k - 1), precision slightly greater than n/2 for i = k - 2, etc. (Details, and more
efficient methods, are given in [4, 6].) Thus the result follows from Lemma 2.1. Since
x / y = x ( 1 / y ) , it is clear that floating-point division may also be done in O ( M ( n ) )
operations.
LEMMA 2.3. I f C ~_ 0 is a floating-point number, then c ~ can be evaluated, to precision n,
in O( M ( n ) ) operations.
PROOF. If C = 0 then ct = 0. If c ~ 0, the proof is similar to that of Lemma 2.2,
using the Newton iteration x,+l -- (x, -b c/x,)~2.
LEMMA 2.4. F o r a n y f i x e d k > O, M ( k n ) = O ( M ( n ) ) a s h - - , ~ .
PRooL Since we can add integers less than 2" in O ( M ( n ) ) operations, we can add
integers less than 2 k" in O ( k M ( n ) ) = O ( M ( n ) ) operations. The multiplication of
integers less than 2 k~ can be split into O(k ~) multiplications of integers less than 2",
and O(k ~) additions, so it can be done in O(k2M(n) ) -- O ( M ( n ) ) operations.

3. Solution of Nonlinear Equations


I n Section 6 we need to solve nonlinear equations to precision n. The following lemma is
sufficient for this application. Stronger results are given in [4, 6].
LEMMA 3.1. I f the equation f ( x ) -- c has a s~mple root ~" ~ O, f ~s Lipsch~tz continuous
near ~', and we can evaluate f ( x ) to precision n in O(M(n)ch(n) ) operations, where ¢h(n)
is a posztive, monotomc ~ncreasing funetwn, for x near ~', then ~"can be evaluated to precision
n in O(M(n)e~(n)) operatzons.
PROOF. Consider the discrete Newton iteration

x,+l = x, -- h,(f(x,) -- c ) / ( f ( x , + h,) - f ( x , ) ) . (3.1)


If h, = 2 -'/~, x, - ~ = 0(2-~/2), and the right side of (3.1) is evaluated with precision
n, then a standard analysis shows that x.+l - ~" = 0 ( 2 - " ) . Since a sufficiently good
starting approximation xo may be found in 0 ( 1 ) operations, the result follows in the
same way as in the proof of Lemma 2.2, using the fact that Lemma 2.1 holds with M ( n )
replaced by M ( n ) ~ ( n ) . The assumption ~"~ 0 is only necessary because we want to
obtain ~ with a relative (not absolute) error 0 ( 2 - ' ) .
Other methods, e.g. the secant method, may also be used if the precision is increased
appropriately at each iteration. I n our applications there is no difficulty in finding a
suitable initial approximation x0 (see Section 6).

4. Results on Elliptic Integrals


I n this section we summarize some classical results from elliptic integral theory. Most of
the results may be found in [1], so proofs are omitted. Elliptic integrals of the first and
second kind are defined by

F(&, a) ffi (1 -- ~/n2a sin~O)-IdO (4.1)


Fast Multiple-Precision Evaluation of Elementary Functions 245

and

E ( ¢ , a ) -- (1 -- sin2a sin20)½dO, (4.2)

respectively. F o r our purposes we m a y assume t h a t a and ~b are in [0, ~r/2]. T h e complete


elliptic integrals, F ( v / 2 , c~) and E(Tr/2, a), are simply w r i t t e n as F ( a ) and E ( a ) ,
respectively.
Legendre's Relation. W e need the i d e n t i t y of Legendre [17]:
E ( a ) F ( ~ r / 2 - a) % E(~r/2 - a ) F ( a ) -- F ( a ) F ( T r / 2 -- a) = ~r/2, (4.3)
and, in particular, the special case
2E(v/4)F(v/4) - (F(v/4))2 = v/2. (4.4)
Small Angle Approximation. F r o m (4.1) it is clear t h a t
F(¢,a) = ~+O(a 2) (4.5)
a s a --~ O.
Large Angle A pproximahon. From(4.1),
F(~/,, a ) = F(~b, ~-/2) + O ( r / 2 -- a)2, (4.6)
uniformly for 0 _< ~ ~ ~bo < ~r/2, as a ~ ~-/2. Also, we note t h a t
F ( ~ , ~r/2) = log tan(Tr/4 + ~b/2). (4.7)
Ascending Landen Transformation. If 0 <, a, < a,~1 < 7r/2, 0 < ~/',+1 < ~b, < 7r/2,
sin a , = t a n 2 ( a , + l / 2 ) , (4.8)
and
sin(2~b,+l - - ¢~,) = sin c~, sin ~b,, (4.9)
then
F ( ~ , + , , a,+,) = [(1 + sin a,)/2]F(~b,, o~,). (4.10)
If s, = sin a , and v, = t a n ( G . / 2 ) , then (4.8) gives
s,+1 = 28~/(1 -t- s,), (4.11)
and (4.9) gives
v,+, = w # ( l + (1 + m~)~), (4.12)
where
wa = tan if,+, = (v, T w 2 ) / ( 1 - v,w2), (4.13)
w2 = tan(ff,+l - ~,/2) = w,/(1 -b (1 - wl2)t), (4.14)
and
wl = sin(2~b,+l -- ¢,) = 2 s x , / ( 1 + v,:). (4.15)
Arithmetic-Geometric Mean Iteration. F r o m the ascending L a n d e n t r a n s f o r m a t i o n
it is possible to derive the arithmetic-geometric m e a n iteration of Gauss [12] and La-
grange [16]: if ao = 1, bo = cos a > 0,
a,+~ = (a, + b , ) / 2 , (4,16)
and
b,+x = (a,b,) t, (4.17)
246 RICHARD P. BRENT

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

An Infinite Product. Lets,,a,,andb, beasabove, with a = ~r/2 - ao , s o so =


b o / a o . F r o m (4.11), (4.16), and (4.17), it follows t h a t s, = b , / a , for all t > 0. T h u s ,
(1 4- s , ) / 2 = a , + l / a , , and

f i [(1 -t- ~,)/2J = hm a, = ~ r / [ 2 F ( T r / 2 - - ao)] (4.21)


t~0 l~oo

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

L e t a o = 1, b0 = co = 2 , A = h m , ~ ® a , , a n d T = lim,~= t, , where a, , b, , and c, are


'~" ~ ,-~j--1 2
detined by (4.16), (4.17), and (4.19) for i _> 1, and t, = ½ - 2..~2~o ~ c~. F r o m (4.4),
(4.18), and (4.20), we h a v e
~r = A 2 / T . (5.1)

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 logan + 0 ( 1 ) iterations are needed, it is necessary to work with precision n


O(log log(n)), even though the algorithm is numerically stable in the conventional
sense. F r o m Lemmas 2.2-2.4, each iteration requires O ( M ( n ) ) operations, so ~" m a y be
evaluated to precision n in O(M(n) log(n)) operations. This is asymptotically faster
t h a n the usual O(n 2) methods [14, 21] if a fast multiplication algorithm is used. A high-
precision computation of ~" by a similar algorithm is described in [10]. Note that, becatrse
the arithmetic-geometric mean iteration is not self-correcting, we cannot obtain a bound
O(M(n) ) in the same way as for the evaluation of reciprocals and square roots b y New-
ton's method.

6. Evaluation of exp(x) and log(x)


Suppose 6 > O fixed, and m E [6, 1 - 6]. If sin ~ o = r o t , we m a y evaluate F(o~0) to pre-
cision n in O(M(n) log(n)) operations, using (4.18) and the arithmetic-geometric
mean iteration, as for the special case F(7r/4) described in Section 5. (When using (4.18)
we need ~-, which m a y be evaluated as described above.) Applying the ascending Landen
transformation (4.8)-(4.10) with ~ = 0, 1, • • • , k - 1 and ~bo = ~'/2 gives

F(~bk, ak) = t,=0 [(1 + s i n a , ) / 2 l F(ao). (6.1)

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

U(m)= i , ~ [(1 - t - s i n a , ) / 2 ] t F(o~o) (6.3)

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.

Algorithm for T(m)

whilel--~ > 2-~do


248 RICHARD P. BRENT

b e g i n W ~-- 2 S V / ( 1 -t- VZ); ,


W *- W / ( 1 + (1 -- WS)t);
W ~- (V + W ) / ( 1 - VW);
V ~ W/(I + (1 + W'p);
,S ~ 2SJ/(1 + ,S)
end;
r e t l l r n (1 "~ V ) / ( 1 -- V).

Properties of U(m) and T(m). F r o m (4.21) and (6.3),


U(m) = ( ~r/2)F( ao)/F( ~'/2 - ceo), (6.6)
where sin c~0 = ni t as before. Both F ( ~ ) and F(~-/2 - ce0) m a y be evaluated by the
arithmetic-geometric mean iteration, which leads to a slightly more efficient algorithm
for U(m) t h a n the one above, because the division by (1 -{- S) in the final "while"
loop is avoided. F r o m (6.5) and (6.6), we have the special cases U(½) = Ir/2 and T(½)
= e "/2. Also, (6.6) gives
U(m)U(1 - m) = ~r~/4, (6.7)
for all m E (0, 1).
Although we shall avoid using values of m near 0 or 1, it is interesting to obtain asymp-
totic expressions for U(m) and T(m) as m --~ 0 or 1. F r o m the algorithm for T(m),
T(1 - ~) = 4~-~ - ~i + O(et) as ~--~ 0. Thus, from (6.5), U(1 - ~) = L(~) - ~/4
-{- O(e~), where L (+) = log (4/~+). Using (6.7), this gives U(~) = r2/[4L(~) ] + O(e/L~),
and hence T(e) = exp(~r2/[4L(~)]) + O(~/L2). Some values of U(m) and T(m) are
given in Table I I .
Evaluation of exp(x). To evaluate exp(x) to precision n, we first use identities such
as exp(2x) = ( e x p ( x ) ) 2 and e x p ( - x ) = 1/exp(x) to reduce the argument to a
suitable domain, say 1 _< x _< 2 (see below). W e then solve the nonlinear equation
U(m) = x, (6.8)
obtaining m to precision n, b y a method such as the one described in Section 3. F r o m
L e m m a 3.1, with (b(n) = log(n), this m a y be done in O(M(n)log(n)) operations.
Finally, we evaluate T(m) to precision n, again using O(M(n)log(n)) operations.
F r o m (6.5) and (6.8), T(m) = exp(x), so we have computed exp(x) to precision n.
A n y preliminary transformations m a y now be undone.
Evaluation of log(x). Since we can evaluate exp(x) to precision n in O(M(n) l o g ( n ) )
operations, L e m m a 3.1 shows t h a t we can also evaluate log(x) in O(M(n) l o g ( n ) )
operations, b y solving the equation exp(y) = x to the desired accuracy. A more direct
method is to solve T(m) = x (after suitable domain reduction), and then evaluate U(m).
Further detads. If x E [1, 2] then the solution m of (6.8) lies in (0.10, 0.75), and it
m a y be verified t h a t the secant method, applied to (6.8), converges if the starting ap-
proximations are m0 = 0.2 and ml = 0.7. If desired, the discrete Newton method or
some other locally convergent method m a y be used after a few iterations of the secant
method have given a good approximation to m.

TABLE II. THE FUNC'rlONS U(m) AND T(m)


m U (m) T (m) m U (m) T (m)

0 01 0 6693 1.9529 0.60 1.7228 5.6004


0.05 0,8593 2.3615 0.70 1.9021 6 6999
0.10 O, 9824 2.6710 0.80 2.1364 8.4688
0.20 1.1549 3.1738 0.90 2.5115 12.3235
0.30 1.2972 3.6591 0.95 2.8714 17.6617
0.40 1.4322 4.1878 0.99 3.6864 39.8997
0.50 1.5708 4.8105
Fast Multiple-Precision Evaluation of Elementary Functions 249

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.

7. Evaluation of Trigonometric Functzons


Suppose 6 > 0 fixed, and x E [6, 1]. Let so = sin s0 = 2 -'j~ and Vo = tan(~b0/2) =
x / ( 1 + (1 + x~)t), so tan ~b0 = x. Applying the ascending Landen transformation, as
for (6.1), gives

F(~bk, ak) = t,~o [(1 +4- s,)/2l F(&0, SoL (7.1)

Also, from (4.5) and the choice of so,


F(~bo, So) = artan(x) + O(2-n). (7.2)
From (4.11), s,+l > s,i, so there is s o m e j < log2n + O(1) such that s~ E [~, ~]. Since
s. -~ 1 with order 2, there is some k .< 2 log2n + O(1) such that 1 - sk = O(2-~). From
(4.6) and (4.7), F(~k, otk) = log t a n ( v / 4 + &k/2) + O(2-"). Thus, from (7.1) and
(7.2),

artan(x) = ~ [2/(1 + s,)]~ log t a n ( r / 4 + ~k/2) + O(2-~). (7.3)


~,,~0 y
If we evaluate tan(~-/4 + ~bk/2) as above, and use the algorithm of Section 6 to evaluate
the logarithm in (7.3), we have artan(x) to precision n in O ( M ( n ) log(n)) operations.
The algorithm may be written as follows.

Algorithm for arian(x), x E [~, 11


S ~ 2-~1~; V ~ x/(1 + (1 + x2)l); Q ~-- 1;
whilel--S > ~do
b e g i n Q ~- 2Q/(I ~- S);
W ~ 2SV/(1 + vs);
W ~--W/(1 + (1 - WS)~);
W ~- (V + W)/(t - VW);
V ~-- W/(1 + (1 + W*)~);
S ~ 2Sy(1 +S)
end ;
return Q log((1 -I- V ) / ( I - V)).

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.

RECEIVED M A R C H 1975; REVISED S E P T E M B E R 1975

Journal of the Aaaociation for Computing Machmery, Vol. 23, No. 2, April 1976

You might also like