]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/manuals/dc/N.1
Upgrade to version 3.1.4
[FreeBSD/FreeBSD.git] / contrib / bc / manuals / dc / N.1
1 .\"
2 .\" SPDX-License-Identifier: BSD-2-Clause
3 .\"
4 .\" Copyright (c) 2018-2020 Gavin D. Howard and contributors.
5 .\"
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions are met:
8 .\"
9 .\" * Redistributions of source code must retain the above copyright notice,
10 .\"   this list of conditions and the following disclaimer.
11 .\"
12 .\" * Redistributions in binary form must reproduce the above copyright notice,
13 .\"   this list of conditions and the following disclaimer in the documentation
14 .\"   and/or other materials provided with the distribution.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 .\" POSSIBILITY OF SUCH DAMAGE.
27 .\"
28 .TH "DC" "1" "July 2020" "Gavin D. Howard" "General Commands Manual"
29 .SH Name
30 .PP
31 dc \- arbitrary\-precision reverse\-Polish notation calculator
32 .SH SYNOPSIS
33 .PP
34 \f[B]dc\f[] [\f[B]\-hiPvVx\f[]] [\f[B]\-\-version\f[]]
35 [\f[B]\-\-help\f[]] [\f[B]\-\-interactive\f[]] [\f[B]\-\-no\-prompt\f[]]
36 [\f[B]\-\-extended\-register\f[]] [\f[B]\-e\f[] \f[I]expr\f[]]
37 [\f[B]\-\-expression\f[]=\f[I]expr\f[]...] [\f[B]\-f\f[]
38 \f[I]file\f[]...] [\f[B]\-file\f[]=\f[I]file\f[]...] [\f[I]file\f[]...]
39 .SH DESCRIPTION
40 .PP
41 dc(1) is an arbitrary\-precision calculator.
42 It uses a stack (reverse Polish notation) to store numbers and results
43 of computations.
44 Arithmetic operations pop arguments off of the stack and push the
45 results.
46 .PP
47 If no files are given on the command\-line as extra arguments (i.e., not
48 as \f[B]\-f\f[] or \f[B]\-\-file\f[] arguments), then dc(1) reads from
49 \f[B]stdin\f[].
50 Otherwise, those files are processed, and dc(1) will then exit.
51 .PP
52 This is different from the dc(1) on OpenBSD and possibly other dc(1)
53 implementations, where \f[B]\-e\f[] (\f[B]\-\-expression\f[]) and
54 \f[B]\-f\f[] (\f[B]\-\-file\f[]) arguments cause dc(1) to execute them
55 and exit.
56 The reason for this is that this dc(1) allows users to set arguments in
57 the environment variable \f[B]DC_ENV_ARGS\f[] (see the \f[B]ENVIRONMENT
58 VARIABLES\f[] section).
59 Any expressions given on the command\-line should be used to set up a
60 standard environment.
61 For example, if a user wants the \f[B]scale\f[] always set to
62 \f[B]10\f[], they can set \f[B]DC_ENV_ARGS\f[] to \f[B]\-e 10k\f[], and
63 this dc(1) will always start with a \f[B]scale\f[] of \f[B]10\f[].
64 .PP
65 If users want to have dc(1) exit after processing all input from
66 \f[B]\-e\f[] and \f[B]\-f\f[] arguments (and their equivalents), then
67 they can just simply add \f[B]\-e q\f[] as the last command\-line
68 argument or define the environment variable \f[B]DC_EXPR_EXIT\f[].
69 .SH OPTIONS
70 .PP
71 The following are the options that dc(1) accepts.
72 .TP
73 .B \f[B]\-h\f[], \f[B]\-\-help\f[]
74 Prints a usage message and quits.
75 .RS
76 .RE
77 .TP
78 .B \f[B]\-v\f[], \f[B]\-V\f[], \f[B]\-\-version\f[]
79 Print the version information (copyright header) and exit.
80 .RS
81 .RE
82 .TP
83 .B \f[B]\-i\f[], \f[B]\-\-interactive\f[]
84 Forces interactive mode.
85 (See the \f[B]INTERACTIVE MODE\f[] section.)
86 .RS
87 .PP
88 This is a \f[B]non\-portable extension\f[].
89 .RE
90 .TP
91 .B \f[B]\-P\f[], \f[B]\-\-no\-prompt\f[]
92 Disables the prompt in TTY mode.
93 (The prompt is only enabled in TTY mode.
94 See the \f[B]TTY MODE\f[] section) This is mostly for those users that
95 do not want a prompt or are not used to having them in dc(1).
96 Most of those users would want to put this option in
97 \f[B]DC_ENV_ARGS\f[].
98 .RS
99 .PP
100 This is a \f[B]non\-portable extension\f[].
101 .RE
102 .TP
103 .B \f[B]\-x\f[] \f[B]\-\-extended\-register\f[]
104 Enables extended register mode.
105 See the \f[I]Extended Register Mode\f[] subsection of the
106 \f[B]REGISTERS\f[] section for more information.
107 .RS
108 .PP
109 This is a \f[B]non\-portable extension\f[].
110 .RE
111 .TP
112 .B \f[B]\-e\f[] \f[I]expr\f[], \f[B]\-\-expression\f[]=\f[I]expr\f[]
113 Evaluates \f[I]expr\f[].
114 If multiple expressions are given, they are evaluated in order.
115 If files are given as well (see below), the expressions and files are
116 evaluated in the order given.
117 This means that if a file is given before an expression, the file is
118 read in and evaluated first.
119 .RS
120 .PP
121 After processing all expressions and files, dc(1) will exit, unless
122 \f[B]\-\f[] (\f[B]stdin\f[]) was given as an argument at least once to
123 \f[B]\-f\f[] or \f[B]\-\-file\f[].
124 .PP
125 This is a \f[B]non\-portable extension\f[].
126 .RE
127 .TP
128 .B \f[B]\-f\f[] \f[I]file\f[], \f[B]\-\-file\f[]=\f[I]file\f[]
129 Reads in \f[I]file\f[] and evaluates it, line by line, as though it were
130 read through \f[B]stdin\f[].
131 If expressions are also given (see above), the expressions are evaluated
132 in the order given.
133 .RS
134 .PP
135 After processing all expressions and files, dc(1) will exit, unless
136 \f[B]\-\f[] (\f[B]stdin\f[]) was given as an argument at least once to
137 \f[B]\-f\f[] or \f[B]\-\-file\f[].
138 However, if any other \f[B]\-e\f[], \f[B]\-\-expression\f[],
139 \f[B]\-f\f[], or \f[B]\-\-file\f[] arguments are given after that, bc(1)
140 will give a fatal error and exit.
141 .PP
142 This is a \f[B]non\-portable extension\f[].
143 .RE
144 .PP
145 All long options are \f[B]non\-portable extensions\f[].
146 .SH STDOUT
147 .PP
148 Any non\-error output is written to \f[B]stdout\f[].
149 .PP
150 \f[B]Note\f[]: Unlike other dc(1) implementations, this dc(1) will issue
151 a fatal error (see the \f[B]EXIT STATUS\f[] section) if it cannot write
152 to \f[B]stdout\f[], so if \f[B]stdout\f[] is closed, as in \f[B]dc
153 >&\-\f[], it will quit with an error.
154 This is done so that dc(1) can report problems when \f[B]stdout\f[] is
155 redirected to a file.
156 .PP
157 If there are scripts that depend on the behavior of other dc(1)
158 implementations, it is recommended that those scripts be changed to
159 redirect \f[B]stdout\f[] to \f[B]/dev/null\f[].
160 .SH STDERR
161 .PP
162 Any error output is written to \f[B]stderr\f[].
163 .PP
164 \f[B]Note\f[]: Unlike other dc(1) implementations, this dc(1) will issue
165 a fatal error (see the \f[B]EXIT STATUS\f[] section) if it cannot write
166 to \f[B]stderr\f[], so if \f[B]stderr\f[] is closed, as in \f[B]dc
167 2>&\-\f[], it will quit with an error.
168 This is done so that dc(1) can exit with an error code when
169 \f[B]stderr\f[] is redirected to a file.
170 .PP
171 If there are scripts that depend on the behavior of other dc(1)
172 implementations, it is recommended that those scripts be changed to
173 redirect \f[B]stderr\f[] to \f[B]/dev/null\f[].
174 .SH SYNTAX
175 .PP
176 Each item in the input source code, either a number (see the
177 \f[B]NUMBERS\f[] section) or a command (see the \f[B]COMMANDS\f[]
178 section), is processed and executed, in order.
179 Input is processed immediately when entered.
180 .PP
181 \f[B]ibase\f[] is a register (see the \f[B]REGISTERS\f[] section) that
182 determines how to interpret constant numbers.
183 It is the "input" base, or the number base used for interpreting input
184 numbers.
185 \f[B]ibase\f[] is initially \f[B]10\f[].
186 The max allowable value for \f[B]ibase\f[] is \f[B]16\f[].
187 The min allowable value for \f[B]ibase\f[] is \f[B]2\f[].
188 The max allowable value for \f[B]ibase\f[] can be queried in dc(1)
189 programs with the \f[B]T\f[] command.
190 .PP
191 \f[B]obase\f[] is a register (see the \f[B]REGISTERS\f[] section) that
192 determines how to output results.
193 It is the "output" base, or the number base used for outputting numbers.
194 \f[B]obase\f[] is initially \f[B]10\f[].
195 The max allowable value for \f[B]obase\f[] is \f[B]DC_BASE_MAX\f[] and
196 can be queried with the \f[B]U\f[] command.
197 The min allowable value for \f[B]obase\f[] is \f[B]0\f[].
198 If \f[B]obase\f[] is \f[B]0\f[], values are output in scientific
199 notation, and if \f[B]obase\f[] is \f[B]1\f[], values are output in
200 engineering notation.
201 Otherwise, values are output in the specified base.
202 .PP
203 Outputting in scientific and engineering notations are
204 \f[B]non\-portable extensions\f[].
205 .PP
206 The \f[I]scale\f[] of an expression is the number of digits in the
207 result of the expression right of the decimal point, and \f[B]scale\f[]
208 is a register (see the \f[B]REGISTERS\f[] section) that sets the
209 precision of any operations (with exceptions).
210 \f[B]scale\f[] is initially \f[B]0\f[].
211 \f[B]scale\f[] cannot be negative.
212 The max allowable value for \f[B]scale\f[] can be queried in dc(1)
213 programs with the \f[B]V\f[] command.
214 .PP
215 \f[B]seed\f[] is a register containing the current seed for the
216 pseudo\-random number generator.
217 If the current value of \f[B]seed\f[] is queried and stored, then if it
218 is assigned to \f[B]seed\f[] later, the pseudo\-random number generator
219 is guaranteed to produce the same sequence of pseudo\-random numbers
220 that were generated after the value of \f[B]seed\f[] was first queried.
221 .PP
222 Multiple values assigned to \f[B]seed\f[] can produce the same sequence
223 of pseudo\-random numbers.
224 Likewise, when a value is assigned to \f[B]seed\f[], it is not
225 guaranteed that querying \f[B]seed\f[] immediately after will return the
226 same value.
227 In addition, the value of \f[B]seed\f[] will change after any call to
228 the \f[B]\[aq]\f[] command or the \f[B]"\f[] command that does not get
229 receive a value of \f[B]0\f[] or \f[B]1\f[].
230 The maximum integer returned by the \f[B]\[aq]\f[] command can be
231 queried with the \f[B]W\f[] command.
232 .PP
233 \f[B]Note\f[]: The values returned by the pseudo\-random number
234 generator with the \f[B]\[aq]\f[] and \f[B]"\f[] commands are guaranteed
235 to \f[B]NOT\f[] be cryptographically secure.
236 This is a consequence of using a seeded pseudo\-random number generator.
237 However, they \f[B]are\f[] guaranteed to be reproducible with identical
238 \f[B]seed\f[] values.
239 .PP
240 The pseudo\-random number generator, \f[B]seed\f[], and all associated
241 operations are \f[B]non\-portable extensions\f[].
242 .SS Comments
243 .PP
244 Comments go from \f[B]#\f[] until, and not including, the next newline.
245 This is a \f[B]non\-portable extension\f[].
246 .SH NUMBERS
247 .PP
248 Numbers are strings made up of digits, uppercase letters up to
249 \f[B]F\f[], and at most \f[B]1\f[] period for a radix.
250 Numbers can have up to \f[B]DC_NUM_MAX\f[] digits.
251 Uppercase letters are equal to \f[B]9\f[] + their position in the
252 alphabet (i.e., \f[B]A\f[] equals \f[B]10\f[], or \f[B]9+1\f[]).
253 If a digit or letter makes no sense with the current value of
254 \f[B]ibase\f[], they are set to the value of the highest valid digit in
255 \f[B]ibase\f[].
256 .PP
257 Single\-character numbers (i.e., \f[B]A\f[] alone) take the value that
258 they would have if they were valid digits, regardless of the value of
259 \f[B]ibase\f[].
260 This means that \f[B]A\f[] alone always equals decimal \f[B]10\f[] and
261 \f[B]F\f[] alone always equals decimal \f[B]15\f[].
262 .PP
263 In addition, dc(1) accepts numbers in scientific notation.
264 These have the form \f[B]<number>e<integer>\f[].
265 The power (the portion after the \f[B]e\f[]) must be an integer.
266 An example is \f[B]1.89237e9\f[], which is equal to \f[B]1892370000\f[].
267 Negative exponents are also allowed, so \f[B]4.2890e_3\f[] is equal to
268 \f[B]0.0042890\f[].
269 .PP
270 \f[B]WARNING\f[]: Both the number and the exponent in scientific
271 notation are interpreted according to the current \f[B]ibase\f[], but
272 the number is still multiplied by \f[B]10^exponent\f[] regardless of the
273 current \f[B]ibase\f[].
274 For example, if \f[B]ibase\f[] is \f[B]16\f[] and dc(1) is given the
275 number string \f[B]FFeA\f[], the resulting decimal number will be
276 \f[B]2550000000000\f[], and if dc(1) is given the number string
277 \f[B]10e_4\f[], the resulting decimal number will be \f[B]0.0016\f[].
278 .PP
279 Accepting input as scientific notation is a \f[B]non\-portable
280 extension\f[].
281 .SH COMMANDS
282 .PP
283 The valid commands are listed below.
284 .SS Printing
285 .PP
286 These commands are used for printing.
287 .PP
288 Note that both scientific notation and engineering notation are
289 available for printing numbers.
290 Scientific notation is activated by assigning \f[B]0\f[] to
291 \f[B]obase\f[] using \f[B]0o\f[], and engineering notation is activated
292 by assigning \f[B]1\f[] to \f[B]obase\f[] using \f[B]1o\f[].
293 To deactivate them, just assign a different value to \f[B]obase\f[].
294 .PP
295 Printing numbers in scientific notation and/or engineering notation is a
296 \f[B]non\-portable extension\f[].
297 .TP
298 .B \f[B]p\f[]
299 Prints the value on top of the stack, whether number or string, and
300 prints a newline after.
301 .RS
302 .PP
303 This does not alter the stack.
304 .RE
305 .TP
306 .B \f[B]n\f[]
307 Prints the value on top of the stack, whether number or string, and pops
308 it off of the stack.
309 .RS
310 .RE
311 .TP
312 .B \f[B]P\f[]
313 Pops a value off the stack.
314 .RS
315 .PP
316 If the value is a number, it is truncated and the absolute value of the
317 result is printed as though \f[B]obase\f[] is \f[B]UCHAR_MAX+1\f[] and
318 each digit is interpreted as an ASCII character, making it a byte
319 stream.
320 .PP
321 If the value is a string, it is printed without a trailing newline.
322 .PP
323 This is a \f[B]non\-portable extension\f[].
324 .RE
325 .TP
326 .B \f[B]f\f[]
327 Prints the entire contents of the stack, in order from newest to oldest,
328 without altering anything.
329 .RS
330 .PP
331 Users should use this command when they get lost.
332 .RE
333 .SS Arithmetic
334 .PP
335 These are the commands used for arithmetic.
336 .TP
337 .B \f[B]+\f[]
338 The top two values are popped off the stack, added, and the result is
339 pushed onto the stack.
340 The \f[I]scale\f[] of the result is equal to the max \f[I]scale\f[] of
341 both operands.
342 .RS
343 .RE
344 .TP
345 .B \f[B]\-\f[]
346 The top two values are popped off the stack, subtracted, and the result
347 is pushed onto the stack.
348 The \f[I]scale\f[] of the result is equal to the max \f[I]scale\f[] of
349 both operands.
350 .RS
351 .RE
352 .TP
353 .B \f[B]*\f[]
354 The top two values are popped off the stack, multiplied, and the result
355 is pushed onto the stack.
356 If \f[B]a\f[] is the \f[I]scale\f[] of the first expression and
357 \f[B]b\f[] is the \f[I]scale\f[] of the second expression, the
358 \f[I]scale\f[] of the result is equal to
359 \f[B]min(a+b,max(scale,a,b))\f[] where \f[B]min()\f[] and \f[B]max()\f[]
360 return the obvious values.
361 .RS
362 .RE
363 .TP
364 .B \f[B]/\f[]
365 The top two values are popped off the stack, divided, and the result is
366 pushed onto the stack.
367 The \f[I]scale\f[] of the result is equal to \f[B]scale\f[].
368 .RS
369 .PP
370 The first value popped off of the stack must be non\-zero.
371 .RE
372 .TP
373 .B \f[B]%\f[]
374 The top two values are popped off the stack, remaindered, and the result
375 is pushed onto the stack.
376 .RS
377 .PP
378 Remaindering is equivalent to 1) Computing \f[B]a/b\f[] to current
379 \f[B]scale\f[], and 2) Using the result of step 1 to calculate
380 \f[B]a\-(a/b)*b\f[] to \f[I]scale\f[]
381 \f[B]max(scale+scale(b),scale(a))\f[].
382 .PP
383 The first value popped off of the stack must be non\-zero.
384 .RE
385 .TP
386 .B \f[B]~\f[]
387 The top two values are popped off the stack, divided and remaindered,
388 and the results (divided first, remainder second) are pushed onto the
389 stack.
390 This is equivalent to \f[B]x y / x y %\f[] except that \f[B]x\f[] and
391 \f[B]y\f[] are only evaluated once.
392 .RS
393 .PP
394 The first value popped off of the stack must be non\-zero.
395 .PP
396 This is a \f[B]non\-portable extension\f[].
397 .RE
398 .TP
399 .B \f[B]^\f[]
400 The top two values are popped off the stack, the second is raised to the
401 power of the first, and the result is pushed onto the stack.
402 .RS
403 .PP
404 The first value popped off of the stack must be an integer, and if that
405 value is negative, the second value popped off of the stack must be
406 non\-zero.
407 .RE
408 .TP
409 .B \f[B]v\f[]
410 The top value is popped off the stack, its square root is computed, and
411 the result is pushed onto the stack.
412 The \f[I]scale\f[] of the result is equal to \f[B]scale\f[].
413 .RS
414 .PP
415 The value popped off of the stack must be non\-negative.
416 .RE
417 .TP
418 .B \f[B]_\f[]
419 If this command \f[I]immediately\f[] precedes a number (i.e., no spaces
420 or other commands), then that number is input as a negative number.
421 .RS
422 .PP
423 Otherwise, the top value on the stack is popped and copied, and the copy
424 is negated and pushed onto the stack.
425 This behavior without a number is a \f[B]non\-portable extension\f[].
426 .RE
427 .TP
428 .B \f[B]b\f[]
429 The top value is popped off the stack, and if it is zero, it is pushed
430 back onto the stack.
431 Otherwise, its absolute value is pushed onto the stack.
432 .RS
433 .PP
434 This is a \f[B]non\-portable extension\f[].
435 .RE
436 .TP
437 .B \f[B]|\f[]
438 The top three values are popped off the stack, a modular exponentiation
439 is computed, and the result is pushed onto the stack.
440 .RS
441 .PP
442 The first value popped is used as the reduction modulus and must be an
443 integer and non\-zero.
444 The second value popped is used as the exponent and must be an integer
445 and non\-negative.
446 The third value popped is the base and must be an integer.
447 .PP
448 This is a \f[B]non\-portable extension\f[].
449 .RE
450 .TP
451 .B \f[B]$\f[]
452 The top value is popped off the stack and copied, and the copy is
453 truncated and pushed onto the stack.
454 .RS
455 .PP
456 This is a \f[B]non\-portable extension\f[].
457 .RE
458 .TP
459 .B \f[B]\@\f[]
460 The top two values are popped off the stack, and the precision of the
461 second is set to the value of the first, whether by truncation or
462 extension.
463 .RS
464 .PP
465 The first value popped off of the stack must be an integer and
466 non\-negative.
467 .PP
468 This is a \f[B]non\-portable extension\f[].
469 .RE
470 .TP
471 .B \f[B]H\f[]
472 The top two values are popped off the stack, and the second is shifted
473 left (radix shifted right) to the value of the first.
474 .RS
475 .PP
476 The first value popped off of the stack must be an integer and
477 non\-negative.
478 .PP
479 This is a \f[B]non\-portable extension\f[].
480 .RE
481 .TP
482 .B \f[B]h\f[]
483 The top two values are popped off the stack, and the second is shifted
484 right (radix shifted left) to the value of the first.
485 .RS
486 .PP
487 The first value popped off of the stack must be an integer and
488 non\-negative.
489 .PP
490 This is a \f[B]non\-portable extension\f[].
491 .RE
492 .TP
493 .B \f[B]G\f[]
494 The top two values are popped off of the stack, they are compared, and a
495 \f[B]1\f[] is pushed if they are equal, or \f[B]0\f[] otherwise.
496 .RS
497 .PP
498 This is a \f[B]non\-portable extension\f[].
499 .RE
500 .TP
501 .B \f[B]N\f[]
502 The top value is popped off of the stack, and if it a \f[B]0\f[], a
503 \f[B]1\f[] is pushed; otherwise, a \f[B]0\f[] is pushed.
504 .RS
505 .PP
506 This is a \f[B]non\-portable extension\f[].
507 .RE
508 .TP
509 .B \f[B](\f[]
510 The top two values are popped off of the stack, they are compared, and a
511 \f[B]1\f[] is pushed if the first is less than the second, or \f[B]0\f[]
512 otherwise.
513 .RS
514 .PP
515 This is a \f[B]non\-portable extension\f[].
516 .RE
517 .TP
518 .B \f[B]{\f[]
519 The top two values are popped off of the stack, they are compared, and a
520 \f[B]1\f[] is pushed if the first is less than or equal to the second,
521 or \f[B]0\f[] otherwise.
522 .RS
523 .PP
524 This is a \f[B]non\-portable extension\f[].
525 .RE
526 .TP
527 .B \f[B])\f[]
528 The top two values are popped off of the stack, they are compared, and a
529 \f[B]1\f[] is pushed if the first is greater than the second, or
530 \f[B]0\f[] otherwise.
531 .RS
532 .PP
533 This is a \f[B]non\-portable extension\f[].
534 .RE
535 .TP
536 .B \f[B]}\f[]
537 The top two values are popped off of the stack, they are compared, and a
538 \f[B]1\f[] is pushed if the first is greater than or equal to the
539 second, or \f[B]0\f[] otherwise.
540 .RS
541 .PP
542 This is a \f[B]non\-portable extension\f[].
543 .RE
544 .TP
545 .B \f[B]M\f[]
546 The top two values are popped off of the stack.
547 If they are both non\-zero, a \f[B]1\f[] is pushed onto the stack.
548 If either of them is zero, or both of them are, then a \f[B]0\f[] is
549 pushed onto the stack.
550 .RS
551 .PP
552 This is like the \f[B]&&\f[] operator in bc(1), and it is \f[I]not\f[] a
553 short\-circuit operator.
554 .PP
555 This is a \f[B]non\-portable extension\f[].
556 .RE
557 .TP
558 .B \f[B]m\f[]
559 The top two values are popped off of the stack.
560 If at least one of them is non\-zero, a \f[B]1\f[] is pushed onto the
561 stack.
562 If both of them are zero, then a \f[B]0\f[] is pushed onto the stack.
563 .RS
564 .PP
565 This is like the \f[B]||\f[] operator in bc(1), and it is \f[I]not\f[] a
566 short\-circuit operator.
567 .PP
568 This is a \f[B]non\-portable extension\f[].
569 .RE
570 .SS Pseudo\-Random Number Generator
571 .PP
572 dc(1) has a built\-in pseudo\-random number generator.
573 These commands query the pseudo\-random number generator.
574 (See Parameters for more information about the \f[B]seed\f[] value that
575 controls the pseudo\-random number generator.)
576 .PP
577 The pseudo\-random number generator is guaranteed to \f[B]NOT\f[] be
578 cryptographically secure.
579 .TP
580 .B \f[B]\[aq]\f[]
581 Generates an integer between 0 and \f[B]DC_RAND_MAX\f[], inclusive (see
582 the \f[B]LIMITS\f[] section).
583 .RS
584 .PP
585 The generated integer is made as unbiased as possible, subject to the
586 limitations of the pseudo\-random number generator.
587 .PP
588 This is a \f[B]non\-portable extension\f[].
589 .RE
590 .TP
591 .B \f[B]"\f[]
592 Pops a value off of the stack, which is used as an \f[B]exclusive\f[]
593 upper bound on the integer that will be generated.
594 If the bound is negative or is a non\-integer, an error is raised, and
595 dc(1) resets (see the \f[B]RESET\f[] section) while \f[B]seed\f[]
596 remains unchanged.
597 If the bound is larger than \f[B]DC_RAND_MAX\f[], the higher bound is
598 honored by generating several pseudo\-random integers, multiplying them
599 by appropriate powers of \f[B]DC_RAND_MAX+1\f[], and adding them
600 together.
601 Thus, the size of integer that can be generated with this command is
602 unbounded.
603 Using this command will change the value of \f[B]seed\f[], unless the
604 operand is \f[B]0\f[] or \f[B]1\f[].
605 In that case, \f[B]0\f[] is pushed onto the stack, and \f[B]seed\f[] is
606 \f[I]not\f[] changed.
607 .RS
608 .PP
609 The generated integer is made as unbiased as possible, subject to the
610 limitations of the pseudo\-random number generator.
611 .PP
612 This is a \f[B]non\-portable extension\f[].
613 .RE
614 .SS Stack Control
615 .PP
616 These commands control the stack.
617 .TP
618 .B \f[B]c\f[]
619 Removes all items from ("clears") the stack.
620 .RS
621 .RE
622 .TP
623 .B \f[B]d\f[]
624 Copies the item on top of the stack ("duplicates") and pushes the copy
625 onto the stack.
626 .RS
627 .RE
628 .TP
629 .B \f[B]r\f[]
630 Swaps ("reverses") the two top items on the stack.
631 .RS
632 .RE
633 .TP
634 .B \f[B]R\f[]
635 Pops ("removes") the top value from the stack.
636 .RS
637 .RE
638 .SS Register Control
639 .PP
640 These commands control registers (see the \f[B]REGISTERS\f[] section).
641 .TP
642 .B \f[B]s\f[]\f[I]r\f[]
643 Pops the value off the top of the stack and stores it into register
644 \f[I]r\f[].
645 .RS
646 .RE
647 .TP
648 .B \f[B]l\f[]\f[I]r\f[]
649 Copies the value in register \f[I]r\f[] and pushes it onto the stack.
650 This does not alter the contents of \f[I]r\f[].
651 .RS
652 .RE
653 .TP
654 .B \f[B]S\f[]\f[I]r\f[]
655 Pops the value off the top of the (main) stack and pushes it onto the
656 stack of register \f[I]r\f[].
657 The previous value of the register becomes inaccessible.
658 .RS
659 .RE
660 .TP
661 .B \f[B]L\f[]\f[I]r\f[]
662 Pops the value off the top of the stack for register \f[I]r\f[] and push
663 it onto the main stack.
664 The previous value in the stack for register \f[I]r\f[], if any, is now
665 accessible via the \f[B]l\f[]\f[I]r\f[] command.
666 .RS
667 .RE
668 .SS Parameters
669 .PP
670 These commands control the values of \f[B]ibase\f[], \f[B]obase\f[],
671 \f[B]scale\f[], and \f[B]seed\f[].
672 Also see the \f[B]SYNTAX\f[] section.
673 .TP
674 .B \f[B]i\f[]
675 Pops the value off of the top of the stack and uses it to set
676 \f[B]ibase\f[], which must be between \f[B]2\f[] and \f[B]16\f[],
677 inclusive.
678 .RS
679 .PP
680 If the value on top of the stack has any \f[I]scale\f[], the
681 \f[I]scale\f[] is ignored.
682 .RE
683 .TP
684 .B \f[B]o\f[]
685 Pops the value off of the top of the stack and uses it to set
686 \f[B]obase\f[], which must be between \f[B]0\f[] and
687 \f[B]DC_BASE_MAX\f[], inclusive (see the \f[B]LIMITS\f[] section and the
688 \f[B]NUMBERS\f[] section).
689 .RS
690 .PP
691 If the value on top of the stack has any \f[I]scale\f[], the
692 \f[I]scale\f[] is ignored.
693 .RE
694 .TP
695 .B \f[B]k\f[]
696 Pops the value off of the top of the stack and uses it to set
697 \f[B]scale\f[], which must be non\-negative.
698 .RS
699 .PP
700 If the value on top of the stack has any \f[I]scale\f[], the
701 \f[I]scale\f[] is ignored.
702 .RE
703 .TP
704 .B \f[B]j\f[]
705 Pops the value off of the top of the stack and uses it to set
706 \f[B]seed\f[].
707 The meaning of \f[B]seed\f[] is dependent on the current pseudo\-random
708 number generator but is guaranteed to not change except for new major
709 versions.
710 .RS
711 .PP
712 The \f[I]scale\f[] and sign of the value may be significant.
713 .PP
714 If a previously used \f[B]seed\f[] value is used again, the
715 pseudo\-random number generator is guaranteed to produce the same
716 sequence of pseudo\-random numbers as it did when the \f[B]seed\f[]
717 value was previously used.
718 .PP
719 The exact value assigned to \f[B]seed\f[] is not guaranteed to be
720 returned if the \f[B]J\f[] command is used.
721 However, if \f[B]seed\f[] \f[I]does\f[] return a different value, both
722 values, when assigned to \f[B]seed\f[], are guaranteed to produce the
723 same sequence of pseudo\-random numbers.
724 This means that certain values assigned to \f[B]seed\f[] will not
725 produce unique sequences of pseudo\-random numbers.
726 .PP
727 There is no limit to the length (number of significant decimal digits)
728 or \f[I]scale\f[] of the value that can be assigned to \f[B]seed\f[].
729 .PP
730 This is a \f[B]non\-portable extension\f[].
731 .RE
732 .TP
733 .B \f[B]I\f[]
734 Pushes the current value of \f[B]ibase\f[] onto the main stack.
735 .RS
736 .RE
737 .TP
738 .B \f[B]O\f[]
739 Pushes the current value of \f[B]obase\f[] onto the main stack.
740 .RS
741 .RE
742 .TP
743 .B \f[B]K\f[]
744 Pushes the current value of \f[B]scale\f[] onto the main stack.
745 .RS
746 .RE
747 .TP
748 .B \f[B]J\f[]
749 Pushes the current value of \f[B]seed\f[] onto the main stack.
750 .RS
751 .PP
752 This is a \f[B]non\-portable extension\f[].
753 .RE
754 .TP
755 .B \f[B]T\f[]
756 Pushes the maximum allowable value of \f[B]ibase\f[] onto the main
757 stack.
758 .RS
759 .PP
760 This is a \f[B]non\-portable extension\f[].
761 .RE
762 .TP
763 .B \f[B]U\f[]
764 Pushes the maximum allowable value of \f[B]obase\f[] onto the main
765 stack.
766 .RS
767 .PP
768 This is a \f[B]non\-portable extension\f[].
769 .RE
770 .TP
771 .B \f[B]V\f[]
772 Pushes the maximum allowable value of \f[B]scale\f[] onto the main
773 stack.
774 .RS
775 .PP
776 This is a \f[B]non\-portable extension\f[].
777 .RE
778 .TP
779 .B \f[B]W\f[]
780 Pushes the maximum (inclusive) integer that can be generated with the
781 \f[B]\[aq]\f[] pseudo\-random number generator command.
782 .RS
783 .PP
784 This is a \f[B]non\-portable extension\f[].
785 .RE
786 .SS Strings
787 .PP
788 The following commands control strings.
789 .PP
790 dc(1) can work with both numbers and strings, and registers (see the
791 \f[B]REGISTERS\f[] section) can hold both strings and numbers.
792 dc(1) always knows whether the contents of a register are a string or a
793 number.
794 .PP
795 While arithmetic operations have to have numbers, and will print an
796 error if given a string, other commands accept strings.
797 .PP
798 Strings can also be executed as macros.
799 For example, if the string \f[B][1pR]\f[] is executed as a macro, then
800 the code \f[B]1pR\f[] is executed, meaning that the \f[B]1\f[] will be
801 printed with a newline after and then popped from the stack.
802 .TP
803 .B \f[B][\f[]\f[I]characters\f[]\f[B]]\f[]
804 Makes a string containing \f[I]characters\f[] and pushes it onto the
805 stack.
806 .RS
807 .PP
808 If there are brackets (\f[B][\f[] and \f[B]]\f[]) in the string, then
809 they must be balanced.
810 Unbalanced brackets can be escaped using a backslash (\f[B]\\\f[])
811 character.
812 .PP
813 If there is a backslash character in the string, the character after it
814 (even another backslash) is put into the string verbatim, but the
815 (first) backslash is not.
816 .RE
817 .TP
818 .B \f[B]a\f[]
819 The value on top of the stack is popped.
820 .RS
821 .PP
822 If it is a number, it is truncated and its absolute value is taken.
823 The result mod \f[B]UCHAR_MAX+1\f[] is calculated.
824 If that result is \f[B]0\f[], push an empty string; otherwise, push a
825 one\-character string where the character is the result of the mod
826 interpreted as an ASCII character.
827 .PP
828 If it is a string, then a new string is made.
829 If the original string is empty, the new string is empty.
830 If it is not, then the first character of the original string is used to
831 create the new string as a one\-character string.
832 The new string is then pushed onto the stack.
833 .PP
834 This is a \f[B]non\-portable extension\f[].
835 .RE
836 .TP
837 .B \f[B]x\f[]
838 Pops a value off of the top of the stack.
839 .RS
840 .PP
841 If it is a number, it is pushed back onto the stack.
842 .PP
843 If it is a string, it is executed as a macro.
844 .PP
845 This behavior is the norm whenever a macro is executed, whether by this
846 command or by the conditional execution commands below.
847 .RE
848 .TP
849 .B \f[B]>\f[]\f[I]r\f[]
850 Pops two values off of the stack that must be numbers and compares them.
851 If the first value is greater than the second, then the contents of
852 register \f[I]r\f[] are executed.
853 .RS
854 .PP
855 For example, \f[B]0 1>a\f[] will execute the contents of register
856 \f[B]a\f[], and \f[B]1 0>a\f[] will not.
857 .PP
858 If either or both of the values are not numbers, dc(1) will raise an
859 error and reset (see the \f[B]RESET\f[] section).
860 .RE
861 .TP
862 .B \f[B]>\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
863 Like the above, but will execute register \f[I]s\f[] if the comparison
864 fails.
865 .RS
866 .PP
867 If either or both of the values are not numbers, dc(1) will raise an
868 error and reset (see the \f[B]RESET\f[] section).
869 .PP
870 This is a \f[B]non\-portable extension\f[].
871 .RE
872 .TP
873 .B \f[B]!>\f[]\f[I]r\f[]
874 Pops two values off of the stack that must be numbers and compares them.
875 If the first value is not greater than the second (less than or equal
876 to), then the contents of register \f[I]r\f[] are executed.
877 .RS
878 .PP
879 If either or both of the values are not numbers, dc(1) will raise an
880 error and reset (see the \f[B]RESET\f[] section).
881 .RE
882 .TP
883 .B \f[B]!>\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
884 Like the above, but will execute register \f[I]s\f[] if the comparison
885 fails.
886 .RS
887 .PP
888 If either or both of the values are not numbers, dc(1) will raise an
889 error and reset (see the \f[B]RESET\f[] section).
890 .PP
891 This is a \f[B]non\-portable extension\f[].
892 .RE
893 .TP
894 .B \f[B]<\f[]\f[I]r\f[]
895 Pops two values off of the stack that must be numbers and compares them.
896 If the first value is less than the second, then the contents of
897 register \f[I]r\f[] are executed.
898 .RS
899 .PP
900 If either or both of the values are not numbers, dc(1) will raise an
901 error and reset (see the \f[B]RESET\f[] section).
902 .RE
903 .TP
904 .B \f[B]<\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
905 Like the above, but will execute register \f[I]s\f[] if the comparison
906 fails.
907 .RS
908 .PP
909 If either or both of the values are not numbers, dc(1) will raise an
910 error and reset (see the \f[B]RESET\f[] section).
911 .PP
912 This is a \f[B]non\-portable extension\f[].
913 .RE
914 .TP
915 .B \f[B]!<\f[]\f[I]r\f[]
916 Pops two values off of the stack that must be numbers and compares them.
917 If the first value is not less than the second (greater than or equal
918 to), then the contents of register \f[I]r\f[] are executed.
919 .RS
920 .PP
921 If either or both of the values are not numbers, dc(1) will raise an
922 error and reset (see the \f[B]RESET\f[] section).
923 .RE
924 .TP
925 .B \f[B]!<\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
926 Like the above, but will execute register \f[I]s\f[] if the comparison
927 fails.
928 .RS
929 .PP
930 If either or both of the values are not numbers, dc(1) will raise an
931 error and reset (see the \f[B]RESET\f[] section).
932 .PP
933 This is a \f[B]non\-portable extension\f[].
934 .RE
935 .TP
936 .B \f[B]=\f[]\f[I]r\f[]
937 Pops two values off of the stack that must be numbers and compares them.
938 If the first value is equal to the second, then the contents of register
939 \f[I]r\f[] are executed.
940 .RS
941 .PP
942 If either or both of the values are not numbers, dc(1) will raise an
943 error and reset (see the \f[B]RESET\f[] section).
944 .RE
945 .TP
946 .B \f[B]=\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
947 Like the above, but will execute register \f[I]s\f[] if the comparison
948 fails.
949 .RS
950 .PP
951 If either or both of the values are not numbers, dc(1) will raise an
952 error and reset (see the \f[B]RESET\f[] section).
953 .PP
954 This is a \f[B]non\-portable extension\f[].
955 .RE
956 .TP
957 .B \f[B]!=\f[]\f[I]r\f[]
958 Pops two values off of the stack that must be numbers and compares them.
959 If the first value is not equal to the second, then the contents of
960 register \f[I]r\f[] are executed.
961 .RS
962 .PP
963 If either or both of the values are not numbers, dc(1) will raise an
964 error and reset (see the \f[B]RESET\f[] section).
965 .RE
966 .TP
967 .B \f[B]!=\f[]\f[I]r\f[]\f[B]e\f[]\f[I]s\f[]
968 Like the above, but will execute register \f[I]s\f[] if the comparison
969 fails.
970 .RS
971 .PP
972 If either or both of the values are not numbers, dc(1) will raise an
973 error and reset (see the \f[B]RESET\f[] section).
974 .PP
975 This is a \f[B]non\-portable extension\f[].
976 .RE
977 .TP
978 .B \f[B]?\f[]
979 Reads a line from the \f[B]stdin\f[] and executes it.
980 This is to allow macros to request input from users.
981 .RS
982 .RE
983 .TP
984 .B \f[B]q\f[]
985 During execution of a macro, this exits the execution of that macro and
986 the execution of the macro that executed it.
987 If there are no macros, or only one macro executing, dc(1) exits.
988 .RS
989 .RE
990 .TP
991 .B \f[B]Q\f[]
992 Pops a value from the stack which must be non\-negative and is used the
993 number of macro executions to pop off of the execution stack.
994 If the number of levels to pop is greater than the number of executing
995 macros, dc(1) exits.
996 .RS
997 .RE
998 .SS Status
999 .PP
1000 These commands query status of the stack or its top value.
1001 .TP
1002 .B \f[B]Z\f[]
1003 Pops a value off of the stack.
1004 .RS
1005 .PP
1006 If it is a number, calculates the number of significant decimal digits
1007 it has and pushes the result.
1008 .PP
1009 If it is a string, pushes the number of characters the string has.
1010 .RE
1011 .TP
1012 .B \f[B]X\f[]
1013 Pops a value off of the stack.
1014 .RS
1015 .PP
1016 If it is a number, pushes the \f[I]scale\f[] of the value onto the
1017 stack.
1018 .PP
1019 If it is a string, pushes \f[B]0\f[].
1020 .RE
1021 .TP
1022 .B \f[B]z\f[]
1023 Pushes the current stack depth (before execution of this command).
1024 .RS
1025 .RE
1026 .SS Arrays
1027 .PP
1028 These commands manipulate arrays.
1029 .TP
1030 .B \f[B]:\f[]\f[I]r\f[]
1031 Pops the top two values off of the stack.
1032 The second value will be stored in the array \f[I]r\f[] (see the
1033 \f[B]REGISTERS\f[] section), indexed by the first value.
1034 .RS
1035 .RE
1036 .TP
1037 .B \f[B];\f[]\f[I]r\f[]
1038 Pops the value on top of the stack and uses it as an index into the
1039 array \f[I]r\f[].
1040 The selected value is then pushed onto the stack.
1041 .RS
1042 .RE
1043 .SH REGISTERS
1044 .PP
1045 Registers are names that can store strings, numbers, and arrays.
1046 (Number/string registers do not interfere with array registers.)
1047 .PP
1048 Each register is also its own stack, so the current register value is
1049 the top of the stack for the register.
1050 All registers, when first referenced, have one value (\f[B]0\f[]) in
1051 their stack.
1052 .PP
1053 In non\-extended register mode, a register name is just the single
1054 character that follows any command that needs a register name.
1055 The only exception is a newline (\f[B]\[aq]\\n\[aq]\f[]); it is a parse
1056 error for a newline to be used as a register name.
1057 .SS Extended Register Mode
1058 .PP
1059 Unlike most other dc(1) implentations, this dc(1) provides nearly
1060 unlimited amounts of registers, if extended register mode is enabled.
1061 .PP
1062 If extended register mode is enabled (\f[B]\-x\f[] or
1063 \f[B]\-\-extended\-register\f[] command\-line arguments are given), then
1064 normal single character registers are used \f[I]unless\f[] the character
1065 immediately following a command that needs a register name is a space
1066 (according to \f[B]isspace()\f[]) and not a newline
1067 (\f[B]\[aq]\\n\[aq]\f[]).
1068 .PP
1069 In that case, the register name is found according to the regex
1070 \f[B][a\-z][a\-z0\-9_]*\f[] (like bc(1) identifiers), and it is a parse
1071 error if the next non\-space characters do not match that regex.
1072 .SH RESET
1073 .PP
1074 When dc(1) encounters an error or a signal that it has a non\-default
1075 handler for, it resets.
1076 This means that several things happen.
1077 .PP
1078 First, any macros that are executing are stopped and popped off the
1079 stack.
1080 The behavior is not unlike that of exceptions in programming languages.
1081 Then the execution point is set so that any code waiting to execute
1082 (after all macros returned) is skipped.
1083 .PP
1084 Thus, when dc(1) resets, it skips any remaining code waiting to be
1085 executed.
1086 Then, if it is interactive mode, and the error was not a fatal error
1087 (see the \f[B]EXIT STATUS\f[] section), it asks for more input;
1088 otherwise, it exits with the appropriate return code.
1089 .SH PERFORMANCE
1090 .PP
1091 Most dc(1) implementations use \f[B]char\f[] types to calculate the
1092 value of \f[B]1\f[] decimal digit at a time, but that can be slow.
1093 This dc(1) does something different.
1094 .PP
1095 It uses large integers to calculate more than \f[B]1\f[] decimal digit
1096 at a time.
1097 If built in a environment where \f[B]DC_LONG_BIT\f[] (see the
1098 \f[B]LIMITS\f[] section) is \f[B]64\f[], then each integer has
1099 \f[B]9\f[] decimal digits.
1100 If built in an environment where \f[B]DC_LONG_BIT\f[] is \f[B]32\f[]
1101 then each integer has \f[B]4\f[] decimal digits.
1102 This value (the number of decimal digits per large integer) is called
1103 \f[B]DC_BASE_DIGS\f[].
1104 .PP
1105 In addition, this dc(1) uses an even larger integer for overflow
1106 checking.
1107 This integer type depends on the value of \f[B]DC_LONG_BIT\f[], but is
1108 always at least twice as large as the integer type used to store digits.
1109 .SH LIMITS
1110 .PP
1111 The following are the limits on dc(1):
1112 .TP
1113 .B \f[B]DC_LONG_BIT\f[]
1114 The number of bits in the \f[B]long\f[] type in the environment where
1115 dc(1) was built.
1116 This determines how many decimal digits can be stored in a single large
1117 integer (see the \f[B]PERFORMANCE\f[] section).
1118 .RS
1119 .RE
1120 .TP
1121 .B \f[B]DC_BASE_DIGS\f[]
1122 The number of decimal digits per large integer (see the
1123 \f[B]PERFORMANCE\f[] section).
1124 Depends on \f[B]DC_LONG_BIT\f[].
1125 .RS
1126 .RE
1127 .TP
1128 .B \f[B]DC_BASE_POW\f[]
1129 The max decimal number that each large integer can store (see
1130 \f[B]DC_BASE_DIGS\f[]) plus \f[B]1\f[].
1131 Depends on \f[B]DC_BASE_DIGS\f[].
1132 .RS
1133 .RE
1134 .TP
1135 .B \f[B]DC_OVERFLOW_MAX\f[]
1136 The max number that the overflow type (see the \f[B]PERFORMANCE\f[]
1137 section) can hold.
1138 Depends on \f[B]DC_LONG_BIT\f[].
1139 .RS
1140 .RE
1141 .TP
1142 .B \f[B]DC_BASE_MAX\f[]
1143 The maximum output base.
1144 Set at \f[B]DC_BASE_POW\f[].
1145 .RS
1146 .RE
1147 .TP
1148 .B \f[B]DC_DIM_MAX\f[]
1149 The maximum size of arrays.
1150 Set at \f[B]SIZE_MAX\-1\f[].
1151 .RS
1152 .RE
1153 .TP
1154 .B \f[B]DC_SCALE_MAX\f[]
1155 The maximum \f[B]scale\f[].
1156 Set at \f[B]DC_OVERFLOW_MAX\-1\f[].
1157 .RS
1158 .RE
1159 .TP
1160 .B \f[B]DC_STRING_MAX\f[]
1161 The maximum length of strings.
1162 Set at \f[B]DC_OVERFLOW_MAX\-1\f[].
1163 .RS
1164 .RE
1165 .TP
1166 .B \f[B]DC_NAME_MAX\f[]
1167 The maximum length of identifiers.
1168 Set at \f[B]DC_OVERFLOW_MAX\-1\f[].
1169 .RS
1170 .RE
1171 .TP
1172 .B \f[B]DC_NUM_MAX\f[]
1173 The maximum length of a number (in decimal digits), which includes
1174 digits after the decimal point.
1175 Set at \f[B]DC_OVERFLOW_MAX\-1\f[].
1176 .RS
1177 .RE
1178 .TP
1179 .B \f[B]DC_RAND_MAX\f[]
1180 The maximum integer (inclusive) returned by the \f[B]\[aq]\f[] command,
1181 if dc(1).
1182 Set at \f[B]2^DC_LONG_BIT\-1\f[].
1183 .RS
1184 .RE
1185 .TP
1186 .B Exponent
1187 The maximum allowable exponent (positive or negative).
1188 Set at \f[B]DC_OVERFLOW_MAX\f[].
1189 .RS
1190 .RE
1191 .TP
1192 .B Number of vars
1193 The maximum number of vars/arrays.
1194 Set at \f[B]SIZE_MAX\-1\f[].
1195 .RS
1196 .RE
1197 .PP
1198 These limits are meant to be effectively non\-existent; the limits are
1199 so large (at least on 64\-bit machines) that there should not be any
1200 point at which they become a problem.
1201 In fact, memory should be exhausted before these limits should be hit.
1202 .SH ENVIRONMENT VARIABLES
1203 .PP
1204 dc(1) recognizes the following environment variables:
1205 .TP
1206 .B \f[B]DC_ENV_ARGS\f[]
1207 This is another way to give command\-line arguments to dc(1).
1208 They should be in the same format as all other command\-line arguments.
1209 These are always processed first, so any files given in
1210 \f[B]DC_ENV_ARGS\f[] will be processed before arguments and files given
1211 on the command\-line.
1212 This gives the user the ability to set up "standard" options and files
1213 to be used at every invocation.
1214 The most useful thing for such files to contain would be useful
1215 functions that the user might want every time dc(1) runs.
1216 Another use would be to use the \f[B]\-e\f[] option to set
1217 \f[B]scale\f[] to a value other than \f[B]0\f[].
1218 .RS
1219 .PP
1220 The code that parses \f[B]DC_ENV_ARGS\f[] will correctly handle quoted
1221 arguments, but it does not understand escape sequences.
1222 For example, the string \f[B]"/home/gavin/some dc file.dc"\f[] will be
1223 correctly parsed, but the string \f[B]"/home/gavin/some "dc"
1224 file.dc"\f[] will include the backslashes.
1225 .PP
1226 The quote parsing will handle either kind of quotes, \f[B]\[aq]\f[] or
1227 \f[B]"\f[].
1228 Thus, if you have a file with any number of single quotes in the name,
1229 you can use double quotes as the outside quotes, as in \f[B]"some
1230 \[aq]bc\[aq] file.bc"\f[], and vice versa if you have a file with double
1231 quotes.
1232 However, handling a file with both kinds of quotes in
1233 \f[B]DC_ENV_ARGS\f[] is not supported due to the complexity of the
1234 parsing, though such files are still supported on the command\-line
1235 where the parsing is done by the shell.
1236 .RE
1237 .TP
1238 .B \f[B]DC_LINE_LENGTH\f[]
1239 If this environment variable exists and contains an integer that is
1240 greater than \f[B]1\f[] and is less than \f[B]UINT16_MAX\f[]
1241 (\f[B]2^16\-1\f[]), dc(1) will output lines to that length, including
1242 the backslash newline combo.
1243 The default line length is \f[B]70\f[].
1244 .RS
1245 .RE
1246 .TP
1247 .B \f[B]DC_EXPR_EXIT\f[]
1248 If this variable exists (no matter the contents), dc(1) will exit
1249 immediately after executing expressions and files given by the
1250 \f[B]\-e\f[] and/or \f[B]\-f\f[] command\-line options (and any
1251 equivalents).
1252 .RS
1253 .RE
1254 .SH EXIT STATUS
1255 .PP
1256 dc(1) returns the following exit statuses:
1257 .TP
1258 .B \f[B]0\f[]
1259 No error.
1260 .RS
1261 .RE
1262 .TP
1263 .B \f[B]1\f[]
1264 A math error occurred.
1265 This follows standard practice of using \f[B]1\f[] for expected errors,
1266 since math errors will happen in the process of normal execution.
1267 .RS
1268 .PP
1269 Math errors include divide by \f[B]0\f[], taking the square root of a
1270 negative number, using a negative number as a bound for the
1271 pseudo\-random number generator, attempting to convert a negative number
1272 to a hardware integer, overflow when converting a number to a hardware
1273 integer, and attempting to use a non\-integer where an integer is
1274 required.
1275 .PP
1276 Converting to a hardware integer happens for the second operand of the
1277 power (\f[B]^\f[]), places (\f[B]\@\f[]), left shift (\f[B]H\f[]), and
1278 right shift (\f[B]h\f[]) operators.
1279 .RE
1280 .TP
1281 .B \f[B]2\f[]
1282 A parse error occurred.
1283 .RS
1284 .PP
1285 Parse errors include unexpected \f[B]EOF\f[], using an invalid
1286 character, failing to find the end of a string or comment, and using a
1287 token where it is invalid.
1288 .RE
1289 .TP
1290 .B \f[B]3\f[]
1291 A runtime error occurred.
1292 .RS
1293 .PP
1294 Runtime errors include assigning an invalid number to \f[B]ibase\f[],
1295 \f[B]obase\f[], or \f[B]scale\f[]; give a bad expression to a
1296 \f[B]read()\f[] call, calling \f[B]read()\f[] inside of a
1297 \f[B]read()\f[] call, type errors, and attempting an operation when the
1298 stack has too few elements.
1299 .RE
1300 .TP
1301 .B \f[B]4\f[]
1302 A fatal error occurred.
1303 .RS
1304 .PP
1305 Fatal errors include memory allocation errors, I/O errors, failing to
1306 open files, attempting to use files that do not have only ASCII
1307 characters (dc(1) only accepts ASCII characters), attempting to open a
1308 directory as a file, and giving invalid command\-line options.
1309 .RE
1310 .PP
1311 The exit status \f[B]4\f[] is special; when a fatal error occurs, dc(1)
1312 always exits and returns \f[B]4\f[], no matter what mode dc(1) is in.
1313 .PP
1314 The other statuses will only be returned when dc(1) is not in
1315 interactive mode (see the \f[B]INTERACTIVE MODE\f[] section), since
1316 dc(1) resets its state (see the \f[B]RESET\f[] section) and accepts more
1317 input when one of those errors occurs in interactive mode.
1318 This is also the case when interactive mode is forced by the
1319 \f[B]\-i\f[] flag or \f[B]\-\-interactive\f[] option.
1320 .PP
1321 These exit statuses allow dc(1) to be used in shell scripting with error
1322 checking, and its normal behavior can be forced by using the
1323 \f[B]\-i\f[] flag or \f[B]\-\-interactive\f[] option.
1324 .SH INTERACTIVE MODE
1325 .PP
1326 Like bc(1), dc(1) has an interactive mode and a non\-interactive mode.
1327 Interactive mode is turned on automatically when both \f[B]stdin\f[] and
1328 \f[B]stdout\f[] are hooked to a terminal, but the \f[B]\-i\f[] flag and
1329 \f[B]\-\-interactive\f[] option can turn it on in other cases.
1330 .PP
1331 In interactive mode, dc(1) attempts to recover from errors (see the
1332 \f[B]RESET\f[] section), and in normal execution, flushes
1333 \f[B]stdout\f[] as soon as execution is done for the current input.
1334 .SH TTY MODE
1335 .PP
1336 If \f[B]stdin\f[], \f[B]stdout\f[], and \f[B]stderr\f[] are all
1337 connected to a TTY, dc(1) turns on "TTY mode."
1338 .PP
1339 TTY mode is required for history to be enabled (see the \f[B]COMMAND
1340 LINE HISTORY\f[] section).
1341 It is also required to enable special handling for \f[B]SIGINT\f[]
1342 signals.
1343 .PP
1344 The prompt is enabled in TTY mode.
1345 .PP
1346 TTY mode is different from interactive mode because interactive mode is
1347 required in the bc(1)
1348 specification (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html),
1349 and interactive mode requires only \f[B]stdin\f[] and \f[B]stdout\f[] to
1350 be connected to a terminal.
1351 .SH SIGNAL HANDLING
1352 .PP
1353 Sending a \f[B]SIGINT\f[] will cause dc(1) to stop execution of the
1354 current input.
1355 If dc(1) is in TTY mode (see the \f[B]TTY MODE\f[] section), it will
1356 reset (see the \f[B]RESET\f[] section).
1357 Otherwise, it will clean up and exit.
1358 .PP
1359 Note that "current input" can mean one of two things.
1360 If dc(1) is processing input from \f[B]stdin\f[] in TTY mode, it will
1361 ask for more input.
1362 If dc(1) is processing input from a file in TTY mode, it will stop
1363 processing the file and start processing the next file, if one exists,
1364 or ask for input from \f[B]stdin\f[] if no other file exists.
1365 .PP
1366 This means that if a \f[B]SIGINT\f[] is sent to dc(1) as it is executing
1367 a file, it can seem as though dc(1) did not respond to the signal since
1368 it will immediately start executing the next file.
1369 This is by design; most files that users execute when interacting with
1370 dc(1) have function definitions, which are quick to parse.
1371 If a file takes a long time to execute, there may be a bug in that file.
1372 The rest of the files could still be executed without problem, allowing
1373 the user to continue.
1374 .PP
1375 \f[B]SIGTERM\f[] and \f[B]SIGQUIT\f[] cause dc(1) to clean up and exit,
1376 and it uses the default handler for all other signals.
1377 The one exception is \f[B]SIGHUP\f[]; in that case, when dc(1) is in TTY
1378 mode, a \f[B]SIGHUP\f[] will cause dc(1) to clean up and exit.
1379 .SH COMMAND LINE HISTORY
1380 .PP
1381 dc(1) supports interactive command\-line editing.
1382 If dc(1) is in TTY mode (see the \f[B]TTY MODE\f[] section), history is
1383 enabled.
1384 Previous lines can be recalled and edited with the arrow keys.
1385 .PP
1386 \f[B]Note\f[]: tabs are converted to 8 spaces.
1387 .SH SEE ALSO
1388 .PP
1389 bc(1)
1390 .SH STANDARDS
1391 .PP
1392 The dc(1) utility operators are compliant with the operators in the
1393 bc(1) IEEE Std 1003.1\-2017
1394 (“POSIX.1\-2017”) (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html)
1395 specification.
1396 .SH BUGS
1397 .PP
1398 None are known.
1399 Report bugs at https://git.yzena.com/gavin/bc.
1400 .SH AUTHOR
1401 .PP
1402 Gavin D.
1403 Howard <yzena.tech@gmail.com> and contributors.