Lines Matching +full:dc +full:- +full:to +full:- +full:dc
1 <!---
3 SPDX-License-Identifier: BSD-2-Clause
5 Copyright (c) 2018-2024 Gavin D. Howard and contributors.
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 -->
33 dc - arbitrary-precision decimal reverse-Polish notation calculator
37 …dc** [**-cChiPRvVx**] [**-\-version**] [**-\-help**] [**-\-digit-clamp**] [**-\-no-digit-clamp**] …
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
45 If no files are given on the command-line, then dc(1) reads from **stdin** (see
46 the **STDIN** section). Otherwise, those files are processed, and dc(1) will
49 If a user wants to set up a standard environment, they can use **DC_ENV_ARGS**
51 **scale** always set to **10**, they can set **DC_ENV_ARGS** to **-e 10k**, and
52 this dc(1) will always start with a **scale** of **10**.
56 The following are the options that dc(1) accepts.
58 **-C**, **-\-no-digit-clamp**
60 : Disables clamping of digits greater than or equal to the current **ibase**
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
67 If this and/or the **-c** or **-\-digit-clamp** options are given multiple
72 with the **-h** or **-\-help** options.
74 This is a **non-portable extension**.
76 **-c**, **-\-digit-clamp**
78 : Enables clamping of digits greater than or equal to the current **ibase**
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
86 If this and/or the **-C** or **-\-no-digit-clamp** options are given
91 with the **-h** or **-\-help** options.
93 This is a **non-portable extension**.
95 **-e** *expr*, **-\-expression**=*expr*
102 If this option is given on the command-line (i.e., not in **DC_ENV_ARGS**,
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.
110 This is a **non-portable extension**.
112 **-f** *file*, **-\-file**=*file*
118 If this option is given on the command-line (i.e., not in **DC_ENV_ARGS**,
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.
125 This is a **non-portable extension**.
127 **-h**, **-\-help**
131 **-I** *ibase*, **-\-ibase**=*ibase*
133 : Sets the builtin variable **ibase** to the value *ibase* assuming that
138 This is a **non-portable extension**.
140 **-i**, **-\-interactive**
144 This is a **non-portable extension**.
146 **-L**, **-\-no-line-length**
149 newlines. In other words, this option sets **BC_LINE_LENGTH** to **0** (see
152 This is a **non-portable extension**.
154 **-O** *obase*, **-\-obase**=*obase*
156 : Sets the builtin variable **obase** to the value *obase* assuming that
161 This is a **non-portable extension**.
163 **-P**, **-\-no-prompt**
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**.
173 This is a **non-portable extension**.
175 **-R**, **-\-no-read-prompt**
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
182 lines of dc(1) scripts that prompt for user input.
191 This is a **non-portable extension**.
193 **-S** *scale*, **-\-scale**=*scale*
195 : Sets the builtin variable **scale** to the value *scale* assuming that
200 This is a **non-portable extension**.
202 **-v**, **-V**, **-\-version**
206 **-x** **-\-extended-register**
211 This is a **non-portable extension**.
213 **-z**, **-\-leading-zeroes**
215 : Makes dc(1) print all numbers greater than **-1** and less than **1**, and
216 not equal to **0**, with a leading zero.
218 This is a **non-portable extension**.
220 All long options are **non-portable extensions**.
224 If no files are given on the command-line and no files or expressions are given
225 by the **-f**, **-\-file**, **-e**, or **-\-expression** options, then dc(1)
228 However, there is a caveat to this.
230 First, **stdin** is evaluated a line at a time. The only exception to this is if
232 brackets, all brackets must be balanced before dc(1) parses and executes.
236 Any non-error output is written to **stdout**. In addition, if history (see the
238 both are output to **stdout**.
240 **Note**: Unlike other dc(1) implementations, this dc(1) will issue a fatal
241 error (see the **EXIT STATUS** section) if it cannot write to **stdout**, so if
242 **stdout** is closed, as in **dc <file> >&-**, it will quit with an error. This
243 is done so that dc(1) can report problems when **stdout** is redirected to a
246 If there are scripts that depend on the behavior of other dc(1) implementations,
247 it is recommended that those scripts be changed to redirect **stdout** to
252 Any error output is written to **stderr**.
254 **Note**: Unlike other dc(1) implementations, this dc(1) will issue a fatal
255 error (see the **EXIT STATUS** section) if it cannot write to **stderr**, so if
256 **stderr** is closed, as in **dc <file> 2>&-**, it will quit with an error. This
257 is done so that dc(1) can exit with an error code when **stderr** is redirected
258 to a file.
260 If there are scripts that depend on the behavior of other dc(1) implementations,
261 it is recommended that those scripts be changed to redirect **stderr** to
270 **ibase** is a register (see the **REGISTERS** section) that determines how to
274 The max allowable value for **ibase** can be queried in dc(1) programs with the
277 **obase** is a register (see the **REGISTERS** section) that determines how to
287 allowable value for **scale** can be queried in dc(1) programs with the **V**
293 **non-portable extension**.
297 Numbers are strings made up of digits, uppercase letters up to **F**, and at
298 most **1** period for a radix. Numbers can have up to **DC_NUM_MAX** digits.
299 Uppercase letters are equal to **9** plus their position in the alphabet (i.e.,
303 they are greater than or equal to the current value of **ibase**), then the
304 behavior depends on the existence of the **-c**/**-\-digit-clamp** or
305 **-C**/**-\-no-digit-clamp** options (see the **OPTIONS** section), the
308 the **-h**/**-\-help** option.
310 If clamping is off, then digits or letters that are greater than or equal to the
313 means that, with an **ibase** of **3**, the number **AB** is equal to
316 If clamping is on, then digits or letters that are greater than or equal to the
317 current value of **ibase** are set to the value of the highest valid digit in
320 **AB** is equal to **3\^1\*2+3\^0\*2**, which is **3** times **2** plus **2**,
323 There is one exception to clamping: single-character numbers (i.e., **A**
327 mandated by the standard for bc(1) (see the STANDARDS section) and is meant to
328 provide an easy way to set the current **ibase** (with the **i** command)
360 interpreted as an 8-bit ASCII character, making it a byte stream.
364 This is a **non-portable extension**.
368 : Prints the entire contents of the stack, in order from newest to oldest,
380 onto the stack. The *scale* of the result is equal to the max *scale* of
383 **-**
386 pushed onto the stack. The *scale* of the result is equal to the max
394 is equal to **min(a+b,max(scale,a,b))** where **min()** and **max()** return
400 pushed onto the stack. The *scale* of the result is equal to **scale**.
402 The first value popped off of the stack must be non-zero.
409 Remaindering is equivalent to 1) Computing **a/b** to current **scale**, and
410 2) Using the result of step 1 to calculate **a-(a/b)\*b** to *scale*
413 The first value popped off of the stack must be non-zero.
419 This is equivalent to **x y / x y %** except that **x** and **y** are only
422 The first value popped off of the stack must be non-zero.
424 This is a **non-portable extension**.
428 : The top two values are popped off the stack, the second is raised to the
430 the result is equal to **scale**.
434 non-zero.
439 result is pushed onto the stack. The *scale* of the result is equal to
442 The value popped off of the stack must be non-negative.
451 **non-portable extension**.
458 This is a **non-portable extension**.
466 integer and non-zero. The second value popped is used as the exponent and
467 must be an integer and non-negative. The third value popped is the base and
470 This is a **non-portable extension**.
477 This is a **non-portable extension**.
484 This is a **non-portable extension**.
491 This is a **non-portable extension**.
496 **1** is pushed if the first is less than or equal to the second, or **0**
499 This is a **non-portable extension**.
506 This is a **non-portable extension**.
511 **1** is pushed if the first is greater than or equal to the second, or
514 This is a **non-portable extension**.
518 : The top two values are popped off of the stack. If they are both non-zero, a
522 This is like the **&&** operator in bc(1), and it is *not* a short-circuit
525 This is a **non-portable extension**.
530 non-zero, a **1** is pushed onto the stack. If both of them are zero, then a
533 This is like the **||** operator in bc(1), and it is *not* a short-circuit
536 This is a **non-portable extension**.
590 : Pops the value off of the top of the stack and uses it to set **ibase**,
597 : Pops the value off of the top of the stack and uses it to set **obase**,
605 : Pops the value off of the top of the stack and uses it to set **scale**,
606 which must be non-negative.
626 This is a **non-portable extension**.
632 This is a **non-portable extension**.
638 This is a **non-portable extension**.
644 dc(1) can work with both numbers and strings, and registers (see the
645 **REGISTERS** section) can hold both strings and numbers. dc(1) always knows
648 While arithmetic operations have to have numbers, and will print an error if
673 string; otherwise, push a one-character string where the character is the
678 the original string is used to create the new string as a one-character
681 This is a **non-portable extension**.
703 If either or both of the values are not numbers, dc(1) will raise an error
710 If either or both of the values are not numbers, dc(1) will raise an error
713 This is a **non-portable extension**.
718 the first value is not greater than the second (less than or equal to), then
721 If either or both of the values are not numbers, dc(1) will raise an error
728 If either or both of the values are not numbers, dc(1) will raise an error
731 This is a **non-portable extension**.
739 If either or both of the values are not numbers, dc(1) will raise an error
746 If either or both of the values are not numbers, dc(1) will raise an error
749 This is a **non-portable extension**.
754 the first value is not less than the second (greater than or equal to), then
757 If either or both of the values are not numbers, dc(1) will raise an error
764 If either or both of the values are not numbers, dc(1) will raise an error
767 This is a **non-portable extension**.
772 the first value is equal to the second, then the contents of register *r*
775 If either or both of the values are not numbers, dc(1) will raise an error
782 If either or both of the values are not numbers, dc(1) will raise an error
785 This is a **non-portable extension**.
790 the first value is not equal to the second, then the contents of register
793 If either or both of the values are not numbers, dc(1) will raise an error
800 If either or both of the values are not numbers, dc(1) will raise an error
803 This is a **non-portable extension**.
807 : Reads a line from the **stdin** and executes it. This is to allow macros to
814 macro executing, dc(1) exits.
818 : Pops a value from the stack which must be non-negative and is used the
819 number of macro executions to pop off of the execution stack. If the number
820 of levels to pop is greater than the number of executing macros, dc(1)
827 is exactly as many as is needed to make dc(1) exit with the **Q** command,
828 so the sequence **,Q** will make dc(1) exit.
830 This is a **non-portable extension**.
859 This is a **non-portable extension**.
866 This is a **non-portable extension**.
878 item) when dc(1) starts, dc(1) requires that each register's stack must
879 always have at least one item; dc(1) will give an error and reset otherwise
883 This is a **non-portable extension**.
903 This is a **non-portable extension**.
926 with the **-z** or **-\-leading-zeroes** options (see the **OPTIONS**
927 section), non-zero otherwise.
936 (**0**) in their stack, and it is a runtime error to attempt to pop that item
939 In non-extended register mode, a register name is just the single character that
942 newline or a left bracket to be used as a register name.
946 Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited
949 If extended register mode is enabled (**-x** or **-\-extended-register**
950 command-line arguments are given), then normal single character registers are
952 register name is a space (according to **isspace()**) and not a newline
955 In that case, the register name is found according to the regex
956 **\[a-z\]\[a-z0-9\_\]\*** (like bc(1) identifiers), and it is a parse error if
957 the next non-space characters do not match that regex.
961 When dc(1) encounters an error or a signal that it has a non-default handler
966 Then the execution point is set so that any code waiting to execute (after all
972 Thus, when dc(1) resets, it skips any remaining code waiting to be executed.
979 Most dc(1) implementations use **char** types to calculate the value of **1**
980 decimal digit at a time, but that can be slow. This dc(1) does something
983 It uses large integers to calculate more than **1** decimal digit at a time. If
990 In addition, this dc(1) uses an even larger integer for overflow checking. This
992 twice as large as the integer type used to store digits.
996 The following are the limits on dc(1):
1000 : The number of bits in the **long** type in the environment where dc(1) was
1025 : The maximum size of arrays. Set at **SIZE_MAX-1**.
1029 : The maximum **scale**. Set at **DC_OVERFLOW_MAX-1**.
1033 : The maximum length of strings. Set at **DC_OVERFLOW_MAX-1**.
1037 : The maximum length of identifiers. Set at **DC_OVERFLOW_MAX-1**.
1042 after the decimal point. Set at **DC_OVERFLOW_MAX-1**.
1051 : The maximum number of vars/arrays. Set at **SIZE_MAX-1**.
1053 These limits are meant to be effectively non-existent; the limits are so large
1054 (at least on 64-bit machines) that there should not be any point at which they
1060 As **non-portable extensions**, dc(1) recognizes the following environment
1065 : This is another way to give command-line arguments to dc(1). They should be
1066 in the same format as all other command-line arguments. These are always
1068 before arguments and files given on the command-line. This gives the user
1069 the ability to set up "standard" options and files to be used at every
1070 invocation. The most useful thing for such files to contain would be useful
1071 functions that the user might want every time dc(1) runs. Another use would
1072 be to use the **-e** option to set **scale** to a value other than **0**.
1076 **"/home/gavin/some dc file.dc"** will be correctly parsed, but the string
1077 **"/home/gavin/some \"dc\" file.dc"** will include the backslashes.
1081 double quotes as the outside quotes, as in **"some 'dc' file.dc"**, and vice
1083 both kinds of quotes in **DC_ENV_ARGS** is not supported due to the
1085 command-line where the parsing is done by the shell.
1090 than **1** and is less than **UINT16_MAX** (**2\^16-1**), dc(1) will output
1091 lines to that length, including the backslash newline combo. The default
1095 numbers without regard to line length and without backslashes and newlines.
1099 : If dc(1) is not in interactive mode (see the **INTERACTIVE MODE** section),
1100 then this environment variable has no effect because dc(1) exits on
1103 However, when dc(1) is in interactive mode, then if this environment
1104 variable exists and contains an integer, a non-zero value makes dc(1) reset
1105 on **SIGINT**, rather than exit, and zero makes dc(1) exit. If this
1106 environment variable exists and is *not* an integer, then dc(1) will exit on
1110 the **-h** or **-\-help** options.
1118 exists and contains an integer, then a non-zero value makes dc(1) use TTY
1119 mode, and zero makes dc(1) not use TTY mode.
1122 the **-h** or **-\-help** options.
1130 exists and contains an integer, a non-zero value makes dc(1) use a prompt,
1131 and zero or a non-integer makes dc(1) not use a prompt. If this environment
1136 override the default, which can be queried with the **-h** or **-\-help**
1141 : If any expressions or expression files are given on the command-line with
1142 **-e**, **-\-expression**, **-f**, or **-\-file**, then if this environment
1143 variable exists and contains an integer, a non-zero value makes dc(1) exit
1145 dc(1) not exit.
1148 the **-h** or **-\-help** options.
1153 integer, a non-zero value makes dc(1) clamp digits that are greater than or
1154 equal to the current **ibase** so that all such digits are considered equal
1155 to the **ibase** minus 1, and a zero value disables such clamping so that
1156 those digits are always equal to their value, which is multiplied by the
1159 This never applies to single-digit numbers, as per the bc(1) standard (see
1163 the **-h** or **-\-help** options.
1167 dc(1) returns the following exit statuses:
1180 number, attempting to convert a negative number to a hardware integer,
1181 overflow when converting a number to a hardware integer, overflow when
1182 calculating the size of a number, and attempting to use a non-integer where
1185 Converting to a hardware integer happens for the second operand of the power
1193 to find the end of a string or comment, and using a token where it is
1200 Runtime errors include assigning an invalid number to any global (**ibase**,
1201 **obase**, or **scale**), giving a bad expression to a **read()** call,
1203 attempting to execute a number), and attempting an operation when the stack
1210 Fatal errors include memory allocation errors, I/O errors, failing to open
1211 files, attempting to use files that do not have only ASCII characters (dc(1)
1212 only accepts ASCII characters), attempting to open a directory as a file,
1213 and giving invalid command-line options.
1215 The exit status **4** is special; when a fatal error occurs, dc(1) always exits
1216 and returns **4**, no matter what mode dc(1) is in.
1218 The other statuses will only be returned when dc(1) is not in interactive mode
1219 (see the **INTERACTIVE MODE** section), since dc(1) resets its state (see the
1222 **-i** flag or **-\-interactive** option.
1224 These exit statuses allow dc(1) to be used in shell scripting with error
1225 checking, and its normal behavior can be forced by using the **-i** flag or
1226 **-\-interactive** option.
1230 Like bc(1), dc(1) has an interactive mode and a non-interactive mode.
1232 are hooked to a terminal, but the **-i** flag and **-\-interactive** option can
1235 In interactive mode, dc(1) attempts to recover from errors (see the **RESET**
1237 done for the current input. dc(1) may also reset on **SIGINT** instead of exit,
1243 If **stdin**, **stdout**, and **stderr** are all connected to a TTY, then "TTY
1244 mode" is considered to be available, and thus, dc(1) can turn on TTY mode,
1245 subject to some settings.
1249 non-zero integer, dc(1) will turn on TTY mode when **stdin**, **stdout**, and
1250 **stderr** are all connected to a TTY. If the **DC_TTY_MODE** environment
1251 variable exists but is *not* a non-zero integer, then dc(1) will not turn TTY
1255 setting is used. The default setting can be queried with the **-h** or
1256 **-\-help** options.
1260 requires only **stdin** and **stdout** to be connected to a terminal.
1262 ## Command-Line History
1264 Command-line history is only enabled if TTY mode is, i.e., that **stdin**,
1265 **stdout**, and **stderr** are connected to a TTY and the **DC_TTY_MODE**
1276 If the environment variable **DC_PROMPT** exists and is a non-zero integer, then
1278 to a TTY and the **-P** and **-\-no-prompt** options were not used. The read
1279 prompt will be turned on under the same conditions, except that the **-R** and
1280 **-\-no-read-prompt** options must also not be used.
1283 with the **DC_TTY_MODE** environment variable, the **-P** and **-\-no-prompt**
1284 options, and the **-R** and **-\-no-read-prompt** options. See the **ENVIRONMENT
1289 Sending a **SIGINT** will cause dc(1) to do one of two things.
1291 If dc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), or
1293 section), or its default, is either not an integer or it is zero, dc(1) will
1296 However, if dc(1) is in interactive mode, and the **DC_SIGINT_RESET** or its
1297 default is an integer and non-zero, then dc(1) will stop executing the current
1300 Note that "current input" can mean one of two things. If dc(1) is processing
1301 input from **stdin** in interactive mode, it will ask for more input. If dc(1)
1306 This means that if a **SIGINT** is sent to dc(1) as it is executing a file, it
1307 can seem as though dc(1) did not respond to the signal since it will immediately
1309 when interacting with dc(1) have function definitions, which are quick to parse.
1310 If a file takes a long time to execute, there may be a bug in that file. The
1311 rest of the files could still be executed without problem, allowing the user to
1314 **SIGTERM** and **SIGQUIT** cause dc(1) to clean up and exit, and it uses the
1316 case, and only when dc(1) is in TTY mode (see the **TTY MODE** section), a
1317 **SIGHUP** will cause dc(1) to clean up and exit.
1321 dc(1) supports interactive command-line editing.
1323 If dc(1) can be in TTY mode (see the **TTY MODE** section), history can be
1324 enabled. This means that command-line history can only be enabled when
1325 **stdin**, **stdout**, and **stderr** are all connected to a TTY.
1330 **Note**: tabs are converted to 8 spaces.
1338 The dc(1) utility operators and some behavior are compliant with the operators
1339 in the IEEE Std 1003.1-2017 (“POSIX.1-2017”) bc(1) specification at