]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/dc/dc.1
Notable upstream pull request merges:
[FreeBSD/FreeBSD.git] / usr.bin / dc / dc.1
1 .\"     $OpenBSD: dc.1,v 1.27 2012/08/19 12:07:21 jmc Exp $
2 .\"
3 .\" Copyright (C) Caldera International Inc.  2001-2002.
4 .\" All rights reserved.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
8 .\" are met:
9 .\" 1. Redistributions of source code and documentation must retain the above
10 .\"    copyright notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\"    notice, this list of conditions and the following disclaimer in the
13 .\"    documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\"    must display the following acknowledgement:
16 .\"     This product includes software developed or owned by Caldera
17 .\"     International, Inc.
18 .\" 4. Neither the name of Caldera International, Inc. nor the names of other
19 .\"    contributors may be used to endorse or promote products derived from
20 .\"    this software without specific prior written permission.
21 .\"
22 .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23 .\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 .\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
27 .\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 .\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 .\" POSSIBILITY OF SUCH DAMAGE.
34 .\"
35 .Dd September 4, 2019
36 .Dt DC 1
37 .Os
38 .Sh NAME
39 .Nm dc
40 .Nd desk calculator
41 .Sh SYNOPSIS
42 .Nm
43 .Op Fl hxV
44 .Op Fl e Ar expression
45 .Op Fl f Ar filename
46 .Op Ar filename
47 .Sh DESCRIPTION
48 .Nm
49 is an arbitrary precision arithmetic package.
50 The overall structure of
51 .Nm
52 is
53 a stacking (reverse Polish) calculator i.e.\&
54 numbers are stored on a stack.
55 Adding a number pushes it onto the stack.
56 Arithmetic operations pop arguments off the stack
57 and push the results.
58 See also the
59 .Xr bc 1
60 utility, which is a preprocessor for
61 .Nm
62 providing infix notation and a C-like syntax
63 which implements functions and reasonable control
64 structures for programs.
65 The options are as follows:
66 .Bl -tag -width Ds
67 .It Fl e Ar expr , Fl Fl expression Ar expr
68 Evaluate
69 .Ar expression .
70 If multiple
71 .Fl e
72 options are specified, they will be processed in the order given.
73 .It Fl f Ar filename , Fl Fl file Ar filename
74 Process the content of the given file before further calculations are done.
75 If multiple
76 .Fl f
77 options are specified, they will be processed in the order given.
78 .It Fl h , Fl Fl help
79 Print short usage info.
80 .It Fl V , Fl Fl version
81 Print version info.
82 .It Fl x
83 Enable extended register mode.
84 This mode is used by
85 .Xr bc 1
86 to allow more than 256 registers.
87 See
88 .Sx Registers
89 for a more detailed description.
90 .El
91 .Pp
92 If neither
93 .Ar expression
94 nor
95 .Ar file
96 are specified on the command line,
97 .Nm
98 reads from the standard input.
99 Otherwise
100 .Ar expression
101 and
102 .Ar file
103 are processed and
104 .Nm
105 exits.
106 .Pp
107 Ordinarily,
108 .Nm
109 operates on decimal integers,
110 but one may specify an input base, output base,
111 and a number of fractional digits (scale) to be maintained.
112 Whitespace is ignored, except where it signals the end of a number,
113 end of a line or when a register name is expected.
114 The following constructions are recognized:
115 .Bl -tag -width "number"
116 .It Va number
117 The value of the number is pushed on the stack.
118 A number is an unbroken string of the digits 0\-9 and letters A\-F.
119 It may be preceded by an underscore
120 .Pq Sq _
121 to input a negative number.
122 A number may contain a single decimal point.
123 A number may also contain the characters A\-F, with the values 10\-15.
124 .It Cm "+ - / * % ~ ^"
125 The
126 top two values on the stack are added
127 (+),
128 subtracted
129 (\-),
130 multiplied (*),
131 divided (/),
132 remaindered (%),
133 divided and remaindered (~),
134 or exponentiated (^).
135 The two entries are popped off the stack;
136 the result is pushed on the stack in their place.
137 Any fractional part of an exponent is ignored.
138 .Pp
139 For addition, subtraction, and remainder, the scale of the result is the
140 maximum of scales of the operands.
141 For division the scale of the result is defined
142 by the scale set by the
143 .Ic k
144 operation.
145 For multiplication, the scale is defined by the expression
146 .Sy min(a+b,max(a,b,scale)) ,
147 where
148 .Sy a
149 and
150 .Sy b
151 are the scales of the operands, and
152 .Sy scale
153 is the scale defined by the
154 .Ic k
155 operation.
156 For exponentiation with a non-negative exponent, the scale of the result is
157 .Sy min(a*b,max(scale,a)) ,
158 where
159 .Sy a
160 is the scale of the base, and
161 .Sy b
162 is the
163 .Em value
164 of the exponent.
165 If the exponent is negative, the scale of the result is the scale
166 defined by the
167 .Ic k
168 operation.
169 .Pp
170 In the case of the division and modulus operator (~),
171 the resultant quotient is pushed first followed by the remainder.
172 This is a shorthand for the sequence:
173 .Bd -literal -offset indent -compact
174 x y / x y %
175 .Ed
176 The division and modulus operator is a non-portable extension.
177 .It Ic a
178 Pop the top value from the stack.
179 If that value is a number, compute the integer part of the number modulo 256.
180 If the result is zero, push an empty string.
181 Otherwise push a one character string by interpreting the computed value
182 as an
183 .Tn ASCII
184 character.
185 .Pp
186 If the top value is a string, push a string containing the first character
187 of the original string.
188 If the original string is empty, an empty string is pushed back.
189 The
190 .Ic a
191 operator is a non-portable extension.
192 .It Ic c
193 All values on the stack are popped.
194 .It Ic d
195 The top value on the stack is duplicated.
196 .It Ic e
197 Equivalent to
198 .Ic p ,
199 except that the output is written to the standard error stream.
200 .It Ic f
201 All values on the stack are printed, separated by newlines.
202 .It Ic G
203 The top two numbers are popped from the stack and compared.
204 A one is pushed if the top of the stack is equal to the second number
205 on the stack.
206 A zero is pushed otherwise.
207 This is a non-portable extension.
208 .It Ic I
209 Pushes the input base on the top of the stack.
210 .It Ic i
211 The top value on the stack is popped and used as the
212 base for further input.
213 The initial input base is 10.
214 .It Ic J
215 Pop the top value from the stack.
216 The recursion level is popped by that value and, following that,
217 the input is skipped until the first occurrence of the
218 .Ic M
219 operator.
220 The
221 .Ic J
222 operator is a non-portable extension, used by the
223 .Xr bc 1
224 command.
225 .It Ic K
226 The current scale factor is pushed onto the stack.
227 .It Ic k
228 The top of the stack is popped, and that value is used as
229 a non-negative scale factor:
230 the appropriate number of places
231 are printed on output,
232 and maintained during multiplication, division, and exponentiation.
233 The interaction of scale factor,
234 input base, and output base will be reasonable if all are changed
235 together.
236 .It Ic L Ns Ar x
237 Register
238 .Ar x
239 is treated as a stack and its top value is popped onto the main stack.
240 .It Ic l Ns Ar x
241 The
242 value in register
243 .Ar x
244 is pushed on the stack.
245 The register
246 .Ar x
247 is not altered.
248 Initially, all registers contain the value zero.
249 .It Ic M
250 Mark used by the
251 .Ic J
252 operator.
253 The
254 .Ic M
255 operator is a non-portable extensions, used by the
256 .Xr bc 1
257 command.
258 .It Ic N
259 The top of the stack is replaced by one if the top of the stack
260 is equal to zero.
261 If the top of the stack is unequal to zero, it is replaced by zero.
262 This is a non-portable extension.
263 .It Ic n
264 The top value on the stack is popped and printed without a newline.
265 This is a non-portable extension.
266 .It Ic O
267 Pushes the output base on the top of the stack.
268 .It Ic o
269 The top value on the stack is popped and used as the
270 base for further output.
271 The initial output base is 10.
272 .It Ic P
273 The top of the stack is popped.
274 If the top of the stack is a string, it is printed without a trailing newline.
275 If the top of the stack is a number, it is interpreted as a
276 base 256 number, and each digit of this base 256 number is printed as
277 an
278 .Tn ASCII
279 character, without a trailing newline.
280 .It Ic p
281 The top value on the stack is printed with a trailing newline.
282 The top value remains unchanged.
283 .It Ic Q
284 The top value on the stack is popped and the string execution level is popped
285 by that value.
286 .It Ic q
287 Exits the program.
288 If executing a string, the recursion level is
289 popped by two.
290 .It Ic R
291 The top of the stack is removed (popped).
292 This is a non-portable extension.
293 .It Ic r
294 The top two values on the stack are reversed (swapped).
295 This is a non-portable extension.
296 .It Ic S Ns Ar x
297 Register
298 .Ar x
299 is treated as a stack.
300 The top value of the main stack is popped and pushed on it.
301 .It Ic s Ns Ar x
302 The
303 top of the stack is popped and stored into
304 a register named
305 .Ar x .
306 .It Ic v
307 Replaces the top element on the stack by its square root.
308 The scale of the result is the maximum of the scale of the argument
309 and the current value of scale.
310 .It Ic X
311 Replaces the number on the top of the stack with its scale factor.
312 If the top of the stack is a string, replace it with the integer 0.
313 .It Ic x
314 Treats the top element of the stack as a character string
315 and executes it as a string of
316 .Nm
317 commands.
318 .It Ic Z
319 Replaces the number on the top of the stack with its length.
320 The length of a string is its number of characters.
321 The length of a number is its number of digits, not counting the minus sign
322 and decimal point.
323 .It Ic z
324 The stack level is pushed onto the stack.
325 .It Cm \&[ Ns ... Ns Cm \&]
326 Puts the bracketed
327 .Tn ASCII
328 string onto the top of the stack.
329 If the string includes brackets, these must be properly balanced.
330 The backslash character
331 .Pq Sq \e
332 may be used as an escape character, making it
333 possible to include unbalanced brackets in strings.
334 To include a backslash in a string, use a double backslash.
335 .It Xo
336 .Cm < Ns Va x
337 .Cm > Ns Va x
338 .Cm = Ns Va x
339 .Cm !< Ns Va x
340 .Cm !> Ns Va x
341 .Cm != Ns Va x
342 .Xc
343 The top two elements of the stack are popped and compared.
344 Register
345 .Ar x
346 is executed if they obey the stated
347 relation.
348 .It Xo
349 .Cm < Ns Va x Ns e Ns Va y
350 .Cm > Ns Va x Ns e Ns Va y
351 .Cm = Ns Va x Ns e Ns Va y
352 .Cm !< Ns Va x Ns e Ns Va y
353 .Cm !> Ns Va x Ns e Ns Va y
354 .Cm != Ns Va x Ns e Ns Va y
355 .Xc
356 These operations are variants of the comparison operations above.
357 The first register name is followed by the letter
358 .Sq e
359 and another register name.
360 Register
361 .Ar x
362 will be executed if the relation is true, and register
363 .Ar y
364 will be executed if the relation is false.
365 This is a non-portable extension.
366 .It Ic \&(
367 The top two numbers are popped from the stack and compared.
368 A one is pushed if the top of the stack is less than the second number
369 on the stack.
370 A zero is pushed otherwise.
371 This is a non-portable extension.
372 .It Ic {
373 The top two numbers are popped from the stack and compared.
374 A one is pushed if the top of stack is less than or equal to the
375 second number on the stack.
376 A zero is pushed otherwise.
377 This is a non-portable extension.
378 .It Ic \&!
379 Interprets the rest of the line as a
380 .Ux
381 command.
382 .It Ic \&?
383 A line of input is taken from the input source (usually the terminal)
384 and executed.
385 .It Ic \&: Ns Ar r
386 Pop two values from the stack.
387 The second value on the stack is stored into the array
388 .Ar r
389 indexed by the top of stack.
390 .It Ic \&; Ns Ar r
391 Pop a value from the stack.
392 The value is used as an index into register
393 .Ar r .
394 The value in this register is pushed onto the stack.
395 .Pp
396 Array elements initially have the value zero.
397 Each level of a stacked register has its own array associated with
398 it.
399 The command sequence
400 .Bd -literal -offset indent
401 [first] 0:a [dummy] Sa [second] 0:a 0;a p La 0;a p
402 .Ed
403 .Pp
404 will print
405 .Bd -literal -offset indent
406 second
407 first
408 .Ed
409 .Pp
410 since the string
411 .Ql second
412 is written in an array that is later popped, to reveal the array that
413 stored
414 .Ql first .
415 .It Ic #
416 Skip the rest of the line.
417 This is a non-portable extension.
418 .El
419 .Ss Registers
420 Registers have a single character name
421 .Ar x ,
422 where
423 .Ar x
424 may be any character, including space, tab or any other special character.
425 If extended register mode is enabled using the
426 .Fl x
427 option and the register identifier
428 .Ar x
429 has the value 255, the next two characters are interpreted as a
430 two-byte register index.
431 The set of standard single character registers and the set of extended
432 registers do not overlap.
433 Extended register mode is a non-portable extension.
434 .Sh EXAMPLES
435 An example which prints the first ten values of
436 .Ic n! :
437 .Bd -literal -offset indent
438 [la1+dsa*pla10>y]sy
439 0sa1
440 lyx
441 .Ed
442 .Pp
443 Independent of the current input base, the command
444 .Bd -literal -offset indent
445 Ai
446 .Ed
447 .Pp
448 will reset the input base to decimal 10.
449 .Sh DIAGNOSTICS
450 .Bl -diag
451 .It %c (0%o) is unimplemented
452 an undefined operation was called.
453 .It stack empty
454 for not enough elements on the stack to do what was asked.
455 .It stack register '%c' (0%o) is empty
456 for an
457 .Ar L
458 operation from a stack register that is empty.
459 .It Runtime warning: non-zero scale in exponent
460 for a fractional part of an exponent that is being ignored.
461 .It divide by zero
462 for trying to divide by zero.
463 .It remainder by zero
464 for trying to take a remainder by zero.
465 .It square root of negative number
466 for trying to take the square root of a negative number.
467 .It index too big
468 for an array index that is larger than 2048.
469 .It negative index
470 for a negative array index.
471 .It "input base must be a number between 2 and 16"
472 for trying to set an illegal input base.
473 .It output base must be a number greater than 1
474 for trying to set an illegal output base.
475 .It scale must be a nonnegative number
476 for trying to set a negative or zero scale.
477 .It scale too large
478 for trying to set a scale that is too large.
479 A scale must be representable as a 32-bit unsigned number.
480 .It Q command argument exceeded string execution depth
481 for trying to pop the recursion level more than the current
482 recursion level.
483 .It Q command requires a number >= 1
484 for trying to pop an illegal number of recursion levels.
485 .It recursion too deep
486 for too many levels of nested execution.
487 .Pp
488 The recursion level is increased by one if the
489 .Ar x
490 or
491 .Ar ?\&
492 operation or one of the compare operations resulting in the execution
493 of register is executed.
494 As an exception, the recursion level is not increased if the operation
495 is executed as the last command of a string.
496 For example, the commands
497 .Bd -literal -offset indent
498 [lax]sa
499 1 lax
500 .Ed
501 .Pp
502 will execute an endless loop, while the commands
503 .Bd -literal -offset indent
504 [laxp]sa
505 1 lax
506 .Ed
507 .Pp
508 will terminate because of a too deep recursion level.
509 .It J command argument exceeded string execution depth
510 for trying to pop the recursion level more than the current
511 recursion level.
512 .It mark not found
513 for a failed scan for an occurrence of the
514 .Ic M
515 operator.
516 .El
517 .Sh SEE ALSO
518 .Xr bc 1
519 .Pp
520 .An -nosplit
521 .An L. L. Cherry ,
522 .An R. Morris
523 "DC \- An Interactive Desk Calculator"
524 .Pa /usr/share/doc/usd/05.dc/ .
525 .Sh STANDARDS
526 The arithmetic operations of the
527 .Nm
528 utility are expected to conform to the definition listed in the
529 .Xr bc 1
530 section of the
531 .St -p1003.2
532 specification.
533 .Sh HISTORY
534 The
535 .Nm
536 command first appeared in
537 .At v1 .
538 A complete rewrite of the
539 .Nm
540 command using the
541 .Xr bn 3
542 big number routines first appeared in
543 .Ox 3.5 .
544 .Sh AUTHORS
545 .An -nosplit
546 The original version of the
547 .Nm
548 command was written by
549 .An Robert Morris
550 and
551 .An Lorinda Cherry .
552 The current version of the
553 .Nm
554 utility was written by
555 .An Otto Moerbeek .