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