Académique Documents
Professionnel Documents
Culture Documents
ALGORITHM
PROCEDURES
ALLAN GIBB*
61
FOR
RANGE
ARITHMETIC
U n i v e r s i t y of A l b e r t a , C a l g a r y , A l b e r t a , C a n a d a
begin
p r o c e d u r e R A N G E S U M (a, b, c, d, e, f);
real a , b , c , d , e , f ;
c o m m e n t The term "range n u m b e r " was used by P. S. Dwyer,
Linear Computations (Wiley, 1951). Machine procedures for
range arithmetic were developed about 1958 by Ramon Moore,
"Automatic Error Analysis in Digital C o m p u t a t i o n , " LMSD
Report 48421, 28 Jan. 1959, Lockheed Missiles and Space Division, Palo Alto, California, 59 pp. If a _< x -< b and c ~ y ~ d,
then R A N G E S U M yields an interval [e, f] such t h a t e =< (x + y)
f. Because of machine operation (truncation or rounding) the
machine sums a -4- c and b -4- d may not provide safe end-points
of the output interval. Thus R A N G E S U M requires a non-local
real procedure A D J U S T S U M which will compensate for the
machine arithmetic. The body of A D J U S T S U M will be dependent upon the type of machine for which it is written and so
is not given here. (An example, however, appears below.) I t
is assumed t h a t A D J U S T S U M has as parameters real v and w,
and integer i, and is accompanied by a non-local real procedure
C O R R E C T I O N which gives an upper bound to the magnitude
of the error involved in the machine representation of a number.
The output A D J U S T S U M provides the left end-point of the
output interval of R A N G E S U M when A D J U S T S U M is called
with i = --1, and the right end-point when called with i = 1
The procedures RANGESUB, R A N G E M P Y , and R A N G E D V D
provide for the remaining fundamental operations in range
arithmetic. R A N G E S Q R gives an interval within which the
square of a range nmnber must lie. R N G S U M C , RNGSUBC,
R N G M P Y C and R N G D V D C provide for range arithmetic with
complex range arguments, i.e. the real and imaginary parts
are range numbers~
begin
e : = A D J U S T S U M (a, c, - 1 ) ;
f : = A D J U S T S U M (b, d, 1)
end RANGESUM;
p r o c e d u r e R A N G E S U B (a, b, c, d, e, f) ;
real a, b , c , d , e , f;
c o m m e n t R A N G E S U M is a non-local procedure;
begin
R A N G E S U M (a, b, - d , --c, e, f)
end RANGESUB ;
p r o c e d u r e R A N G E M P Y (a, b, c, d, e, f);
real a, b, c, d, e, f;
c o m m e n t A D J U S T P R O D , which appears at the end of this
procedure, is analogous to A D J U S T S U M above and is a nonlocal real procedure. M A X and M I N find the maximum and
minimum of a set of real numbers and are non-local;
begin
real v, w;
i f a < 0 A c => 0 t h e n
1: b e g i n
v:=c;
c:=a;
a:=v;
w:=d;
d:=b;
b:=w
e n d 1;
i f a => O t h e n
2: b e g i n
i f c >= 0 t h e n
3:begin
e:= aXe;f := bXd;goto8
end 3 ;
e:=bXc;
ifd ~ 0then
4:
begin
f:=bXd;
goto8
e n d 4;
f:=aXd;
goto8
5: e n d 2;
ifb > 0 then
6: b e g i n
if d > 0 then
begin
e : = M I N ( a X d, b X c);
f : = M A X ( a X c , b X d); go t o 8
e n d 6;
e : = b X c; f : = a X c; go t o 8
e n d 5;
f:=aXc;
i f d _-<O t h e n
7: b e g i n
e:=bXd;
goto8
end 7 ;
e:=aXd;
8: e : = A D J U S T P R O D (e, - 1 ) ;
f := A D J U S T P R O D (f, 1)
end RANGEMPY;
p r o c e d u r e R A N G E D V D (a, b, c, d, e, f) ;
real a, b, c, d, e, f;
c o m m e n t If the range divisor includes zero the program
exists to a non-local label " z e r o d v s r " . R A N G E D V D assumes a
non-local real procedure A D J U S T Q U O T which is analogous
(possibly identical) to A D J U S T P R O D ;
begin
i f c =< 0 A d ~ 0 t h e n go to zer0dvsr;
if c < 0 then
1: b e g i n
ifb > 0 then
2:
begin
e : = b / d ; go t o 3
e n d 2;
e := b/c;
3:
i f a -->_0 t h e n
4:
begin
f : = a / c ; go t o 8
e n d 4;
f : = a / d ; go t o 8
end 1;
if a < 0 then
5: b e g i n
e : = a/c; go t o 6
end 5 ;
e := a/d;
6: i f b > 0 t h e n
7: b e g i n
f : = b / c ; go t o 8
end 7 ;
f := b/d;
8: e : = ADJUSTQUOT (e, - 1 ) ; f : = ADJUSTQUOT (f,1)
end RANGEDVD ;
p r o c e d u r e R A N G E S Q R (a, b, e, f);
r e a l a, b, e, f;
c o m m e n t A D J U S T P R O D is a non-10cal procedure;
begin
if a < 0 then
C o m m u n i c a t i o n s o f t h e &CM
319
1:
begin
if b < 0 t h e n
2:
begin
e : = b X b; f : = a X a; go t o 3
end 2;
e:=0;
m:=MAX(-a,b);
f:=mXm;
goto3
end 1 ;
e : = a X a; f : = b X b;
3:
A D J U S T P R O D (e, - 1 ) ;
A I ) J U S T P R O D (f, 1)
end R A N G E S Q R ;
p r o c e d u r e R N G S U M C (aL, aR, bL, bU, cL, cR, dL, dU, eL,
eR, fL, fU);
real aL, aR, bL, bU, eL, cR, dL, dU, eL, eR, fL, fU;
c o m m e n t Rangesum is a non-local procedure;
begin
R N G M P Y C (aL, aR, bL, bU, cL, cR, - d U , --dL, L1, R1, L2,
R2) ;
R A N G E S Q R (cL, cR, L3, R3);
R A N G E S Q R (dL, dU, L4, R4);
R A N G E S U M (L3, R3, L4, R4, L5, R5);
R A N G E D V D (L1, R1, L5, R5, eL, eR);
R A N G E D V D (L2, R2, L5, R5, fL, fU)
end R N G D V D C
end
EXAMPLE
real procedure
C O R R E C T I O N (p); r e a l p;
c o m m e n t C O R R E C T I O N and the procedures below are intended for use with single-precision "normalized floating-point
arithmetic for machines in which the mantissa of a floating-point
number is expressible to s significant figures, base b. Limitations
of the machine or requirements of the user will limit the range of
p to b m =< I P I < b n+l for some integers m and n. Appropriate
integers must replace s, b, m and n below. Signal is a non-local
label. The procedures of the example would be included in the
same block as the range procedures above;
320
C o m m u n i c a t i o n s o f t h e ACM
begin
i n t e g e r w;
for w := m step 1 until n do
1 : begin
begin
C O R R E C T I O N := b ~ ( w - t - l - s ) ;
g o t o exit
end 2
end 1;
g o t o signal;
exit: e n d
CORRECTION;
A D J U S T S U M (w, v, i);
real procedure
r e a l w, v;
i n t e g e r i;
ifp X i ~ 0 then
begin
A D J U S T P R O D : = p; g o t o out
end 1;
A D J U S T P R O D : = p -4- i X C O R R E C T I O N (p);
out: e n d A D J U S T P R O D ;
c o m m e n t Although ordinarily rounded arithmetic is preferable
to truncated (chopped) arithmetic, for these range procedures
truncated arithmetic leads to closer bounds than rounding does.
1:
ALGORITHM
62
A SET OF ASSOCIATE LEGENDRE
OF THE SECOND KIND*
POLYNOMIALS
JOHN" R. HERNDON
Stanford Research Institute, l~ienlo Park, California
e c ) m m e n t This procedure places a set of values of QnIn(x) in the
array Q[ ] for values of n from 0 to nmax for a particular value
of m and a value of x which is real if ri is 0 and is purely imaginary,
ix, ortherwise. R[ ] will contain the set of ratios of successive
values of Q. These ratios may be especially valuable when the
Q,~"~(x) of the smallest size is so small as to underflow the machine
representation (e.g. 10-6~ if 10-~1 were the smallest representable
n u m b e r ) . 9.9 X 10 45 is u s e d to r e p r e s e n t i n f i n i t y . I m a g i n a r y
v a l u e s of x m a y n o t be n e g a t i v e a n d reM v a l u e s of x m a y n o t be
s m a l l e r t h a n 1.
V a l u e s of Qd~'(x) m a y be c a l c u l a t e d e a s i l y by h y p e r g e o m e t r i c
series if x is n o t t o o s m a l l n o r (n - m ) t o o large. Q~m(x) c a n be
c o m p u t e d f r o m a n a p p r o p r i a t e set of v a l u e s of Pnm(X) if X is n e a r
1.0 or ix is n e a r 0. L o s s of s i g n i f i c a n t d i g i t s o c c u r s for x as s m a l l as
1.1 if n is l a r g e r t h a n 10. L o s s of s i g n i f i c a n t d i g i t s is a m a j o r diffic u l t y in u s i n g finite p o l y n o m i M r e p r e s e n t a t i o n s also if n is l a r g e r
t h a n m . H o w e v e r , Q L E G h a s b e e n t e s t e d in r e g i o n s of x a n d n
b o t h large a n d small;
procedure
Q L E G ( m , n m a x , x, ri, R, Q); v a l u e In, n m a x , x, ri;
r e a l In, m n a x , x, ri; r e a l a r r a y R , Q;
begin
r e a l t, i, n, q0, s;
n : = 20;
i f n m a x > 13 t h e n
n := nmax +7;
i f ri = 0 t h e n
begin
ifm = 0then
Q[0] : = 0.5 X 10g((x + 1 ) / ( x - 1))
else
t : = - - 1 . 0 / s q r t ( x X x - - 1);
begin
q0 : = 0;
Q[O] : = t ;
for i : = 1 step 1 until m do
begin
s :=
(x+x)X(i-1)Xt
Q[0]+ (3i-ii-2)q0;
q0 : = Q[0];
Q[0] : = s e n d e n d ;
if
x = 1 then
Q[0] : = 9.9 I" 45;
R [ n + 1] : = x - s q r t ( x X x - 1);
for
i : = n s t e p --1 u n t i l 1 d o
R[i] : = (i + m ) / ( ( i + i + 1) X x
+(m-i1) X R [ i + l ] ) ;
g o to t h e e n d ;
if m = 0 then
b e g i n i f x < 0.5 t b e n
Q[0] : = a r c t a n ( x ) - 1.5707963 e l s e
Q[0] : = - a r e t a n ( 1 / x ) e n d e l s e
t : = 1 / s q r t ( x X x + 1);
begin
q0 : = 0;
q[0] := t;
for
R[n +
for
for
the:
for i : =
end
QLEG;
i : = 2 step 1 until m do
b e g i n s : = (x + x) X (i -- 1) X t X Q[0I
+(3i+iX
i -- 2) q0;
qO : = Q[0];
Q[0] := s e n d e n d ;
1] : = x - s q r t ( x x + 1);
i := n step - 1 until 1 do
R[i] : = (i + m ) / ( ( i -- m + 1) R[i + 1]
--(i+i+
1) X x);
i : = 1 step 2 until nmax do
Rill : = -- Rill;
1 step 1 until nnmx do
Q[i] : = Q[i - 1] X R[i]
* T h i s p r o c e d u r e w a s d e v e l o p e d in p a r t u n d e r t h e s p o n s o r s h i p
of t h e A i r F o r c e C a m b r i d g e R e s e a r c h C e n t e r .
ALGORITHM 63
PARTITION
C. A. R. HOARE
Elliott Brothers Ltd., Borehamwood, Hertfordshire, Eng.
procedure
conunent
I a n d J are o u t p u t v a r i a b l e s , a n d A is t h e a r r a y ( w i t h
s u b s c r i p t b o u n d s M : N ) w h i c h is o p e r a t e d u p o n b y t h i s p r o c e d u r e .
P a r t i t i o n t a k e s t h e v a l u e X of a r a n d o m e l e m e n t of t h e a r r a y A,
a n d r e a r r a n g e s t h e v a l u e s of t h e e l e m e n t s of t h e a r r a y in s u c h a
w a y t h a t t h e r e e x i s t i n t e g e r s I a n d J w i t h t h e following p r o p e r t i e s :
M _-< J < I =< N p r o v i d e d M
< N
A[R] =< X f o r M
=< R _-< J
A[R] = X f o r J
< R < I
A[R] ~ X f o r I =< R ~ N
The procedure uses an integer procedure random (M,N) which
chooses equiprobably a random integer F between M and N, and
also a p r o c e d u r e e x c h a n g e , w h i c h e x c h a n g e s t h e v a l u e s of its t w o
parameters ;
begin
real X; integer F;
F : = r a n d o m ( M , N ) ; X : = A[F];
I:=M;
J:=N;
for I : = I s t e p 1 u n t i l N d o
up:
i f X < A [I] t h e n g o to d o w n ;
I:=N;
down:
f o r J : = J s t e p --1 u n t i l M d o
if A[J]<X then go to change;
J:=M;
c h a n g e : i f I < J t h e n b e g i n e x c h a n g e (A[IL A[J]);
I := I+ 1;J:=
J - 1;
g o to u p
end
else
i f [ < F t h e n b e g i n e x c h a n g e (A[IL A[F]) i
I:=I+l
end
else
i f F < J t l l e n b e g i n e x c h a n g e (A[F], A[J]) ;
J:=J-1
end ;
end
partition
ALGORITHM 64
QUICKSORT
C. A. R. HOARE
Elliott Brothers Ltd., Borehamwood, Hertfordshire, Eng.
procedure
ALGORITHM 65
FIND
C. A. R. HOARE
Elliott Brothers Ltd., Borehamwood, Hertfordshire, Eng.
procedure
find ( A , M , N , K ) ; v a l u e M , N , K ;
a r r a y A; i n t e g e r M , N , K ;
comment
F i n d will a s s i g n to A [K] t h e v a l u e w h i c h it w o u l d
h a v e if t h e a r r a y A [ M : N ] h a d b e e n s o r t e d . T h e a r r a y A will be
p a r t l y s o r t e d , a n d s u b s e q u e n t e n t r i e s will be f a s t e r t h a n t h e first;
Communications
of the ACM
321
begin
end
integer [,J;
i f M < N t h e n b e g i n p a r t i t i o n (A, M, N, I, J ) ;
i f K=< I t h e n find ( A , M , I , K )
e l s e if J N K t h e n find ( A , J , N , K )
end
find
b e g i n i n t e g e r i, j, k, m ; r e a l a r r a y v [ l : n ] ; r e a l s;
integer array c[l:n];
t a b l e : j : = -- n; k : = n-4- 1; f o r i : = 1 s t e p l u n t i l n d o
begin
j : = j + k - - i; c[il : = j e n d ;
load: f o r i : = l s t e p l u n t i l n d o
b e g i n f o r j : = i s t e p 1 u n t i l n d o R E A D (vii]); m : =
c[i];
ALGORITHM 66
INVRS
JOHN CAFFREY
n--1 do
ALGORITHM 67
CRAM
JOHN CAFFREY
Director of Research,
322
Communications
of the ACM
end
CRAM
R E M A R K ON ALGORITHM 53
Nth ROOTS OF A COMPLEX N U M B E R (John R.
Herndon, Comm. A C M 4, Apr. 1.961)
C. W.
NESTOR, J R .
C o n t r i b u t i o n s to t h i s d e p a r t m e n t m u s t be in t h e f o r m
s t a t e d in t h e A l g o r i t h m s D e p a r t m e n t p o l i c y s t a t e m e n t
(Communications, F e b r u a r y , 1960) e x c e p t t h a t A L G O L 60
n o t a t i o n s h o u l d be used (see Communications, M a y , 1960).
C o n t r i b u t i o n s s h o u l d be s e n t in d u p l i c a t e to J. H .
W e g s t e i n , C o m p u t a t i o n L a b o r a t o r y , N a t i o n a l B u r e a u of
S t a n d a r d s , W a s h i n g t o n 25, D . C. A l g o r i t h m s s h o u l d be in
t h e P u b l i c a t i o n f o r m of A L G O L 60 a n d w r i t t e n in a s t y l e
p a t t e r n e d a f t e r t h e m o s t r e c e n t a l g o r i t h m s a p p e a r i n g in this
department.
A l t h o u g h each a l g o r i t h m has been t e s t e d b y its cont r i b u t o r , no w a r r a n t y , e x p r e s s or i m p l i e d , is m a d e b y t h e
c o n t r i b u t o r , t h e e d i t o r , or t h e A s s o c i a t i o n for C o m p u t i n g
M a c h i n e r y as to t h e a c c u r a c y a n d f u n c t i o n i n g of t h e alg o r i t h m a n d r e l a t e d a l g o r i t h m m a t e r i a l a n d no r e s p o n s i b i l i t y is a s s u m e d b y t h e c o n t r i b u t o r , t h e e d i t o r , or t h e
A s s o c i a t i o n for C o m p u t i n g M a c h i n e r y in c o n n e c t i o n t h e r e with.
T h e r e p r o d u c t i o n of a l g o r i t h m s a p p e a r i n g in t h i s dep a r t m e n t is e x p l i c i t l y p e r m i t t e d w i t h o u t a n y charge. W h e n
r e p r o d u c t i o n is for p u b l i c a t i o n p u r p o s e s , r e f e r e n c e m u s t be
m a d e to t h e a l g o r i t h m a u t h o r a n d t o t h e Communications
issue b e a r i n g t h e a l g o r i t h m .