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