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