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