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