BSD 3 development
[unix-history] / usr / doc / dc
CommitLineData
857300aa
BJ
1.RP
2....TM 75-1271-8 39199 39199-11
3.TL
4DC \- An Interactive Desk Calculator
5.AU "MH 2C-524" 3878
6Robert Morris
7.AU
8Lorinda Cherry
9.AI
10.MH
11.AB
12DC is an interactive desk calculator program implemented
13on the
14.UX
15time-sharing system to do arbitrary-precision
16integer arithmetic.
17It has provision for manipulating scaled fixed-point numbers and
18for input and output in bases other than decimal.
19.PP
20The size of numbers that can be manipulated is limited
21only by available core storage.
22On typical implementations of
23.UX ,
24the size of numbers that
25can be handled varies from several hundred digits on the smallest
26systems to several thousand on the largest.
27.AE
28.PP
29.SH
30.ND
31.PP
32DC is an arbitrary precision arithmetic package implemented
33on the
34.UX
35time-sharing system
36in the form of an interactive desk calculator.
37It works like a stacking calculator using reverse Polish notation.
38Ordinarily DC operates on decimal integers, but one may
39specify an input base, output base, and a number of fractional
40digits to be maintained.
41.PP
42A language called BC [1] has been developed which accepts
43programs written in the familiar style of higher-level
44programming languages and compiles output which is
45interpreted by DC.
46Some of the commands described below were designed
47for the compiler interface and are not easy for a human user
48to manipulate.
49.PP
50Numbers that are typed into DC are put on a push-down
51stack.
52DC commands work by taking the top number or two
53off the stack, performing the desired operation, and pushing the result
54on the stack.
55If an argument is given,
56input is taken from that file until its end,
57then from the standard input.
58.SH
59SYNOPTIC DESCRIPTION
60.PP
61Here we describe the DC commands that are intended
62for use by people. The additional commands that are
63intended to be invoked by compiled output are
64described in the detailed description.
65.PP
66Any number of commands are permitted on a line.
67Blanks and new-line characters are ignored except within numbers
68and in places where a register name is expected.
69.PP
70The following constructions are recognized:
71.SH
72number
73.IP
74The value of the number is pushed onto the main stack.
75A number is an unbroken string of the digits 0-9
76and the capital letters A\-F which are treated as digits
77with values 10\-15 respectively.
78The number may be preceded by an underscore \*_ to input a
79negative number.
80Numbers may contain decimal points.
81.SH
82+ \- * % ^
83.IP
84The
85top two values on the stack are added
86(\fB+\fP),
87subtracted
88(\fB\-\fP),
89multiplied (\fB*\fP),
90divided (\fB/\fP),
91remaindered (\fB%\fP),
92or exponentiated (^).
93The two entries are popped off the stack;
94the result is pushed on the stack in their place.
95The result of a division is an integer truncated toward zero.
96See the detailed description below for the treatment of
97numbers with decimal points.
98An exponent must not have any digits after the decimal point.
99.SH
100s\fIx\fP
101.IP
102The
103top of the main stack is popped and stored into
104a register named \fIx\fP, where \fIx\fP may be any character.
105If
106the
107.ft B
108s
109.ft
110is capitalized,
111.ft I
112x
113.ft
114is treated as a stack and the value is pushed onto it.
115Any character, even blank or new-line, is a valid register name.
116.SH
117l\fIx\fP
118.IP
119The
120value in register
121.ft I
122x
123.ft
124is pushed onto the stack.
125The register
126.ft I
127x
128.ft
129is not altered.
130If the
131.ft B
132l
133.ft
134is capitalized,
135register
136.ft I
137x
138.ft
139is treated as a stack and its top value is popped onto the main stack.
140.LP
141All registers start with empty value which is treated as a zero
142by the command \fBl\fP and is treated as an error by the command \fBL\fP.
143.SH
144.SH
145d
146.IP
147The
148top value on the stack is duplicated.
149.SH
150p
151.IP
152The top value on the stack is printed.
153The top value remains unchanged.
154.SH
155f
156.IP
157All values on the stack and in registers are printed.
158.SH
159x
160.IP
161treats the top element of the stack as a character string,
162removes it from the stack, and
163executes it as a string of DC commands.
164.SH
165[ ... ]
166.IP
167puts the bracketed character string onto the top of the stack.
168.SH
169q
170.IP
171exits the program.
172If executing a string, the recursion level is
173popped by two.
174If
175.ft B
176q
177.ft
178is capitalized,
179the top value on the stack is popped and the string execution level is popped
180by that value.
181.SH
182<\fIx\fP >\fIx\fP =\fIx\fP !<\fIx\fP !>\fIx\fP !=\fIx\fP
183.IP
184The
185top two elements of the stack are popped and compared.
186Register
187.ft I
188x
189.ft
190is executed if they obey the stated
191relation.
192Exclamation point is negation.
193.SH
194v
195.IP
196replaces the top element on the stack by its square root.
197The square root of an integer is truncated to an integer.
198For the treatment of numbers with decimal points, see
199the detailed description below.
200.SH
201!
202.IP
203interprets the rest of the line as a
204.UX
205command.
206Control returns to DC when the
207.UX
208command terminates.
209.SH
210c
211.IP
212All values on the stack are popped; the stack becomes empty.
213.SH
214i
215.IP
216The top value on the stack is popped and used as the
217number radix for further input.
218If \fBi\fP is capitalized, the value of
219the input base is pushed onto the stack.
220No mechanism has been provided for the input of arbitrary
221numbers in bases less than 1 or greater than 16.
222.SH
223o
224.IP
225The top value on the stack is popped and used as the
226number radix for further output.
227If \fBo\fP is capitalized, the value of the output
228base is pushed onto the stack.
229.SH
230k
231.IP
232The top of the stack is popped, and that value is used as
233a scale factor
234that influences the number of decimal places
235that are maintained during multiplication, division, and exponentiation.
236The scale factor must be greater than or equal to zero and
237less than 100.
238If \fBk\fP is capitalized, the value of the scale factor
239is pushed onto the stack.
240.SH
241z
242.IP
243The value of the stack level is pushed onto the stack.
244.SH
245?
246.IP
247A line of input is taken from the input source (usually the console)
248and executed.
249.SH
250DETAILED DESCRIPTION
251.SH
252Internal Representation of Numbers
253.PP
254Numbers are stored internally using a dynamic storage allocator.
255Numbers are kept in the form of a string
256of digits to the base 100 stored one digit per byte
257(centennial digits).
258The string is stored with the low-order digit at the
259beginning of the string.
260For example, the representation of 157
261is 57,1.
262After any arithmetic operation on a number, care is taken
263that all digits are in the range 0\-99 and that
264the number has no leading zeros.
265The number zero is represented by the empty string.
266.PP
267Negative numbers are represented in the 100's complement
268notation, which is analogous to two's complement notation for binary
269numbers.
270The high order digit of a negative number is always \-1
271and all other digits are in the range 0\-99.
272The digit preceding the high order \-1 digit is never a 99.
273The representation of \-157 is 43,98,\-1.
274We shall call this the canonical form of a number.
275The advantage of this kind of representation of negative
276numbers is ease of addition. When addition is performed digit
277by digit, the result is formally correct. The result need only
278be modified, if necessary, to put it into canonical form.
279.PP
280Because the largest valid digit is 99 and the byte can
281hold numbers twice that large, addition can be carried out
282and the handling of carries done later when
283that is convenient, as it sometimes is.
284.PP
285An additional byte is stored with each number beyond
286the high order digit to indicate the number of
287assumed decimal digits after the decimal point. The representation
288of .001 is 1,\fI3\fP
289where the scale has been italicized to emphasize the fact that it
290is not the high order digit.
291The value of this extra byte is called the
292.ft B
293scale factor
294.ft
295of the number.
296.SH
297The Allocator
298.PP
299DC uses a dynamic string storage allocator
300for all of its internal storage.
301All reading and writing of numbers internally is done through
302the allocator.
303Associated with each string in the allocator is a four-word header containing pointers
304to the beginning of the string, the end of the string,
305the next place to write, and the next place to read.
306Communication between the allocator and DC
307is done via pointers to these headers.
308.PP
309The allocator initially has one large string on a list
310of free strings. All headers except the one pointing
311to this string are on a list of free headers.
312Requests for strings are made by size.
313The size of the string actually supplied is the next higher
314power of 2.
315When a request for a string is made, the allocator
316first checks the free list to see if there is
317a string of the desired size.
318If none is found, the allocator finds the next larger free string and splits it repeatedly until
319it has a string of the right size.
320Left-over strings are put on the free list.
321If there are no larger strings,
322the allocator tries to coalesce smaller free strings into
323larger ones.
324Since all strings are the result
325of splitting large strings,
326each string has a neighbor that is next to it in core
327and, if free, can be combined with it to make a string twice as long.
328This is an implementation of the `buddy system' of allocation
329described in [2].
330.PP
331Failing to find a string of the proper length after coalescing,
332the allocator asks the system for more space.
333The amount of space on the system is the only limitation
334on the size and number of strings in DC.
335If at any time in the process of trying to allocate a string, the allocator runs out of
336headers, it also asks the system for more space.
337.PP
338There are routines in the allocator for reading, writing, copying, rewinding,
339forward-spacing, and backspacing strings.
340All string manipulation is done using these routines.
341.PP
342The reading and writing routines
343increment the read pointer or write pointer so that
344the characters of a string are read or written in
345succession by a series of read or write calls.
346The write pointer is interpreted as the end of the
347information-containing portion of a string and a call
348to read beyond that point returns an end-of-string indication.
349An attempt to write beyond the end of a string
350causes the allocator to
351allocate a larger space and then copy
352the old string into the larger block.
353.SH
354Internal Arithmetic
355.PP
356All arithmetic operations are done on integers.
357The operands (or operand) needed for the operation are popped
358from the main stack and their scale factors stripped off.
359Zeros are added or digits removed as necessary to get
360a properly scaled result from the internal arithmetic routine.
361For example, if the scale of the operands is different and decimal
362alignment is required, as it is for
363addition, zeros are appended to the operand with the smaller
364scale.
365After performing the required arithmetic operation,
366the proper scale factor is appended to the end of the number before
367it is pushed on the stack.
368.PP
369A register called \fBscale\fP plays a part
370in the results of most arithmetic operations.
371\fBscale\fP is the bound on the number of decimal places retained in
372arithmetic computations.
373\fBscale\fP may be set to the number on the top of the stack
374truncated to an integer with the \fBk\fP command.
375\fBK\fP may be used to push the value of \fBscale\fP on the stack.
376\fBscale\fP must be greater than or equal to 0 and less than 100.
377The descriptions of the individual arithmetic operations will
378include the exact effect of \fBscale\fP on the computations.
379.SH
380Addition and Subtraction
381.PP
382The scales of the two numbers are compared and trailing
383zeros are supplied to the number with the lower scale to give both
384numbers the same scale. The number with the smaller scale is
385multiplied by 10 if the difference of the scales is odd.
386The scale of the result is then set to the larger of the scales
387of the two operands.
388.PP
389Subtraction is performed by negating the number
390to be subtracted and proceeding as in addition.
391.PP
392Finally, the addition is performed digit by digit from the
393low order end of the number. The carries are propagated
394in the usual way.
395The resulting number is brought into canonical form, which may
396require stripping of leading zeros, or for negative numbers
397replacing the high-order configuration 99,\-1 by the digit \-1.
398In any case, digits which are not in the range 0\-99 must
399be brought into that range, propagating any carries or borrows
400that result.
401.SH
402Multiplication
403.PP
404The scales are removed from the two operands and saved.
405The operands are both made positive.
406Then multiplication is performed in
407a digit by digit manner that exactly mimics the hand method
408of multiplying.
409The first number is multiplied by each digit of the second
410number, beginning with its low order digit. The intermediate
411products are accumulated into a partial sum which becomes the
412final product.
413The product is put into the canonical form and its sign is
414computed from the signs of the original operands.
415.PP
416The scale of the result is set equal to the sum
417of the scales of the two operands.
418If that scale is larger than the internal register
419.ft B
420scale
421.ft
422and also larger than both of the scales of the two operands,
423then the scale of the result is set equal to the largest
424of these three last quantities.
425.SH
426Division
427.PP
428The scales are removed from the two operands.
429Zeros are appended or digits removed from the dividend to make
430the scale of the result of the integer division equal to
431the internal quantity
432\fBscale\fP.
433The signs are removed and saved.
434.PP
435Division is performed much as it would be done by hand.
436The difference of the lengths of the two numbers
437is computed.
438If the divisor is longer than the dividend,
439zero is returned.
440Otherwise the top digit of the divisor is divided into the top
441two digits of the dividend.
442The result is used as the first (high-order) digit of the
443quotient.
444It may turn out be one unit too low, but if it is, the next
445trial quotient will be larger than 99 and this will be
446adjusted at the end of the process.
447The trial digit is multiplied by the divisor and the result subtracted
448from the dividend and the process is repeated to get
449additional quotient digits until the remaining
450dividend is smaller than the divisor.
451At the end, the digits of the quotient are put into
452the canonical form, with propagation of carry as needed.
453The sign is set from the sign of the operands.
454.SH
455Remainder
456.PP
457The division routine is called and division is performed
458exactly as described. The quantity returned is the remains of the
459dividend at the end of the divide process.
460Since division truncates toward zero, remainders have the same
461sign as the dividend.
462The scale of the remainder is set to
463the maximum of the scale of the dividend and
464the scale of the quotient plus the scale of the divisor.
465.SH
466Square Root
467.PP
468The scale is stripped from the operand.
469Zeros are added if necessary to make the
470integer result have a scale that is the larger of
471the internal quantity
472\fBscale\fP
473and the scale of the operand.
474.PP
475The method used to compute sqrt(y) is Newton's method
476with successive approximations by the rule
477.EQ
478x sub {n+1} ~=~ half ( x sub n + y over x sub n )
479.EN
480The initial guess is found by taking the integer square root
481of the top two digits.
482.SH
483Exponentiation
484.PP
485Only exponents with zero scale factor are handled. If the exponent is
486zero, then the result is 1. If the exponent is negative, then
487it is made positive and the base is divided into one. The scale
488of the base is removed.
489.PP
490The integer exponent is viewed as a binary number.
491The base is repeatedly squared and the result is
492obtained as a product of those powers of the base that
493correspond to the positions of the one-bits in the binary
494representation of the exponent.
495Enough digits of the result
496are removed to make the scale of the result the same as if the
497indicated multiplication had been performed.
498.SH
499Input Conversion and Base
500.PP
501Numbers are converted to the internal representation as they are read
502in.
503The scale stored with a number is simply the number of fractional digits input.
504Negative numbers are indicated by preceding the number with a \fB\_\fP.
505The hexadecimal digits A\-F correspond to the numbers 10\-15 regardless of input base.
506The \fBi\fP command can be used to change the base of the input numbers.
507This command pops the stack, truncates the resulting number to an integer,
508and uses it as the input base for all further input.
509The input base is initialized to 10 but may, for example be changed to
5108 or 16 to do octal or hexadecimal to decimal conversions.
511The command \fBI\fP will push the value of the input base on the stack.
512.SH
513Output Commands
514.PP
515The command \fBp\fP causes the top of the stack to be printed.
516It does not remove the top of the stack.
517All of the stack and internal registers can be output
518by typing the command \fBf\fP.
519The \fBo\fP command can be used to change the output base.
520This command uses the top of the stack, truncated to an integer as
521the base for all further output.
522The output base in initialized to 10.
523It will work correctly for any base.
524The command \fBO\fP pushes the value of the output base on the stack.
525.SH
526Output Format and Base
527.PP
528The input and output bases only affect
529the interpretation of numbers on input and output; they have no
530effect on arithmetic computations.
531Large numbers are output with 70 characters per line;
532a \\ indicates a continued line.
533All choices of input and output bases work correctly, although not all are
534useful.
535A particularly useful output base is 100000, which has the effect of
536grouping digits in fives.
537Bases of 8 and 16 can be used for decimal-octal or decimal-hexadecimal
538conversions.
539.SH
540Internal Registers
541.PP
542Numbers or strings may be stored in internal registers or loaded on the stack
543from registers with the commands \fBs\fP and \fBl\fP.
544The command \fBs\fIx\fR pops the top of the stack and
545stores the result in register \fBx\fP.
546\fIx\fP can be any character.
547\fBl\fIx\fR puts the contents of register \fBx\fP on the top of the stack.
548The \fBl\fP command has no effect on the contents of register \fIx\fP.
549The \fBs\fP command, however, is destructive.
550.SH
551Stack Commands
552.PP
553The command \fBc\fP clears the stack.
554The command \fBd\fP pushes a duplicate of the number on the top of the stack
555on the stack.
556The command \fBz\fP pushes the stack size on the stack.
557The command \fBX\fP replaces the number on the top of the stack
558with its scale factor.
559The command \fBZ\fP replaces the top of the stack
560with its length.
561.SH
562Subroutine Definitions and Calls
563.PP
564Enclosing a string in \fB[]\fP pushes the ascii string on the stack.
565The \fBq\fP command quits or in executing a string, pops the recursion levels by two.
566.SH
567Internal Registers \- Programming DC
568.PP
569The load and store
570commands together with \fB[]\fP to store strings, \fBx\fP to execute
571and the testing commands `<', `>', `=', `!<', `!>', `!=' can be used to program
572DC.
573The \fBx\fP command assumes the top of the stack is an string of DC commands
574and executes it.
575The testing commands compare the top two elements on the stack and if the relation holds, execute the register
576that follows the relation.
577For example, to print the numbers 0-9,
578.DS
579[lip1+ si li10>a]sa
5800si lax
581.DE
582.SH
583Push-Down Registers and Arrays
584.PP
585These commands were designed for used by a compiler, not by
586people.
587They involve push-down registers and arrays.
588In addition to the stack that commands work on, DC can be thought
589of as having individual stacks for each register.
590These registers are operated on by the commands \fBS\fP and \fBL\fP.
591\fBS\fIx\fR pushes the top value of the main stack onto the stack for
592the register \fIx\fP.
593\fBL\fIx\fR pops the stack for register \fIx\fP and puts the result on the main
594stack.
595The commands \fBs\fP and \fBl\fP also work on registers but not as push-down
596stacks.
597\fBl\fP doesn't effect the top of the
598register stack, and \fBs\fP destroys what was there before.
599.PP
600The commands to work on arrays are \fB:\fP and \fB;\fP.
601\fB:\fIx\fR pops the stack and uses this value as an index into
602the array \fIx\fP.
603The next element on the stack is stored at this index in \fIx\fP.
604An index must be greater than or equal to 0 and
605less than 2048.
606\fB;\fIx\fR is the command to load the main stack from the array \fIx\fP.
607The value on the top of the stack is the index
608into the array \fIx\fP of the value to be loaded.
609.SH
610Miscellaneous Commands
611.PP
612The command \fB!\fP interprets the rest of the line as a
613.UX
614 command and passes
615it to
616.UX
617to execute.
618One other compiler command is \fBQ\fP.
619This command uses the top of the stack as the number of levels of recursion to skip.
620.SH
621DESIGN CHOICES
622.PP
623The real reason for the use of a dynamic storage allocator was
624that a general purpose program could be (and in fact has been)
625used for a variety of other tasks.
626The allocator has some value for input and for compiling (i.e.
627the bracket [...] commands) where it cannot be known in advance
628how long a string will be.
629The result was that at a modest
630cost in execution time, all considerations of string allocation
631and sizes of strings were removed from the remainder of the program
632and debugging was made easier. The allocation method
633used wastes approximately 25% of available space.
634.PP
635The choice of 100 as a base for internal arithmetic
636seemingly has no compelling advantage. Yet the base cannot
637exceed 127 because of hardware limitations and at the cost
638of 5% in space, debugging was made a great deal easier and
639decimal output was made much faster.
640.PP
641The reason for a stack-type arithmetic design was
642to permit all DC commands from addition to subroutine execution
643to be implemented in essentially the same way. The result
644was a considerable degree of logical separation of the final
645program into modules with very little communication between
646modules.
647.PP
648The rationale for the lack of interaction between the scale and the bases
649was to provide an understandable means of proceeding after
650a change of base or scale when numbers had already been entered.
651An earlier implementation which had global notions of
652scale and base did not work out well.
653If the value of
654.ft B
655scale
656.ft
657were to be interpreted in the current
658input or output base,
659then a change of base or scale in the midst of a
660computation would cause great confusion in the interpretation
661of the results.
662The current scheme has the advantage that the value of
663the input and output bases
664are only used for input and output, respectively, and they
665are ignored in all other operations.
666The value of
667scale
668is not used for any essential purpose by any part of the program
669and it is used only to prevent the number of
670decimal places resulting from the arithmetic operations from
671growing beyond all bounds.
672.PP
673The design rationale for the choices for the scales of
674the results of arithmetic were that in no case should
675any significant digits be thrown away if, on appearances, the
676user actually wanted them. Thus, if the user wants
677to add the numbers 1.5 and 3.517, it seemed reasonable to give
678him the result 5.017 without requiring him to unnecessarily
679specify his rather obvious requirements for precision.
680.PP
681On the other hand, multiplication and exponentiation produce
682results with many more digits than their operands and it
683seemed reasonable to give as a minimum the number of decimal
684places in the operands but not to give more than that
685number of digits
686unless the user asked for them by specifying a value for \fBscale\fP.
687Square root can be handled in just the same way as multiplication.
688The operation of division gives arbitrarily many decimal places
689and there is simply no way to guess how many places the user
690wants.
691In this case only, the user must
692specify a \fBscale\fP to get any decimal places at all.
693.PP
694The scale of remainder was chosen to make it possible
695to recreate the dividend from the quotient and remainder.
696This is easy to implement; no digits are thrown away.
697.SH
698References
699.IP [1]
700L. L. Cherry, R. Morris,
701.ft I
702BC \- An Arbitrary Precision Desk-Calculator Language.
703.ft
704.IP [2]
705K. C. Knowlton,
706.ft I
707A Fast Storage Allocator,
708.ft
709Comm. ACM \fB8\fP, pp. 623-625 (Oct. 1965).