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