1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
'\" te
.\" Copyright 1989 AT&T Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH DC 1 "Aug 29, 2003"
.SH NAME
dc \- desk calculator
.SH SYNOPSIS
.LP
.nf
\fB/usr/bin/dc\fR [\fIfilename\fR]
.fi
.LP
.nf
\fB/usr/xpg6/bin/dc\fR [\fIfilename\fR]
.fi
.SH DESCRIPTION
.sp
.LP
\fBdc\fR is an arbitrary precision arithmetic package. Ordinarily it operates
on decimal integers, but one may specify an input base, output base, and a
number of fractional digits to be maintained. The overall structure of \fBdc\fR
is a stacking (reverse Polish) calculator. If an argument is given, input is
taken from that file until its end, then from the standard input.
.sp
.LP
\fBbc\fR is a preprocessor for \fBdc\fR that provides infix notation and a
C-like syntax that implements functions. \fBbc\fR also provides reasonable
control structures for programs. See \fBbc\fR(1).
.SH USAGE
.SS "/usr/bin/dc, /usr/xpg6/bin/dc"
.sp
.LP
The following constructions are recognized under both \fB/usr/bin/dc\fR and
\fB/usr/xpg6/bin/dc\fR:
.sp
.ne 2
.na
\fB\fInumber\fR\fR
.ad
.RS 15n
The value of the number is pushed on the stack. A number is an unbroken string
of the digits 0\(mi9. It may be preceded by an underscore (\fB_\fR) to input a
negative number. Numbers may contain decimal points.
.RE
.sp
.ne 2
.na
\fB\fBs\fR\fIx\fR\fR
.ad
.RS 15n
The top of the stack is popped and stored into a register named \fIx\fR, where
\fIx\fR may be any character. If the \fBs\fR is capitalized, \fIx\fR is
treated as a stack and the value is pushed on it.
.RE
.sp
.ne 2
.na
\fB\fBl\fR\fIx\fR\fR
.ad
.RS 15n
The value in register \fIx\fR is pushed on the stack. The register \fIx\fR is
not altered. All registers start with zero value. If the \fBl\fR is
capitalized, register \fIx\fR is treated as a stack and its top value is popped
onto the main stack.
.RE
.sp
.ne 2
.na
\fB\fBd\fR\fR
.ad
.RS 15n
The top value on the stack is duplicated.
.RE
.sp
.ne 2
.na
\fB\fBp\fR\fR
.ad
.RS 15n
The top value on the stack is printed. The top value remains unchanged.
.RE
.sp
.ne 2
.na
\fB\fBP\fR\fR
.ad
.RS 15n
Interprets the top of the stack as an ASCII string, removes it, and prints it.
.RE
.sp
.ne 2
.na
\fB\fBf\fR\fR
.ad
.RS 15n
All values on the stack are printed.
.RE
.sp
.ne 2
.na
\fB\fBq\fR\fR
.ad
.RS 15n
Exits the program. If executing a string, the recursion level is popped by two.
.RE
.sp
.ne 2
.na
\fB\fBQ\fR\fR
.ad
.RS 15n
Exits the program. The top value on the stack is popped and the string
execution level is popped by that value.
.RE
.sp
.ne 2
.na
\fB\fBx\fR\fR
.ad
.RS 15n
Treats the top element of the stack as a character string and executes it as a
string of \fBdc\fR commands.
.RE
.sp
.ne 2
.na
\fB\fBX\fR\fR
.ad
.RS 15n
Replaces the number on the top of the stack with its scale factor.
.RE
.sp
.ne 2
.na
\fB\fB[ ... ]\fR\fR
.ad
.RS 15n
Puts the bracketed ASCII string onto the top of the stack.
.RE
.sp
.ne 2
.na
\fB\fB<\fR\fIx \fR \fB>\fR\fIx \fR \fB=\fR\fIx \fR\fR
.ad
.RS 15n
The top two elements of the stack are popped and compared. Register \fIx\fR is
evaluated if they obey the stated relation.
.RE
.sp
.ne 2
.na
\fB\fBv\fR\fR
.ad
.RS 15n
Replaces the top element on the stack by its square root. Any existing
fractional part of the argument is taken into account, but otherwise the scale
factor is ignored.
.RE
.sp
.ne 2
.na
\fB\fB!\fR\fR
.ad
.RS 15n
Interprets the rest of the line as a shell command.
.RE
.sp
.ne 2
.na
\fB\fBc\fR\fR
.ad
.RS 15n
All values on the stack are popped.
.RE
.sp
.ne 2
.na
\fB\fBi\fR\fR
.ad
.RS 15n
The top value on the stack is popped and used as the number radix for further
input.
.RE
.sp
.ne 2
.na
\fB\fBI\fR\fR
.ad
.RS 15n
Pushes the input base on the top of the stack.
.RE
.sp
.ne 2
.na
\fB\fBo\fR\fR
.ad
.RS 15n
The top value on the stack is popped and used as the number radix for further
output.
.RE
.sp
.ne 2
.na
\fB\fBO\fR\fR
.ad
.RS 15n
Pushes the output base on the top of the stack.
.RE
.sp
.ne 2
.na
\fB\fBk\fR\fR
.ad
.RS 15n
The top of the stack is popped, and that value is used as a non-negative scale
factor: the appropriate number of places are printed on output, and maintained
during multiplication, division, and exponentiation. The interaction of scale
factor, input base, and output base will be reasonable if all are changed
together.
.RE
.sp
.ne 2
.na
\fB\fBK\fR\fR
.ad
.RS 15n
Pushes the current scale factor on the top of the stack.
.RE
.sp
.ne 2
.na
\fB\fBz\fR\fR
.ad
.RS 15n
The stack level is pushed onto the stack.
.RE
.sp
.ne 2
.na
\fB\fBZ\fR\fR
.ad
.RS 15n
Replaces the number on the top of the stack with its length.
.RE
.sp
.ne 2
.na
\fB\fB?\fR\fR
.ad
.RS 15n
A line of input is taken from the input source (usually the terminal) and
executed.
.RE
.sp
.ne 2
.na
\fB\fBY\fR\fR
.ad
.RS 15n
Displays \fBdc\fR debugging information.
.RE
.sp
.ne 2
.na
\fB\fB; :\fR\fR
.ad
.RS 15n
Used by \fBbc\fR(1) for array operations.
.RE
.SS "/usr/bin/dc"
.sp
.LP
The following construction is recognized under \fB/usr/bin/dc\fR, using the
scale of whatever the result is.
.sp
.ne 2
.na
\fB\fB+ \(mi / * % ^\fR\fR
.ad
.RS 18n
The top two values on the stack are added (\fB+\fR), subtracted (\fB\(mi\fR),
multiplied (\fB*\fR), divided (\fB/\fR), remaindered (\fB%\fR), or
exponentiated (\fB^\fR). The two entries are popped off the stack; the result
is pushed on the stack in their place. Any fractional part of an exponent is
ignored.
.RE
.SS "/usr/xpg6/bin/dc"
.sp
.LP
The following construction is recognized under \fB/usr/xpg6/bin/dc\fR. The
results of division are forced to be a scale of 20.
.sp
.ne 2
.na
\fB\fB+ \(mi / * % ^\fR\fR
.ad
.RS 18n
The top two values on the stack are added (\fB+\fR), subtracted (\fB\(mi\fR),
multiplied (\fB*\fR), divided (\fB/\fR), remaindered (\fB%\fR), or
exponentiated (\fB^\fR). The two entries are popped off the stack. The result
is pushed on the stack in their place. Any fractional part of an exponent is
ignored.
.sp
Ensures that the scale set prior to division is the scale of the result.
.RE
.SH EXAMPLES
.LP
\fBExample 1 \fRPrinting the first ten values of n!
.sp
.LP
This example prints the first ten values of n!:
.sp
.in +2
.nf
[la1+dsa*pla10>y]sy
0sa1
lyx
.fi
.in -2
.sp
.SH SEE ALSO
.sp
.LP
.BR bc (1),
.BR attributes (7)
.SH DIAGNOSTICS
.sp
.ne 2
.na
\fB\fIx\fR \fBis unimplemented\fR\fR
.ad
.RS 29n
\fIx\fR is an octal number.
.RE
.sp
.ne 2
.na
\fB\fBout of space\fR\fR
.ad
.RS 29n
The free list is exhausted (too many digits).
.RE
.sp
.ne 2
.na
\fB\fBout of stack space\fR\fR
.ad
.RS 29n
Too many pushes onto the stack (stack overflow).
.RE
.sp
.ne 2
.na
\fB\fBempty stack\fR\fR
.ad
.RS 29n
Too many pops from the stack (stack underflow).
.RE
.sp
.ne 2
.na
\fB\fBnesting depth\fR\fR
.ad
.RS 29n
Too many levels of nested execution.
.RE
.sp
.ne 2
.na
\fB\fBdivide by 0\fR\fR
.ad
.RS 29n
Division by zero.
.RE
.sp
.ne 2
.na
\fB\fBsqrt of neg number\fR\fR
.ad
.RS 29n
Square root of a negative number is not defined (no imaginary numbers).
.RE
.sp
.ne 2
.na
\fB\fBexp not an integer\fR\fR
.ad
.RS 29n
\fBdc\fR only processes integer exponentiation.
.RE
.sp
.ne 2
.na
\fB\fBexp too big\fR\fR
.ad
.RS 29n
The largest exponent allowed is 999.
.RE
.sp
.ne 2
.na
\fB\fBinput base is too large\fR\fR
.ad
.RS 29n
The input base x: 2<= x <= 16.
.RE
.sp
.ne 2
.na
\fB\fBinput base is too small\fR\fR
.ad
.RS 29n
The input base x: 2<= x <= 16.
.RE
.sp
.ne 2
.na
\fB\fBoutput base is too large\fR\fR
.ad
.RS 29n
The output base must be no larger than \fBBC_BASE_MAX\fR.
.RE
.sp
.ne 2
.na
\fB\fBinvalid scale factor\fR\fR
.ad
.RS 29n
Scale factor cannot be less than 1.
.RE
.sp
.ne 2
.na
\fB\fBscale factor is too large\fR\fR
.ad
.RS 29n
A scale factor cannot be larger than \fBBC_SCALE_MAX\fR.
.RE
.sp
.ne 2
.na
\fB\fBsymbol table overflow\fR\fR
.ad
.RS 29n
Too many variables have been specified.
.RE
.sp
.ne 2
.na
\fB\fBinvalid index\fR\fR
.ad
.RS 29n
Index cannot be less than 1.
.RE
.sp
.ne 2
.na
\fB\fBindex is too large\fR\fR
.ad
.RS 29n
An index cannot be larger than \fBBC_DIM_MAX\fR.
.RE
|