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