0% found this document useful (0 votes)
31 views

Algorithms

The document summarizes the algorithm used in Hewlett-Packard handheld calculators to compute square roots. It involves: 1) Guessing an answer and finding the remainder when the guess is squared and subtracted from the original number. 2) Refining the guess digit-by-digit to progressively reduce the remainder until the desired accuracy is reached. 3) Using equations to efficiently determine the next digit at each step by finding the largest possible digit that satisfies the remainder inequality.

Uploaded by

jalejan77
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Algorithms

The document summarizes the algorithm used in Hewlett-Packard handheld calculators to compute square roots. It involves: 1) Guessing an answer and finding the remainder when the guess is squared and subtracted from the original number. 2) Refining the guess digit-by-digit to progressively reduce the remainder until the desired accuracy is reached. 3) Using equations to efficiently determine the next digit at each step by finding the largest possible digit that satisfies the remainder inequality.

Uploaded by

jalejan77
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Personal Calculator Algorithms I

Square Roots
A detailed description of the algorithm used in Hewlett-
Packard hand-held calculators to compute square roots.

by William E. Egbert

BEGINNING WITH THE HP-35,1'2 all HP personal the hundreds digit is found, it is squared and sub
calculators have used essentially the same al tracted fromx, and the tens digit is found. This pro
gorithms for computing complex mathematical func cess, however, is not exactly straightforward, so some
tions in their BCD (binary-coded decimal) micro algebra is in order.
processors. While improvements have been made in The following definitions will be used:
newer calculators,3 the changes have affected primarily x = the number whose square root is desired
special cases and not the fundamental algorithms. a = most significant digit(s) of Vx previously
This article is the first of a series that examines computed
these algorithms and their implementation. Each b = the next digit of Vx to be found
article will present in detail the methods used to j = the power of 10 associated with b
implement a common mathematical function. For RQ = x-a2, the current remainder
simplicity, rigorous proofs will not be given, and dj = the new a when digit b is added in its
special cases other than those of particular interest proper place. a¡ = a+(bxlOj) (1)
will be omitted. Rb = the portion of remainder R „ that would be
Although tailored for efficiency within the environ removed by adding b to a. Rfa = a2-a2 (2)
ment of a special-purpose BCD microprocessor, the For example, let x = 54756. Then Vx = 234.
basic mathematical equations and the techniques Let a = 200.
used to transform and implement them are applicable b = the digit we are seeking (3, in this case)
to a wide range of computing problems and devices. j = 1 (the 10's digit is being computed)
Ra = 54756 -(200)2 = 14756.
The Square Root Algorithm Note that a¡ and Rb will vary with the choice of b.
This article will discuss the algorithm and methods The process of finding Vx one decade at a time
used to implement the square root function. approaches the value of Vx from below. That is, at
The core of the square root algorithm is a simple any point in the computation, a =sVx. Consequently,
approximation technique tailored to be efficient Ra^0.
using the instruction set of a B CD processor. The tech With this in mind it is easy to see that for any
nique is as follows: decade j, the value of b is the largest possible digit so
V~x is desired that
1. Guess an answer a Ea-Rb^0
2. Generate a2 or
3. FindR=x-a2 R b ^ R o - ( 3 )
4. If the magnitude of R is sufficiently small, a = Vx. Using equations 1 and 2 we have
5. If R is a positive number, a is too small.
If R is a negative number, a is too big. Rb = [a-HbxlO')]2 -a2.
6. Depending on the result of step 5, modify a and Expanding and simplifying,
return to step 2. Rb = 2abxlOi + (bxlOi)2. (4)
The magnitude of R will progressively decrease until Inserting (4) into (3) yields the following rule for
the desired accuracy is reached. finding digit b.
This procedure is only a rough outline of the actual Digit b is the largest possible digit so that
square root routine used. The first refinement is 2abxlOi+(bxlO')2 =£ Ra (5)
to avoid having to find a2 and x -a2 each time a is When the digit that satisfies equation 5 is found, a
changed. This is done by finding a one decade at a new a is formed by adding bxlO' to the old a, the
time. In other words, find the hundreds digit of a, decade counter (j) is decremented by 1, and a new
then the tens digit, the units digit, and so on. Once Ra is created; the new Ra is the old Ra minus Rb.

22

© Copr. 1949-1998 Hewlett-Packard Co.


Continuing the previous example, give the value b for the next digit of a. Since multi
x = 54756 plying both sides of an inequality by a positive con
j=1 stant does not change the inequality, equations 3 and
a = 200 6 can be multiplied by the number 5.
x-a2 =Ra = 14756
Applying equation 5 to find b : 5Rb =s 5RQ

b 2abxlO) + (bxlO])2 1 lOaxlO' + tlOi-SjxiO2' (7)


0 0 14756
1 4 1 0 0 10656
2 8 4 0 0 6356 b becomes the largest digit so that 5Rb=s5Ra. The new
3 1 2 9 0 0 1856 5Ra is equal to the old 5flQ minus 5flb.
4 1 7 6 0 0 -2844 These transformations may seem useless until we
examine a few examples of the last term of the right
Thus b =3, since b =4 causes overdraft, i.e., side of (7) for various values of b.
Ra-Rb <0. The new a=200 + 3xl01 = 230. The new
Ra = 1856, the new j = 0. With these new parameters, 10axlO'+05xl02i,b=l
the units digit can be found. 10axlOi + 15xi02i,b=2
This process may seem vaguely familiar, which is 10aXlO' + 25xl02i,b=3
not surprising since upon close inspection it turns
out to be the (usually forgotten) scheme taught in Notice that the two-digit coefficient of 102' con
grade school to find square roots longhand. Of course, sists of (b-1) and a 5. These two digits will be ex
trailing zeros and digits are not written in the long pressed as (b-1) 1 5 in succeeding equations. 10a is
hand scheme. formed by a simple right shift and does not change
To make this process efficient for a calculator, still between terms. If the sum defined in equation 7, as
another refinement is needed. b is incremented by 1, is subtracted from 5na until
(bxio')2 can be expressed as a series, using the overdraft occurs, the digit in the next-to-last digit
fact that the square of an integer b is equal to the sum position is b. Best of all, it is in the exact posi
of the first b odd integers. Thus, tion to form the next digit of a without further ma
nipulation. Redoing the previous example may help
(bxiO')2 = b2 xlO2' clarify matters.
Ra = 14756
b = 1
= £ (2i-l)xl02) = 200
5fla = 73780
For example, 5Ra-5Rb
1 20500 53280
f3xlO']2 = i x 102' -)-3 x 102' -)- 5 x 10
2 21500 31780
3 22500 9280 new 5Ra
= 9 xlO2' -14220 overdraft
4 23500
new value of a
Thus 2abxlO'+(bxlO')2 can be expressed as:
digits
b
2abxlOi+(bxlOi)2 = £ 2a xlO' +(2i-l) XlO2' Notice that when overdraft occurs the new value of
a is already created and the new value of 5Ra can be
found by restoring the previous remainder.
or Decrementing the value of j would cause, in effect,
(10a x 10') to shift right one place, and (b-1) | 5 x 102i to
shift right two places. The result is that the final 5
= 2 2axlO' + (2i-l)xl02' (6) shifts one place to the right to make room for a new
digit. Continuing with the same example,
5RQ = 9280
Now comes a key transformation in the square root a = 230
routine. It was shown earlier how inequality 3 will j=0

23

© Copr. 1949-1998 Hewlett-Packard Co.


b 10axiO'+(b-l) 5X102Ã 5fÃ-a-5Rb 3. With an original a of 0, use the method de
1 2 3 0 5 6975 scribed above to find 12 b digits to form the
2 2 3 1 5 4660 mantissa of the answer.
3 2 3 2 5 2335 4. Round the mantissa and attach the exponent
4 2 3 3 5 O remainder found previously.
5 2 3 4 5 -2345 overdraft 5. Display the answer.
final a = The calculator is now ready for another operation, ff

For ease of understanding, the preceding example References


treated a large positive number. A number in the cal 1. T.M. Whitney, F. Rodé, and C.C. Tung, "The 'Powerful
culator actually consists of a mantissa between 1 and Pocketful': An Electronic Calculator Challenges the Slide
10 and an exponent. The problem is to find the square Rule," Hewlett-Packard Journal, June 1972.
root of both parts of this argument. Happily, if the 2. D.S. Cochran, "Algorithms and Accuracy in the
HP-35," Hewlett-Packard Journal, June 1972.
input exponent is an even number, the portion of the
3. D.W. Harms, "The New Accuracy: Making 23 = 8,"
answer resulting from it turns out to be the exponent Hewlett-Packard Journal, November 1976.
of the final answer and is simply the input exponent
divided by 2. Thus to find Vx, the exponent of x is
first made even and the mantissa shifted to keep the
number the same. The exponent of Vx is found by
dividing the corrected input exponent by 2. The
method described above is then used to find the
square root of the shifted input mantissa, which (after
possibly being shifted) can be between 1 and 100. The William E. Egbert
result will then be between 1 and 10, which is the Bill Egbert Is a project leader at
range required for the mantissa of Vx". HP's Corvaills, Oregon Division.
He produced this series of algorithm
During the process of finding Vx the remainder articles as part of his work on the
fla progressively decreases. To avoid losing accu HP-67 and HP-97 Programmable
racy, this remainder is multiplied by 10' after finding Calculators. He was project leader
each new digit b. This avoids shifting a at all, once for the HP-67 and did micro
programming for both calculators.
the square root extraction process begins. A 12-digit
Bill received his BSEE degree from
mantissa is generated, which insures accuracy to Brigham Young University in 1973
±1 in the tenth digit of the mantissa of Vx, and his MSEE from Stanford Uni
In summary, the computation of Vx proceeds as versity in 1976. He's been with HP
follows: since 1973. Born in Fallón, Nevada,
1. Generate exponent of answer. he's married, has two small children,
and lives in Corvallis.
2. Multiply mantissa by 5 to create original 5na

Bulk Rate
Hewlett-Packard Company, 1501 Page Mill U.S. Postage
Road, Palo Alto, California 94304 Paid
Hewlett-Packard
Company
HEWLETT-PACKARDJOURNAL
MAY 1977 Volume 28 • Number 9

Technical information from the Laboratories of


Hewlett-Packard Company

Hewlett-Packard Central Mailing Department


Van Heuven Goedhartlaan 121
Amstelveen-1134 The Netherlands
Yokogawa-Hewlett-Packard Ltd., Shibuya-Ku
Tokyo 151 Japan 20S107JOHNAAA8LAOUA 165
Editorial Director • Howard L. Roberts
Managing Editor • Richard P. Dolan
Art Director, Photographer • Arvid A. Danielson
Illustrator . Susan E. Wright
Administrative Services, Typography • Anne S. LoPresti
European Production Manager • Dick Leeksma «0 ¿QS W
~ /~\ name A your off). r~) p O O . To change your address or delete your name from our mailing list please send us your old address label (it peels off).
L. V-yl Mill U.S.A. \-J 11 COO . Send changes to Hewlett-Packard Journal, 1501 Page Mill Road, Palo Alto, California 94304 U.S.A. Allow 60 days.

© Copr. 1949-1998 Hewlett-Packard Co.


Personal Calculator Algorithms II
Trigonometric Functions
A detailed explanation of the algorithms used by HP
hand-held calculators to compute sine, cosine, and
tangent.
by William E. Egbert

BEGINNING WITH THE HP-35,1'2 all HP personal Scaling


calculators have used essentially the same al Since 6 and 0+n(360°) yield identical trigonometric
gorithms for computing complex mathematical func functions, every angular argument is resolved to a
tions in their BCD (binary-coded decimal) micro positive angle between 0° and 360°. For reasons to be
processors. While improvements have been made in explained later, all calculations assume angles ex
newer calculators,3 the changes have affected primarily pressed in radians. An angle in degrees is first con
special cases and not the fundamental algorithms. verted to radians by:
This article is the second of a series that examines
these algorithms and their implementation. Each 0rad = 0deg X TT/180.
article will present in detail the methods used to
implement a common mathematical function. For
simplicity, rigorous proofs will not be given, and Angles expressed in grads are also converted using
special cases other than those of particular interest the appropriate scale factor.
will be omitted. Once 6 is in radians, 2-rr is subtracted repeatedly
Although tailored for efficiency within the environ from 1 8 1 until the absolute remainder is between 0
ment of a special-purpose BCD microprocessor, the and 2 77. For large angles this would take a long time.
basic mathematical equations and the techniques In such cases 27rxl0n can be subtracted in a process
used to transform and implement them are applicable similar to division. Suppose an angle 6 is expressed
to a wide range of computing problems and devices. in scientific notation (e.g., 8.5X 10s). 2-rr x 10", or 6.28...
xlOn, is then repeatedly subtracted from 6 until
the result becomes negative (underflow). Thus 6.28...
The Trigonometric Function Algorithm x 10s is subtracted from 8.5 x 10s twice and underflow
This article will discuss the method of generating occurs. 6. 28... x 10s is then added to the negative re
sine, cosine, and tangent. To minimize program mainder to give a number between 0 and ITT x 10s, in
length, a single function, tan B, is generated first. this case 2.2X105. The remainder is expressed now as
Once tan 6 is calculated, sin 6 is found by the formula 22xl04 and the process is repeated, this time sub
tracting 277X104. With this method, large angles are
±tanfl quickly resolved.
sin 6 =
Vl+tan20 The problem with this scaling process is that in cur
rent computers numbers can be expressed only to a
It turns out (as will be explained later) that cot 6 can limited number of digits, so 2-77 and therefore ZTTX 10n
easily be generated while generating tan 6. Then cos 6 cannot be expressed exactly. Error creeps in with
is calculated using the formula each shift of the remainder. Thus, the larger the angle,
the fewer significant digits remain in the scaled re
±cot0 sult. A rule of thumb for rough estimates is that for
cos 0 - each count in the exponent, one digit of accuracy
Vl+cot20 will be lost. For example, 5 x 10s when scaled will
lose five digits of accuracy.
It can be seen that these formulas are identical, ex A negative argument is treated the same as a posi
cept for the contangent replacing the tangent. Thus tive number until the end, when the scaling routine
the same routine can solve for either sine or cosine returns a number between 0 and -2-7T. Then 2rr is
depending on whether the argument is tangent or added to the negative result, giving again a number
cotangent. between 0 and 2-rr. This addition of 2-rr causes a digit

© Copr. 1949-1998 Hewlett-Packard Co.


to be lost, which results in asymmetry such as
cos(86°) £ cos(-86°). Newer calculators obviate this
problem by scaling to a number between 0 and 77/4.

Vector Rotation
An angle can be expressed as a vector having X
and Y components and a resultant R (see Fig. 1).
If R is the unit vector, then X=cos 9 and Y=sin 0.
However, regardless of the length of R, Y/X = tan 0and
X/Y=cot0. This holds true for all values of 6
from 0 to 277. Thus, if some way could be found
to generate X and Y for a given 9, all the trigonometric
functions could be found.
In vector geometry a useful formula results when Fig. 2.
one rotates a vector through a given angle. Let
us suppose we have a vector whose angle is 6\, by tan 02 and added or subtracted as needed. If 02 is
and we know its components Xa and Yj (see Fig. 2). chosen so that tan 02 is a simple power of 10
The X2 and Y2 that result when the vector is rotated (i.e., 1, 0.1, 0.01,...) then the multiplications simply
an additional angle 02 are given by: amount to shifting X: and Yj. Thus to generate X2'
and Y2', only a shift and an add or subtract are needed.
X2 = Xa cos 62 - Ya sin 02
Y2 = Y! cos 02 + Xa sin 02 Pseudo-Division
The tangent of 0 is found as follows. First 0 is
Dividing both sides of these equations by cos 02 divided into a sum of smaller angles whose tangents
gives: are powers of 10. The angles are tan"1 (1) = 45°,
tan"1 (0.1) = 5.7°, tan'1 (0.01) « 0.57°, tan'1 (0.001)
X, =0.057°, tan'1 (0.0001)=0.0057°, and so on.
= X1- Yjtan 82 = X2' This process is called pseudo-division. First, 45° is
cos 02 subtracted from 0 until overdraft, keeping track of
(1) the number of subtractions. The remainder is restored
by adding 45°. Then 5.7° is repeatedly subtracted, again
= Y! +X1tan02 = Y2' keeping track of the number of subtractions. This pro
COS 02
cess is repeated with smaller and smaller angles.
Note that X2' and Y2', while not the true values of X2 Thus:
and Y2, both differ by the same factor, cos 02. Thus
Y2'/X2' = Y2/X2. From Fig. 2 it is plain that the quo 0 = q0 tan l (1) + qa tan"1 (0.1) +q2 tan"1 (0.01)... +r
tient Y2'/X2' is equal to tan (0a+ 02). Thus the tan
gent of a large angle can be found by manipulating The coefficients q¡ refer to the number of subtractions
smaJJer angles whose sum equals the large one. Re possible in each decade. Each q¡ is equal to or less
turning to equation 1 above, it can be seen that to than 10, so it can be stored in a single four-bit digit.
generate X2' and Y2'f Xa and Yj need to be multiplied This process of pseudo-division is one reason that
all the trigonometric functions are done in radians.
For accuracy, tan'^lO"') needs to be expressed to
ten digits. In degrees, these constants are random digits
and require considerable ROM (read-only memory)
space to store. However, in radians, they become, for
the most part, nines followed by sixes. Because of this,
they can be generated arithmetically, thus using
fewer ROM states. Also, in radians, tan"1 (1) = 77/4,
which is needed anyway to generate TT. The problem
with using radians is that since 77 is an irrational num
ber, scaling errors occur as discussed earlier. This
means cardinal points do not give exact answers. For
example, sin (720°) ¿ O when calculated this way
but rather 4xlO~9. See reference 3 for a discussion
Fig. 1. of this point.

18

© Copr. 1949-1998 Hewlett-Packard Co.


So far, a pseudo-quotient has been generated that 12300
represents the division of the given angle 6 into 456
smaller angles whose tangents are powers of 10. In 12756
many HP calculators the pseudo-quotient is five
hexadecimal digits long. Each digit represents one The digits in both answers are exactly the same. The
series of subtractions and is a number from 0 to 10. only difference between the two is that the second
For example, if 6 were 359.9999°, the pseudo-quotient answer is 100 times the correct value, which is the
would be 77877, representing 6 = 7tan~a(l) same value by which Y was multiplied before the
+ 7tan~a(0.1) + Stan-^O.Ol) + 7tan~1(0.001) addition. Thus to avoid shifting X, Y must be multi
+ 7tan~1(0.0001). There may also be a remainder r, plied by 10'.
which is the angle remaining after the previous par Expanding this method to the problem at hand also
tial quotient subtractions have taken place. helps us solve another problem, that of accuracy.
Tan 9 can now be found using the vector rotation During pseudo-division, the angle 6 is resolved until
process discussed earlier. a small angle r is left as the original Y value. Since
this is done in fixed point arithmetic, zero digits are
Pseudo-Multiplication generated following the decimal point (e.g., .00123).
To use equation 1 we need an initial Xl and Yv Since zero digits do not convey information except
These correspond to the X and Y of the residual to indicate the decimal point, the remainder is shifted
angle r discussed previously. This angle is small left one place (multiplied by 10) during each decade
(less than 0.001°), and for small angles in radians, of pseudo-division. This preserves an extra digit of
sin 6 = 6 (another reason to use radians instead of accuracy with each decade. The final remainder is
degrees). Thus, to good accuracy, the initial Ya can equal to rxlO4 if the pseudo-quotient is five digits
be set to the residual angle, and the initial Xa set to 1. long.
Equation 1 can now be repeatedly applied, where 02 To demonstrate mathematically the implementa
is the angle whose tangent is 10"'. Each time equa tion that requires only a single register shift, return
tion 1 is applied, a new Xa and Ya are generated, i.e., to equation 1 and replace tan 62 by 10~'. This substi
X2' and Y2'. The number of times equation 1 is tution is legal because 62 = tan"1 (10~'), where j is
applied is determined by the count in the pseudo- the decade digit.
quotient digit for that 6 . Thus if the original angle
had a 3 in the pseudo-quotient digit corresponding to X2' = X^
(2)
tan~a0.1, or 5.7°, equation 1 would be applied three Y2' =
times with Xa and Yl being shifted one place right
for tan (tan~10.1) before the addition or subtraction.
In this manner, new Xa and Yl are formed as the vec Now let Z = Yj x 10', or Ya =ZxlO~¡. Substituting
tor is rotated the amount corresponding to the count in equation 2,
in the pseudo-quotient digits which, of course, sum ./\.i1 ~~ /\.i1 — /< X 1U
to the original angle 6. 2 1
Equation 1 shows that to generate X2 requires a Multiplying the second equation by 10' gives:
shift of Yj and a subtraction from Xa. Likewise Y2 Y2'xlO' = Z+X!
requires a shift of X} and an addition to Yv To imple The left-hand side (Y2'xlO') is in the correct form
ment this would require either two extra registers to to be the new Z for the next iteration. Thus for each
hold the shifted values of Xj and Ya, or else shifting iteration within a decade:
one register twice and the other once. It would be -A-2 = -A-i ¿tXlU
(3)
desirable to shift only one register once. Happily, Y2'xlO' = Z+Xj
this is possible. Consider the following: Let Y = 123 X2' becomes the new Xt
and X = 456. Suppose we want Y + (Xx 0.01). This can Y2'xlO' becomes the new Z
be obtained by keeping the decimal points in the Since the shifted remainder (rxlO4) is desired as Z
same places and shifting X two places right. for the first iteration, the original j is 4.
To implement equation 3, Xj and Z are stored in
123 two registers. ZxlO~2'is formed and stored in a third
+ 4.56 register. Xa is added to Z to form the new Z. This
127.56 leaves Xj undisturbed so that ZxlO"2' can be sub
tracted from it to form the new X2'.
Now suppose instead of shifting X two places right, This implementation saves extra shifts and in
we multiply Y by 100, shifting it two places left. creases accuracy by removing leading zeros in Z.
What happens? The only register shifted is Z.

19

© Copr. 1949-1998 Hewlett-Packard Co.


After equation 3 has been applied the number of sine of the input angle.
times indicated by one pseudo-quotient digit, Z is 4. With X and Y, compute the required function
shifted right one place, and a new pseudo-quotient using elementary operations.
digit is fetched. This in effect creates Y5 x 10', where j 5. Round and display the answer.
is one less than before. Again equation 3 is applied, The calculator is now ready for another operation.^
and the process is repeated until all five pseudo-
quotient digits have been exhausted. The result is an References
X and a Y that are proportional to the cosine and sine 1. T.M. Whitney, F. Rodé, and C.C. Tung, "The 'Powerful
of the angle 6. Because the final j is zero, the final Y Pocketful': An Electronic Calculator Challenges the Slide
( = Z) is correctly normalized with respect to X. Rule," Hewlett-Packard Journal, June 1972.
So far, then, an X and a Y have been generated by a 2. D.S. Cochran, "Algorithms and Accuracy in the
pseudo-multiply operation consisting of shifts and HP-35," Hewlett-Packard Journal, June 1972.
additions. If tan 9 is required, Y/X is generated, which 3. D.W. Harms, "The New Accuracy: Making 23 = 8,"
Hewlett-Packard Journal, November 1976.
is the correct answer. For sin 6, Y/X is calculated, and
for cos 0, X/Y is calculated. Then either X/Y or Y/X is
operated on by the routine described at the beginning
William E. Egbert
of this article. The only difference between the com
Bill Egbert is a project leader at
putation for sin 0 and that for cos 6 is whether X and HP's Corvallis, Oregon Division.
Y are exchanged. He produced this series of algorithm
In summary, the computation of trigonometric articles as part of his work on the
functions proceeds as follows: HP-67 and HP-97 Programmable
1. Scale the input angle to a number in radians Calculators. He was project leader
for the HP-67 and did micro
between 0 and 2rr. programming for both calculators.
2. Using the pseudo-division process divide Bill received his BSEE degree from
the scaled number into groups of selected Brigham Young University in 1973
smaller angles. and his MSEE from Stanford Uni
3. With the pseudo-multiply process of equation 3 versity in 1976. He's been with HP
since 1 973. Born in Fallón, Nevada,
applied once for each angle resulting from the he's married, has two small children,
division of the input argument, generate an X and lives in Corvallis.
and a Y that are proportional to the sine and co

Bulk Rate
Hewlett-Packard Company, 1501 Page Mill U.S. Postage
Road, Palo Alto, California 94304 Paid
Hewlett-Packard
Company
HEWLETT-PACKARDJOURNAL
JUNE 1977 Volume 28 . Number 10

Technical information from the Laboratories of


Hewlett-Packard Company

Hewlett-Packard Central Mailing Department


Van Heuven Goedhartlaan 121
Amstelveen-1134 The Netherlands
Yokogawa-Hewlett-Packard Ltd.. Shibuya-Ku
Tokyo 151 Japan

Editorial Director • Howard L. Roberts


Managing Editor • Richard P. Dolan
Art Director, Photographer • Arvid A. Danielson
Illustrator • Susan E. Wright
Administrative Services. Typography • Anne S. LoPresti
European Production Manager • Dick Leeksma

/^ I j delete N mailing /"> _ peels [~~ A I I |~) p O O • To Cnan9e y°ur address or delete your name from our mailing list please send us your old address label (it peels off)
V-/I I/"\IN Allow days \_/l f\\-J LJ Pi C.OO.Send changes to Hewlett-Packard Journal, 1501 Page Mill Road, Palo Alto, California 94304 U.S.A. Allow 60 days

© Copr. 1949-1998 Hewlett-Packard Co.


Personal Calculator Algorithms III:
Inverse Trigonometric Functions
A detailed description of the algorithms used in
Hewlett-Packard hand-held calculators to compute
arc sine, arc cosine, and arc tangent.

by William E. Egbert

BEGINNING WITH THE HP-35,1'2 all HP personal assumed to be positive and the sign of the input argu
calculators have used essentially the same al ment becomes the sign of the answer. All angles are
gorithms for computing complex mathematical func calculated in radians and converted to degrees or
tions in their BCD (binary-coded decimal) micro grads if necessary.
processors. While improvements have been made in
newer calculators,3 the changes have affected primarily General Algorithm
special cases and not the fundamental algorithms. A vector rotation process similar to that used in the
This article is the third of a series that examines trigonometric routine is used in the inverse process
these algorithms and their implementation. Each as well. A vector expressed in its X and Y components
article presents in detail the methods used to imple can easily be rotated through certain specific angles
ment a common mathematical function. For sim using nothing more than shifts and adds of simple
plicity, rigorous proofs are not given, and special integers. In the algorithm for tan"1 A|, the input
cases other than those of particular interest are argument is | A | , or | tan 6 , where 6 is the unknown.
omitted. Letting tan 6 = Ya/X1; |A| can be expressed as |A /I,
Although tailored for efficiency within the environ where Y: = A| and Xl = 1. A vector rotation pro
ment of a special-purpose BCD microprocessor, the cess (see Fig. 1) is then used to rotate the vector clock
basic mathematical equations and the techniques wise through a series of successively smaller angles
used to transform and implement them are applicable 0¡, counting the number of rotations for each angle,
to a wide range of computing problems and devices. until the Y2 component approaches zero. If q¡
denotes the number of rotations for 8i then
Inverse Trigonometric Functions
This article will discuss the method of generating \6\ = q0 + ql6l +...+ qiflj +...
sin"1, cos"1, and tan"1. An understanding of the
trigonometric function algorithm is assumed. This This process is described in detail below.
was covered in the second article of this series and
the detailed discussion will not be repeated here.4 Vector Rotation
To minimize program length, the function tan-1A To initialize the algorithm, A and 1 are stored in
is always computed, regardless of the inverse trig- fixed-point format in registers corresponding to Y-,
onometric function required. If sin-1A is desired,
A/V 1-A2 is computed first, since

sin A = tan - i Direction of Rotation

For cos A, sin A is computed as above and then


cos"1 A is calculated using

cos"1 A = 7T/2 - sin"aA.

Cos"1 is found in the range 0=£0=£7r and sin"1 and


tan"1 are computed for the range -^12^6^17/2. The
tan"1 routine solves only for angles between 0
and 77/2, since -tan A = tan (-A). Thus A may be Fig. 1. Vector rotation.

22

© Copr. 1949-1998 Hewlett-Packard Co.


and Xa. This is done in such a way as to preserve as metric routine. Thus:
many digits of A as possible when the exponent of A
differs from zero. 6 = q0 tan"1 (I) + qi tan"1 (0.1)
At this point the sign of A is saved and Ya = | A| . +02 tan"1 (0.01) +...+r
Now comes the vector rotation (see Fig. 1). If the vec
tor R is rotated in a clockwise direction, Y2 becomes
Each coefficient q¡ refers to the count in a particular
smaller and smaller until it passes zero and becomes
pseudo-quotient digit.
negative. As soon as Y2 becomes negative, we know
The result of this summation process, also called
that we have rotated R just past the desired angle 0.
Thus to find 6, R is simply rotated clockwise until pseudo-multiplication, is an angle 6 that is equal to
Y2 becomes negative. The amount of rotation is re tan"1 A , where A | is the input argument to the tan"1
membered and is equal to the desired angle 0 = routine. At this point the original sign of A is appended
tan"1 A | . To rotate R, the following formula is used. to 6. For tan"1 this angle is normalized, converted
to degrees or grads if necessary, and displayed. Re
x call that for sin"1, A/Vl-A2 was first generated.
2 = X-i + Y! tan 02 = X2' Thus for sin"1, the result of the tan"1 routine is again
COS0
simply normalized, converted to degrees or grads if
(11
necessary, and displayed. For cos"1, the tan"1 routine
Y2
= Y! -Xjtan02 = Y2' . returns sin"1. Cos"1 is then simply found as

This equation is the same as equation 1 of the Summary


article on trigonometric functions,4 except that the In summary, the computation of inverse trigono
plus and minus signs are exchanged because R is metric functions proceeds as follows:
rotated in the opposite direction. As before, tan 02 is 1. Calculate A/V 1-A2 if the desired function is
chosen such that the implementation requires a simple sm A or cos 1 A.
shift and add (tan 62 = 10"'). To find 6, R is initially 2. Place A | and 1 in fixed-point format into ap
rotated with tan 82 = 1 (02 = 45°). Y2' soon becomes propriate registers, while preserving the sign
negative and the number of successful rotations is of A.
stored as the first digit of what is known as the pseudo- 3. Repeatedly rotate the vector with A = Y and 1 = X
quotient. Y2' is then restored to the last value it had clockwise using equation 1 until Y approaches
before becoming negative and R is rotated again, this zero. The number of rotations and the amount
time through a smaller angle, i.e., tan 62 = 0.1 of each rotation is stored as a pseudo-quotient
(02 = 5.7°). This process is repeated with the angle of along the way.
rotation becoming smaller and smaller until five 4. Using the pseudo-multiplication process of
pseudo-quotient digits have been generated. equation 2, sum all of the angles used in the ro
At the end of each series of rotations, Y2 is multi tation to form 1 0 1 .
plied by 10 to preserve accuracy. 5. Append the proper sign to the answer and cal
culate cos"1 A = 77/2 -sin"1 A if required.
Pseudo-Multiplication 6. Convert to the selected angle mode, and round
It is now time to shift gears and add up all the small and display the answer.
angles represented by the pseudo-quotient digits. The calculator is now ready for another operation. -
There remains a residual angle ,r, represented by the
final X2' and Y2'. Since the residual angle is small, References
we would like to say Y2' = sin r = r. However, 1. T.M. Whitney, F. Rodé, and C.C. Tung, 'The Power
this is true only if X2' = 1. Unfortunately, X2' in this ful Pocketful': An Electronic Calculator Challenges the
case is the product of all the I/cos d terms result Slide Rule," Hewlett-Packard Journal, June 1972.
ing from several applications of equation 1. How 2. D.S. Cochran, "Algorithms and Accuracy in the
ever, Y2' is this same product times Y2. Thus HP-35," Hewlett-Packard Journal, June 1972.
Y2'/X2' = Y2/l. Therefore, the final Y2' is divided by 3. D.W. Harms, "The New Accuracy: Making 23 = 8,"
the final X2' and the result is sin r, which for small Hewlett-Packard Journal, November 1976.
4. W.E. Egbert, "Personal Calculator Algorithms II:
angles in radians is approximately equal to r, the
Trigonometric Functions," Hewlett-Packard Journal, June
residual angle. 1977.
With the residual angle as the first partial sum, 6 is
generated by adding the angles represented by the
digits of the pseudo-quotient. This is exactly the re BiJI Egbert is a project manager at HP's CorvaJJis
verse of the pseudo-division operation in the trigono (Oregon] Division.

23

© Copr. 1949-1998 Hewlett-Packard Co.


Personal Calculator Algorithms IV
Logarithmic Functions
A detailed description of the algorithms used in
Hewlett-Packard hand-held calculators to compute
logarithms.

by William E. Egbert

BEGINNING WITH THE HP-35,1'2 all HP personal Exponent


calculators have used essentially the same al Numbers in HP calculators are stored in scientific
gorithms for computing complex mathematical func notation in the form x = M-10K. M is a number
tions in their BCD (binary-coded decimal) micropro whose magnitude is between 1.00 and 9.999999999
cessors. While improvements have been made in and K is an integer between -99 and +99. Using
newer calculators,3 the changes have affected equation 1, it is easy to see that
primarily special cases and not the fundamental
algorithms. ln(M-10K) = ln(M) + ln(10K)
This article is the fourth in a series that examines
these algorithms and their implementation.4'516 Each At this point, another logarithmic property becomes
article presents in detail the methods used to imple useful, which is
ment a common mathematical function. For simplic
ity, rigorous proofs are not given and special cases ln(Ab) = b-ln(A).
other than those of particular interest are omitted.
Although tailored for efficiency within the en Using this relationship
vironment of a special-purpose BCD microprocessor,
the basic mathematical equations and the techniques ln(M-10K) = ln(M) + K-ln(lO).
used to transform and implement them are applicable
to a wide range of computing problems and devices. Thus to find the logarithm of a number in scientific
notation, one calculates the logarithm of the mantissa
of the number and adds that to the exponent times
The Logarithmic Function Algorithm
This article will discuss the method of generating
the ln(x) and Iog10(x) functions. To minimize pro Mantissa
gram length, a single function, ln(x), is always com The problem of finding ln(x) is now reduced to
puted first. Once ln(x) is calculated, Iog10(x) is found finding the logarithm of its mantissa M.
by the formula Let P = 1/M. Then

ln(x) ln(PM) = ln(P) + ln(M)


Iog1
ln(l) = ln(P) + ln(M)
0 = ln(P) + ln(M)
ln(x) is generated using an approximation process - l n ( P ) = l n ( M ) ( 2 )
much the same as the one used to compute trigono
metric functions.5 The fundamental equation used in This may appear to be a useless exercise since at
this case is the logarithmic property that first glance -ln(P) seems to be as hard to compute as

In anj = InfaJ + In(a2) Suppose, however, that a new number Pn is formed


+ln(an) by multiplying P by r which is a small number close
to 1.
This algorithm simply transforms the input number
x into a product of several terms whose logarithms Pn=P-r
are known. The sum of the logarithms of these various
partial-product terms forms ln(x). In addition, let Pn be defined as a product of powers

29

© Copr. 1949-1998 Hewlett-Packard Co.


of numbers a¡ whose natural logarithms are known. between 0 and 1 if the product PnM is to approach 1.
As M is defined to be between 1 and 10, a new quan
P â € ” a K g K i K ; . . â € ž K n tity A is formed by dividing M by 10. A is now in the
n - aO al ••• aj ' ••• an
proper range (0.1=sA<l) so that using the a¡ terms as
defined will cause the product APn to approach
Thus
1 without exceeding 1.
The product Pn can now be formally defined as a
P = Pn/r series, where j goes from 0 to n. Each partial product
AP¡ has the form
ln(P) = ln(PJ - ln(r)

Using equation 2 A-Pj = A-Pj_a(l+10-J) ', j = 0,1, 2,..., n

ln(M) = ln(r) - ln(PJ P_! = 1, and Kj is the largest integer such that P¡ <1.
In practice, each A-P¡ is formed by multiplying
Finally A-Pj.j by (1 + 10~'), KJ times. There is one inter
mediate product, T¡, for each count of K¡, as shown
ln(M) = ln(r) - (K0ln(a0j + K^nfaJ + ... + Kjln(aj) below.
+ ... + Knln(an))
TO = A(l 10"
Thus to find ln(M) one simply multiplies M by the
carefully selected numbers a¡ so that the product
! = A(l + 10"
MPn is forced to approach 1. If all the logarithms of
a¡ are added up along the way to form ln(Pn) then
ln(M) is the logarithm of the remainder r minus this TKo =A(1
sum. Notice that the remainder r is nothing more than
the final product MPn.
Implementation KO+I 10 Â
-°K
)°1(
How is this algorithm implemented in a special-
purpose microprocessor? First of all, the terms of Pn
Tm =
were chosen to reduce computation time and mini
mize the amount of ROM (read-only memory) needed ...(1 = APn
to store a¡ and its logarithm. The numbers chosen for
the a¡ terms are of the form a¡ = (1 + 10"'), where
m= + K
j = 0-4 (see Table 1).

Table 1 Values of a¡ Terms T¡ = for some j (4)

Ina¡ Notice that each multiplication of the intermediate


"J
product T¡_j by a¡ simply amounts to shifting T¡_!
0 2 0.6931 right the number of digits denoted by the current
1 1.1 0.09531 value of j and adding the shifted value to the original
2 1.01 0.009950 TÃ-I- This very efficient multiplication method is
1.001 0.0009995 similar to the pseudo-multiplication of the trigono
1.0001 0.000099995 metric algorithm.5

To achieve high accuracy using relatively few a¡ An Example


terms, an approximation is used when r = MPn A numeric example to illustrate this process is now
approaches 1. For numbers close to 1, ln(r) r— 1. in order. Let A = 0.155. To compute ln(A), A must be
This yields multiplied by factors of a¡ until APn approaches 1.
To begin the process A = 0.155 is multiplied by a0 = 2
to form the intermediate product T0 = 0.31. Another
In M = (r-l) - £ K¡ln(a¡ (3] multiplication by a0 gives Ta = 0.62. A third multi
i=o plication by 2 results in 1.24, which is larger than 1.
Thus K0 = 2 and AP0 = 0.62. The process is con
Since all of the a¡ terms are larger than 1 , M must be tinued in Table 2.

30

© Copr. 1949-1998 Hewlett-Packard Co.


cessor's capabilities. Recall that the factors a¡ were
Table 2 Generation of ln(0.155) chosen to force the product PnA towards 1. Suppose
Bj = TJ -1. Forcing Bm towards 0 causes PnA to be
T ln(a¡) forced to 1. Substituting B¡ into (4) and simplifying
yields
0.155 0.155
2 0.31 0.6931
2 0.62 0.62 0.6931 (B; + l) =(Bi_1+l)(14-10~i] for some j

B ; + l = 8 ^ ( 1 + 1 0

B = 10~')4-10

Multiplying through by -1 results in the following


equation, which is equivalent to equation 4.

(5)

0.9996 = A-P4 = r 1.8638 = £ ln(aj) This expression is now in a very useful form, since
the a¡ term is the same as before, but the zero test is
performed automatically when the 10 ' subtraction
*Another x2 would result in AP3 >1. Thus a¡ is is done. A test for a borrow is all that is required. An
changed to 1.1.
additional benefit of this transformation is that accu
**The 1.01 constant is skipped entirely.
racy can be increased by shifting -B¡ left one digit for
each 3j term after it has been applied the maximum
Applying the values found in Table 2 to equation 3 number of times possible. This increases accuracy by
results in replacing zeros generated as B¡ approaches zero with
significant digits that otherwise would have been lost
ln(0.155) = (0.9996 - 1) -1.8638 out of the right end of the register. This shifting,
= -1.8642 which is equivalent to a multiplication by 10', gives
yet another benefit. Multiplying equation 5 by 10' and
This answer approximates very closely the correct simplifying,
10-digit answer of -1.864330162.
This example demonstrates the simplicity of this -B¡xlO' = (-Bj.J >)-10~>)xlO'
method of logarithm generation. All that is required
is a multiplication (shift and add) and a test for 1.
-BjXlO' = -Bj.j -1 forsomej (6)
To implement this process using only three working
registers, a pseudo-quotient similar to the one gen
erated in the trigonometric algorithm is formed.5 Each Notice that the 10"' subtraction reduces to a simple
digit represents the number of successful multipli -1 regardless of the value of j. The formation of the
cations by a particular a¡. For the preceding example, initial -B0 is also easy since -B0 = -(A - 1) = 1 - A.
the pseudo-quotient would be This is formed by taking the 10's complement of M
(the original mantissa), creating 10 -- M. A right
shift divides this by 10 to give 1 - M/10 = 1 - A =
-B0. A final, almost incredible, benefit of the B¡
2 5 0 1 1
transformation is that the final remainder -Bm x
t t t t T 10' is in the exact form required to be the first term of
j = O j = 1 j = 2 j = 3 j = 4 the summation process of equation 4 without further
modification. The correct ln(a¡) constants are added
With -ln(r) = (r - 1) as the first term, the appro directly to -Bm x 10', shifting the sum right one
priate logarithms of (a¡) are then summed according digit after each pseudo-quotient digit to preserve
to the count in the pseudo-quotient digit correspond accuracy and restore the proper normalized form dis
ing to the proper a¡. The final sum is -ln(A). rupted by equation 6. The result is -ln(A).
At this point one more transformation is needed to Finally, the required ln(M) is easily found by sub
optimize this algorithm perfectly to the micropro tracting the computed result -ln(A) from ln(10).

31

© Copr. 1949-1998 Hewlett-Packard Co.


- (-ln(A)) = ln(M/10)
= ln(10-M/10) William E. Egbert
Bill Egbert is a project manager at
= ln(M) • HP's Corvallis, Oregon Division.
He produced this series of algorithm
Once ln(M) is computed, K-ln(lO) is added as pre articles as part of his work on the
viously discussed to form ln(xj. At this point log(x) HP-67 and HP-97 Programmable
can be generated by dividing ln(x) by ln(10). Calculators. He was project leader
for the HP-67 and did micro
programming for both calculators.
Summary More recently, he was project
In summary, the compulation of logarithmic func leader for the firmware develop
tions proceeds as follows: ment of the HP-19C and the
1. Find the logarithm of 10K using K -In (10). HP-29C. Bill received his BSEE
degree from Brigham Young Uni
2. Transform the input mantissa to the proper versity in 1 973 and his MSEE from
form required by — B0. Stanford University in 1976. He's been with HP since 1973.
3. Apply equation 6 repeatedly and form a pseudo- Born in Fallón, Nevada, he's married, has two small children,
quotient representing the number of successful and lives in Corvallis.
multiplications by each a¡.
4. Form -ln(A) by summing the ln(P¡) constants
corresponding to the pseudo-quotient digits with Slide Rule," Hewlett-Packard Journal, June 1972.
the remainder -Bm x 10' as the first term in the 2. D.S. Cochran, "Algorithms and Accuracy in the HP-35,"
series. Hewlett-Packard Journal, June 1972.
5. Find ln(x) or log(x) using simple arithmetic 3. D.W. Harms, "The New Accuracy: Making 23 = 8,"
Hewlett-Packard Journal, November 1976.
operations. 4. W.E. Egbert, "Personal Calculator Algorithms I:
6. Round and display the answer. Square Roots," Hewlett-Packard Journal, May 1977.
The calculator is now ready for another operation. S 5. W.E. Egbert, "Personal Calculator Algorithms II: Trigo
. nometric Functions," Hewlett-Packard Journal, June 1977.
References 6. W.E. Egbert, "Personal Calculator Algorithms III:
1. T.M. Whitney, F. Rode, and C.C. Tung, "The 'Power Inverse Trigonometric Functions," Hewlett-Packard Jour
ful Pocketful': An Electronic Calculator Challenges the nal, November 1977.

Bulk Rate
Hewlett-Packard Company, 1501 Page Mill U.S. Postage
Road, Palo Alto, California 94304 Paid
Hewlett-Packard
Company
HEWLETT-PACKARDJOURNAL
APRIL 1978 Volume 29 . Number 8

Technical information from the Laboratories of


Hewlett-Packard Company

Hewlett-Packard Central Mailing Department


Van Heuven Goedhartlaan 121
Amstelveen-1134 The Netherlands
Yokogawa-Hewlett-Packard Ltd Shibuya-Ku
Tokyo 151 Japan

Editorial Director • Howard L. Roberts


Managing Editor • Richard P. Dolan
Art Director, Photographer . Arvid A. Danielson
Illustrator . Susan E. Wright
Administrative Services, Typography • Anne S. LoPresti
European Production Manager • Dick Leeksma

To change peels address or delete your name from our mailing list please send us your old address label (it peels off).
CHANG Send changes to Hewlett-Packard Journal, 1501 Page Mill Road, Palo Alto, California 94304 U.S.A. Allow 60 days.

© Copr. 1949-1998 Hewlett-Packard Co.

You might also like