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