| dc(1) -- arbitrary-precision reverse-Polish notation calculator |
| =============================================================== |
| |
| SYNOPSIS |
| -------- |
| |
| `dc` [`-hvVx`] [`--version`] [`--help`] [`--extended-register`] [`-e` *expr*] |
| [`--expression=`*expr*...] [`-f` *file*...] [`-file=`*file*...] [*file*...] |
| |
| DESCRIPTION |
| ----------- |
| |
| dc(1) is an arbitrary-precision calculator. It uses a stack (reverse Polish |
| notation) to store numbers and results of computations. Arithmetic operations |
| pop arguments off of the stack and push the results. |
| |
| If no expressions or files are given on the command line, as in options below, |
| or for files, as extra command-line arguments, then dc(1) reads from `stdin`. |
| Otherwise, those expressions and files are processed, and dc(1) will then exit. |
| |
| OPTIONS |
| ------- |
| |
| The following are the options that dc(1) accepts. |
| |
| * `-h`, `--help`: |
| Prints a usage message and quits. |
| |
| * `-v`, `-V`, `--version`: |
| Print the version information (copyright header) and exit. |
| |
| * `-x` `--extended-register`: |
| Enables extended register mode. See [REGISTERS](#REGISTERS) for more |
| information. |
| |
| * `-e` *expr*, `--expression`=*expr*: |
| Evaluates `expr`. If multiple expressions are given, they are evaluated in |
| order. If files are given as well (see below), the expressions and files are |
| evaluated in the order given. This means that if a file is given before an |
| expression, the file is read in and evaluated first. |
| |
| * `-f` *file*, `--file`=*file*: |
| Reads in `file` and evaluates it. If expressions are also given (see above), |
| the expressions are evaluated in the order given. |
| |
| SYNTAX |
| ------ |
| |
| `ibase` is a [register][REGISTERS] determining how to interpret constant |
| numbers. It is the "input" base, or the number base used for interpreting input |
| numbers. `ibase` is initially `10`. The max allowable value for `ibase` is `16`. |
| The min allowable value for `ibase` is `2`. |
| |
| `obase` is a [register][REGISTERS] determining how to output results. It is the |
| "output" base, or the number base used for outputting numbers. `obase` is |
| initially `10`. The max allowable value for `obase` is `BC_BASE_MAX`. The min |
| allowable value for `obase` is `2`. |
| |
| The **scale** of an expression is the number of digits in the result of the |
| expression right of the decimal point, and `scale` is a [register][REGISTERS] |
| that sets the precision of any operations (with exceptions). `scale` is |
| initially `0`. `scale` cannot be negative. |
| |
| Each item ([numbers](#NUMBERS) or [commands](#COMMANDS)) in the input source |
| code is processed and executed, in order. Input is processed immediately when |
| entered. |
| |
| ### Comments |
| |
| Comments go from `#` until, and not including, the next newline. This is a |
| **non-portable extension**. |
| |
| NUMBERS |
| ------- |
| |
| Numbers are strings made up of digits, uppercase letters up to `F`, and at most |
| `1` period for a radix. Numbers can have up to `BC_NUM_MAX` digits. Uppercase |
| letters equal `9` + their position in the alphabet (i.e., `A` equals `10`, or |
| `9 + 1`). If a digit or letter makes no sense with the current value of `ibase`, |
| they are set to the value of the highest valid digit in `ibase`. |
| |
| Single-character numbers (i.e., `A`) take the value that they would have if they |
| were valid digits, regardless of the value of `ibase`. This means that `A` |
| always equals decimal `10` and `F` always equals decimal `15`. |
| |
| In addition, if dc(1) was built with the extra math option, it accepts numbers |
| in scientific notation. For dc(1), an example is `1.89237e9`, which is equal to |
| `1892370000`. Negative exponents are also allowed, so `4.2890e_3` is equal to |
| `0.0042890`. |
| |
| **WARNING**: Both the number and the exponent in scientific notation are |
| interpreted according to the current `ibase`, but the number is still multiplied |
| by `10^exponent` regardless of the current `ibase`. For example, if `ibase` is |
| `16` and dc(1) is given the number string `"FFeA"`, the resulting decimal number |
| will be `2550000000000`, and if dc(1) is given the number string `"10e_4"`, the |
| resulting decimal number will be `0.0016`. |
| |
| Scientific notation is a **non-portable extension**. |
| |
| COMMANDS |
| -------- |
| |
| The valid commands are listed below. |
| |
| ### Printing |
| |
| These commands are used for printing. |
| |
| Note that if dc(1) has been built with the extra math option enabled, both |
| scientific notation and engineering notation are available for printing numbers. |
| Scientific notation is activated by assigning `0` to `obase` (in any other |
| context, an `obase` of `0` is invalid), and engineering notation is activated by |
| assigning `1` to `obase` (which is also invalid in any other context). To |
| deactivate them, just assign a different value to `obase`. |
| |
| Printing numbers in scientific notation and/or engineering notation is a |
| **non-portable extension**. |
| |
| * `p`: |
| Prints the value on top of the stack, whether number or string, and prints a |
| newline after. |
| |
| This does not alter the stack. |
| |
| * `n`: |
| Prints the value on top of the tack, whether number or string, and pops it |
| off of the stack. |
| |
| * `P`: |
| Pops a value off the stack. |
| |
| If the value is a number, it is truncated and the result's absolute value is |
| printed as though `obase` is `UCHAR_MAX + 1` and each digit is interpreted |
| as an ASCII character, making it a byte stream. |
| |
| If the value is a string, it is printed without a trailing newline. |
| |
| This is a **non-portable extension**. |
| |
| * `f`: |
| Prints the entire contents of the stack, in order from newest to oldest, |
| without altering anything. |
| |
| Users should use this command when they get lost. |
| |
| ### Arithmetic |
| |
| These are the commands used for arithmetic. |
| |
| * `+`: |
| The top two values are popped off the stack, added, and the result is pushed |
| onto the stack. The result's **scale** is equal to the max **scale** of both |
| operands. |
| |
| * `-`: |
| The top two values are popped off the stack, subtracted, and the result is |
| pushed onto the stack. The result's **scale** is equal to the max **scale** |
| of both operands. |
| |
| * `*`: |
| The top two values are popped off the stack, multiplied, and the result is |
| pushed onto the stack. If `a` is the **scale** of the first expression and |
| `b` is the **scale** of the second expression, the **scale** of the result |
| is equal to `min(a+b,max(scale,a,b))` where `min` and `max` return the |
| obvious values. |
| |
| * `/`: |
| The top two values are popped off the stack, divided, and the result is |
| pushed onto the stack. The result's **scale** is equal to `scale`. |
| |
| * `%`: |
| The top two values are popped off the stack, remaindered, and the result is |
| pushed onto the stack. |
| |
| Remaindering is equivalent to 1) Computing `a/b` to current `scale`, and 2) |
| Using the result of step 1 to calculate `a-(a/b)*b` to **scale** |
| `max(scale + scale(b), scale(a))`. |
| |
| * `~`: |
| The top two values are popped off the stack, divided and remaindered, and |
| the results (divided first, remainder second) are pushed onto the stack. |
| This is equivalent to `x y / x y %` except that `x` and `y` are only |
| evaluated once. |
| |
| This is a **non-portable extension**. |
| |
| * `^`: |
| The top two values are popped off the stack, the second is raised to the |
| power of the first, and the result is pushed onto the stack. |
| |
| The first value popped off the stack must be an integer. |
| |
| * `v`: |
| The top value is popped off the stack, its square root is computed, and the |
| result is pushed onto the stack. The result's **scale** is equal to `scale`. |
| |
| * `_`: |
| If this command *immediately* precedes a number (i.e., no spaces or other |
| commands), then that number is input as a negative number. |
| |
| Otherwise, the top value on the stack is popped and copied, and the copy is |
| negated and pushed onto the stack. This behavior without a number is a |
| non-portable extension. |
| |
| * `b`: |
| The top value is popped off the stack and its absolute value is pushed onto |
| the stack. |
| |
| This is a **non-portable extension**. |
| |
| * `|`: |
| The top three values are popped off the stack, a modular exponentiation is |
| computed, and the result is pushed onto the stack. |
| |
| The first value popped is used as the reduction modulus and must be an |
| integer and non-zero. The second value popped is used as the exponent and |
| must be an integer and non-negative. The third value popped is the base and |
| must be an integer. |
| |
| This is a **non-portable extension**. |
| |
| * `$`: |
| The top value is popped off the stack and copied, and the copy is truncated |
| and pushed onto the stack. |
| |
| This is a **non-portable extension**. |
| |
| * `@`: |
| The top two values are popped off the stack, and the second's precision is |
| set to the value of the first, whether by truncation or extension. |
| |
| The first value must be an integer and non-negative. |
| |
| This is a **non-portable extension**. |
| |
| * `H`: |
| The top two values are popped off the stack, and the second is shifted left |
| (radix shifted right) to the value of the first. |
| |
| The first value must be an integer and non-negative. |
| |
| This is a **non-portable extension**. |
| |
| * `h`: |
| The top two values are popped off the stack, and the second is shifted right |
| (radix shifted left) to the value of the first. |
| |
| The first value must be an integer and non-negative. |
| |
| This is a **non-portable extension**. |
| |
| * `G`: |
| The top two values are popped off of the stack, they are compared, and a `1` |
| is pushed if they are equal, or `0` otherwise. |
| |
| This is a **non-portable extension**. |
| |
| * `N`: |
| The top value is popped off of the stack, and if it a `0`, a `1` is pushed; |
| otherwise, a `0` is pushed. |
| |
| This is a **non-portable extension**. |
| |
| * `(`: |
| The top two values are popped off of the stack, they are compared, and a `1` |
| is pushed if the first is less than the second, or `0` otherwise. |
| |
| This is a **non-portable extension**. |
| |
| * `{`: |
| The top two values are popped off of the stack, they are compared, and a `1` |
| is pushed if the first is less than or equal to the second, or `0` |
| otherwise. |
| |
| This is a **non-portable extension**. |
| |
| * `)`: |
| The top two values are popped off of the stack, they are compared, and a `1` |
| is pushed if the first is greater than the second, or `0` otherwise. |
| |
| This is a **non-portable extension**. |
| |
| * `}`: |
| The top two values are popped off of the stack, they are compared, and a `1` |
| is pushed if the first is greater than or equal to the second, or `0` |
| otherwise. |
| |
| This is a **non-portable extension**. |
| |
| ### Stack Control |
| |
| These commands control the stack. |
| |
| * `c`: |
| Removes all items from ("clears") the stack. |
| |
| * `d`: |
| Copies the item on top of the stack ("duplicates") and pushes the copy onto |
| the stack. |
| |
| * `r`: |
| Swaps ("reverses") the two top items on the stack. |
| |
| * `R`: |
| Pops ("removes") the top value from the stack. |
| |
| ### Register Control |
| |
| These commands control [registers](#REGISTERS). |
| |
| * `s`*r*: |
| Pops the value off the top of the stack and stores it into register `r`. |
| |
| * `l`*r*: |
| Copies the value in register `r` and pushes it onto the stack. This does not |
| alter the contents of `r`. |
| |
| * `S`*r*: |
| Pops the value off the top of the (main) stack and pushes it onto the stack |
| of register `r`. The previous value of the register becomes inaccessible. |
| |
| * `L`*r*: |
| Pops the value off the top of register `r`'s stack and push it onto the main |
| stack. The previous value in register `r`'s stack, if any, is now accessible |
| via the `l`*r* command. |
| |
| ### Parameters |
| |
| These commands control the values of `ibase`, `obase`, and `scale` (see |
| [SYNTAX](#SYNTAX)). |
| |
| * `i`: |
| Pops the value off of the top of the stack and uses it to set `ibase`, which |
| must be between `2` and `16`, inclusive. |
| |
| If the value on top of the stack has any **scale**, the **scale** is |
| ignored. |
| |
| * `o`: |
| Pops the value off of the top of the stack and uses it to set `obase`, which |
| must be between `2` and `BC_BASE_MAX`, inclusive (see bc(1)). |
| |
| If the value on top of the stack has any **scale**, the **scale** is |
| ignored. |
| |
| * `k`: |
| Pops the value off of the top of the stack and uses it to set `scale`, which |
| must be non-negative. |
| |
| If the value on top of the stack has any **scale**, the **scale** is |
| ignored. |
| |
| * `I`: |
| Pushes the current value of `ibase` onto the main stack. |
| |
| * `O`: |
| Pushes the current value of `obase` onto the main stack. |
| |
| * `K`: |
| Pushes the current value of `scale` onto the main stack. |
| |
| ### Strings |
| |
| The following commands control strings. |
| |
| dc(1) can work with both numbers and strings, and [registers](#REGISTERS) can |
| hold both strings and numbers. dc(1) always knows whether a register's contents |
| are a string or a number. |
| |
| While arithmetic operations have to have numbers, and will print an error if |
| given a string, other commands accept strings. |
| |
| Strings can also be executed as macros. For example, if the string `[1pR]` is |
| executed as a macro, then the code `1pR` is executed, meaning that the `1` will |
| be printed with a newline after and then popped from the stack. |
| |
| * `[`*characters*`]`: |
| Makes a string containing `characters` and pushes it onto the stack. |
| |
| If there are brackets (`[` and `]`) in the string, then they must be |
| balanced. Unbalanced brackets can be escaped using a backslash (`\`) |
| character. |
| |
| If there is a backslash character in the string, the character after it |
| (even another backslash) is put into the string verbatim, but the (first) |
| backslash is not. |
| |
| * `a`: |
| The value on top of the stack is popped. |
| |
| If it is a number, it is truncated and its absolute value is taken. The |
| result mod `UCHAR_MAX + 1` is calculated. If that result is `0`, push an |
| empty string; otherwise, push a one-character string where the character is |
| the result of the mod interpreted as an ASCII character. |
| |
| If it is a string, then a new string is made. If the original string is |
| empty, the new string is empty. If it is not, then the first character of |
| the original string is used to create the new string as a one-character |
| string. The new string is then pushed onto the stack. |
| |
| This is a **non-portable extension**. |
| |
| * `x`: |
| Pops a value off of the top of the stack. |
| |
| If it is a number, it is pushed onto the stack. |
| |
| If it is a string, it is executed as a macro. |
| |
| This behavior is the norm whenever a macro is executed, whether by this |
| command or by the conditional execution commands below. |
| |
| * `>`*r*: |
| Pops two values off of the stack that must be numbers and compares them. If |
| the first value is greater than the second, then the contents of register |
| `r` are executed. |
| |
| For example, `0 1>a` will execute the contents of register `a`, and `1 0>a` |
| will not. |
| |
| * `>`*r*`e`*s*: |
| Like the above, but will execute register `s` if the comparison fails. |
| |
| This is a **non-portable extension**. |
| |
| * `!>`*r*: |
| Pops two values off of the stack that must be numbers and compares them. If |
| the first value is not greater than the second (less than or equal to), then |
| the contents of register `r` are executed. |
| |
| * `!>`*r*`e`*s*: |
| Like the above, but will execute register `s` if the comparison fails. |
| |
| This is a **non-portable extension**. |
| |
| * `<`*r*: |
| Pops two values off of the stack that must be numbers and compares them. If |
| the first value is less than the second, then the contents of register `r` |
| are executed. |
| |
| * `<`*r*`e`*s*: |
| Like the above, but will execute register `s` if the comparison fails. |
| |
| This is a **non-portable extension**. |
| |
| * `!<`*r*: |
| Pops two values off of the stack that must be numbers and compares them. If |
| the first value is not less than the second (greater than or equal to), then |
| the contents of register `r` are executed. |
| |
| * `!<`*r*`e`*s*: |
| Like the above, but will execute register `s` if the comparison fails. |
| |
| This is a **non-portable extension**. |
| |
| * `=`*r*: |
| Pops two values off of the stack that must be numbers and compares them. If |
| the first value is equal to the second (greater than or equal to), then the |
| contents of register `r` are executed. |
| |
| * `=`*r*`e`*s*: |
| Like the above, but will execute register `s` if the comparison fails. |
| |
| This is a **non-portable extension**. |
| |
| * `!=`*r*: |
| Pops two values off of the stack that must be numbers and compares them. If |
| the first value is not equal to the second (greater than or equal to), then |
| the contents of register `r` are executed. |
| |
| * `!=`*r*`e`*s*: |
| Like the above, but will execute register `s` if the comparison fails. |
| |
| This is a **non-portable extension**. |
| |
| * `?`: |
| Reads a line from the `stdin` and executes it. This is to allow macros to |
| request input from users. |
| |
| * `q`: |
| During execution of a macro, this exits that macro's execution and the |
| execution of the macro that executed it. If there are no macros, or only one |
| macro executing, dc(1) exits. |
| |
| * `Q`: |
| Pops a value from the stack which must be non-negative and is used the |
| number of macro executions to pop off of the execution stack. If the number |
| of levels to pop is greater than the number of executing macros, dc(1) |
| exits. |
| |
| ### Status |
| |
| These commands query status of the stack or its top value. |
| |
| * `Z`: |
| Pops a value off of the stack. |
| |
| If it is a number, calculates the number of significant decimal digits it |
| has and pushes the result. |
| |
| If it is a string, pushes the number of characters the string has. |
| |
| * `X`: |
| Pops a value off of the stack. |
| |
| If it is a number, pushes the **scale** of the value onto the stack. |
| |
| If it is a string, pushes `0`. |
| |
| * `z`: |
| Pushes the current stack depth (before execution of this command). |
| |
| ### Arrays |
| |
| These commands manipulate arrays. |
| |
| * `:`*r*: |
| Pops the top two values off of the stack. The second value will be stored in |
| the array `r` (see [Registers](#REGISTERS)), indexed by the first value. |
| |
| * `;`*r*: |
| Pops the value on top of the stack and uses it as an index into the array |
| `r`. The selected value is then pushed onto the stack. |
| |
| REGISTERS |
| --------- |
| |
| Registers are names that can store strings, numbers, and arrays. (Number/string |
| registers do not interfere with array registers.) |
| |
| Each register is also its own stack, so the current register value is the top of |
| the register's stack. All registers, when first referenced, have one value (`0`) |
| in their stack. |
| |
| In non-extended register mode, a register name is just the single character that |
| follows any command that needs a register name. The only exception is a newline |
| (`'\n'`); it is a parse error for a newline to be used as a register name. |
| |
| ### Extended Register Mode |
| |
| Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited |
| amounts of registers, if extended register mode is enabled. |
| |
| If extended register mode is enabled (`-x` or `--extended-register` command-line |
| arguments are given), then normal single character registers are used |
| ***unless*** the character immediately following a command that needs a register |
| name is a space (according to `isspace()`) and not a newline (`'\n'`). |
| |
| In that case, the register name is found according to the regex |
| `[a-z][a-z0-9_]*` (like bc(1)), and it is a parse error if the next |
| non-space characters do not match that regex. |
| |
| LIMITS |
| ------ |
| |
| The following are the limits on dc(1): |
| |
| * `DC_BASE_MAX`: |
| The maximum output base. Set at `ULONG_MAX`. |
| |
| * `DC_DIM_MAX`: |
| The maximum size of arrays. Set at `SIZE_MAX-1`. |
| |
| * `DC_SCALE_MAX`: |
| The maximum `scale`. Set at `SIZE_MAX-1`. |
| |
| * `DC_STRING_MAX`: |
| The maximum length of strings. Set at `SIZE_MAX-1`. |
| |
| * `DC_NAME_MAX`: |
| The maximum length of identifiers. Set at `SIZE_MAX-1`. |
| |
| * `DC_NUM_MAX`: |
| The maximum length of a number (in decimal digits), which includes digits |
| after the decimal point. Set at `SIZE_MAX-1`. |
| |
| * Exponent: |
| The maximum allowable exponent (positive or negative). Set at `ULONG_MAX`. |
| |
| * Number of vars: |
| The maximum number of vars/arrays. Set at `SIZE_MAX-1`. |
| |
| Actual values can be queried with the `limits` statement. |
| |
| These limits are meant to be effectively non-existent; the limits are so large |
| (at least on 64-bit machines) that there should not be any point at which they |
| become a problem. In fact, memory should be exhausted before these limits should |
| be hit. |
| |
| ENVIRONMENT VARIABLES |
| --------------------- |
| |
| dc(1) recognizes the following environment variables: |
| |
| * `DC_LINE_LENGTH`: |
| If this environment variable exists and contains an integer that is greater |
| than `1` and is less than `UINT16_MAX` (`2^16-1`), dc(1) will output lines |
| to that length, including the backslash newline combo. The default line |
| length is `70`. |
| |
| EXIT STATUS |
| ----------- |
| |
| dc(1) returns the following exit statuses: |
| |
| * `0`: |
| No error. |
| |
| * `1`: |
| A math error occurred. This follows standard practice of using `1` for |
| expected errors, since math errors will happen in the process of normal |
| execution. |
| |
| Math errors include divide by `0`, taking the square root of a negative |
| number, attempting to convert a negative number to a hardware integer, |
| overflow when converting a number to a hardware integer, and attempting to |
| use a non-integer where an integer is required. |
| |
| Converting to a hardware integer happens for the second operand of the power |
| (`^`), places (`@`), left shift (`H`), and right shift (`h`) operators. |
| |
| * `2`: |
| A parse error occurred. |
| |
| Parse errors include unexpected `EOF`, using an invalid character, failing |
| to find the end of a string or comment, and using a token where it's |
| invalid. |
| |
| * `3`: |
| A runtime error occurred. |
| |
| Runtime errors include assigning an invalid number to `ibase`, `obase`, or |
| `scale`; give a bad expression to a `read()` call, calling `read()` inside |
| of a `read()` call, type errors, and attempting an operation when the stack |
| has too few elements. |
| |
| * `4`: |
| A fatal error occurred. |
| |
| Fatal errors include memory allocation errors, I/O errors, failing to open |
| files, attempting to use files that do not have only ASCII characters (dc(1) |
| only accepts ASCII characters), attempting to open a directory as a file, |
| and giving invalid command-line options. |
| |
| The exit status `4` is special; when a fatal error occurs, dc(1) always exits |
| and returns `4`, no matter what mode dc(1) is in. |
| |
| The other statuses will only be returned when dc(1) is not in interactive mode, |
| since dc(1) resets its state and accepts more input when one of those errors |
| occurs in interactive mode. This is also the case when interactive mode is |
| forced by the `-i` option. |
| |
| These exit statuses allow dc(1) to be used in shell scripting with error |
| checking, and its normal behavior can be forced by using `-i`. |
| |
| SIGNAL HANDLING |
| --------------- |
| |
| If dc(1) has been compiled with the signal handling, sending a `SIGINT` will |
| cause dc(1) to stop execution of the current input and reset, asking for more |
| input. |
| |
| Otherwise, any signals cause dc(1) to exit. |
| |
| COMMAND LINE HISTORY |
| -------------------- |
| |
| dc(1) supports interactive command-line editing, if compiled with the history |
| option enabled. If `stdin` is hooked to a terminal, it is enabled. Previous |
| lines can be recalled and edited with the arrow keys. |
| |
| LOCALES |
| ------- |
| |
| This dc(1) ships with support for adding error messages for different locales. |
| |
| SEE ALSO |
| -------- |
| |
| bc(1) |
| |
| STANDARDS |
| --------- |
| |
| The dc(1) utility operators are compliant with the operators in the bc(1) |
| [IEEE Std 1003.1-2017 (“POSIX.1-2017”)][1] specification. |
| |
| AUTHOR |
| ------ |
| |
| This dc(1) was made from scratch by Gavin D. Howard. |
| |
| BUGS |
| ---- |
| |
| None are known. Report bugs at https://github.com/gavinhoward/bc. |
| |
| [1]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html |