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