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