Desk calculator.
Syntax dc [-cChiPRvVx] [--version] [--help] [--digit-clamp] [--no-digit-clamp] [--interactive] [--no-prompt] [--no-read-prompt] [--extended-register] [-e expr] [--expression=expr...] [-f file...] [--file=file...] [file...] [-I ibase] [--ibase=ibase] [-O obase] [--obase=obase] [-S scale] [--scale=scale] [-E seed] [--seed=seed] Options -C, --no-digit-clamp Disable clamping of digits greater than or equal to the current ibase when parsing numbers. This means that the value added to a number from a digit is always that digit’s value multiplied by the value of ibase raised to the power of the digit’s position, which starts from 0 at the least significant digit. If this and/or the -c or --digit-clamp options are given multiple times, the last one given is used. This option overrides the DC_DIGIT_CLAMP environment variable (see the ENVIRONMENT VARIABLES section) and the default, which can be queried with the -h or --help options. This is a non-portable extension. -c, --digit-clamp Enable clamping of digits greater than or equal to the current ibase when parsing numbers. This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit’s position, which starts from 0 at the least significant digit. If this and/or the -C or --no-digit-clamp options are given multiple times, the last one given is used. This option overrides the DC_DIGIT_CLAMP environment variable (see the ENVIRONMENT VARIABLES section) and the default, which can be queried with the -h or --help options. This is a non-portable extension. -E seed, --seed=seed Set the builtin variable seed to the value seed assume that seed is in base 10. It is a fatal error if seed is not a valid number. If multiple instances of this option are given, the last is used. This is a non-portable extension. -e expr, --expression=expr Evaluate expr as DC commands. 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. If this option is given on the command-line (i.e., not in DC_ENV_ARGS, see the ENVIRONMENT VARIABLES section), then after processing all expressions and files, dc(1) will exit, unless - (stdin) was given as an argument at least once to -f or --file, whether on the command-line or in DC_ENV_ARGS. However, if any other -e, --expression, -f, or --file arguments are given after -f- or equivalent is given, dc(1) will give a fatal error and exit. This is a non-portable extension. -f file, --file=file Read in file and evaluate dc commands from it, line by line, as though it were read through stdin. If expressions are also given (see above), the expressions are evaluated in the order given. If this option is given on the command-line (i.e., not in DC_ENV_ARGS, see the ENVIRONMENT VARIABLES section), then after processing all expressions and files, dc(1) will exit, unless - (stdin) was given as an argument at least once to -f or --file. However, if any other -e, --expression, -f, or --file arguments are given after -f- or equivalent is given, dc(1) will give a fatal error and exit. This is a non-portable extension. -h, --help Print a usage message summarising these command-line options, then exit. -I ibase, --ibase=ibase Set the builtin variable ibase to the value ibase assuming that ibase is in base 10. It is a fatal error if ibase is not a valid number. If multiple instances of this option are given, the last is used. This is a non-portable extension. -i, --interactive Force interactive mode. (See INTERACTIVE MODE.) This is a non-portable extension. -L, --no-line-length Disable line length checking and print numbers without backslashes and newlines. In other words, this option sets BC_LINE_LENGTH to 0 (see the ENVIRONMENT VARIABLES section). This is a non-portable extension. -O obase, --obase=obase Set the builtin variable obase to the value obase assuming that obase is in base 10. It is a fatal error if obase is not a valid number. If multiple instances of this option are given, the last is used. This is a non-portable extension. -P, --no-prompt Disable the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the TTY MODE section.) This is mostly for those users that do not want a prompt or are not used to having them in dc(1). Most of those users would want to put this option in DC_ENV_ARGS. These options override the DC_PROMPT and DC_TTY_MODE environment variables (see the ENVIRONMENT VARIABLES section). This is a non-portable extension. -R, --no-read-prompt Disable the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the TTY MODE section.) This is mostly for those users that do not want a read prompt or are not used to having them in dc(1). Most of those users would want to put this option in BC_ENV_ARGS (see the ENVIRONMENT VARIABLES section). This option is also useful in hash bang lines of dc(1) scripts that prompt for user input. This option does not disable the regular prompt because the read prompt is only used when the ? command is used. These options do override the DC_PROMPT and DC_TTY_MODE environment variables (see the ENVIRONMENT VARIABLES section), but only for the read prompt. This is a non-portable extension. -S scale, --scale=scale Set the builtin variable scale to the value scale assuming that scale is in base 10. It is a fatal error if scale is not a valid number. If multiple instances of this option are given, the last is used. This is a non-portable extension. -v, -V, --version Print the version information (copyright header) and exit. -x --extended-register Enable extended register mode. See the Extended Register Mode subsection of the REGISTERS section for more. This is a non-portable extension. -z, --leading-zeroes Make dc(1) print all numbers greater than -1 and less than 1, and not equal to 0, with a leading zero. This is a non-portable extension. All long options are non-portable extensions. To exit, use 'q' or 'quit'.
dc is a reverse-polish desk calculator which supports unlimited precision arithmetic. It also allows you to define and call macros.
Normally dc reads from the standard input; if any command arguments are given to it, they are filenames, and dc reads and executes the contents of the files before reading from standard input.
To enter a number in dc, type the digits (using upper case letters A through F as "digits" when working with input bases greater than ten), with an optional decimal point. Exponential notation is not supported.
To enter a negative number, begin the number with '_'. '-' cannot be used for this, as it is a binary operator for subtraction instead.
Numbers can have up to DC_NUM_MAX digits. Uppercase letters are equal to 9 plus their position in the alphabet (i.e., A equals 10, or 9+1).
To enter two numbers in succession, separate them with spaces or newlines. These have no meaning as commands.
A reverse-polish calculator stores numbers on a stack. Entering a number pushes it on the stack. Arithmetic operations pop arguments off the stack and push the results.
Support for Binary, Decimal, Octal and Hex.
ibase is a register (see REGISTERS) that determines 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. The max allowable value for ibase can be queried in dc(1) programs with the T command.
obase is a register (see REGISTERS) that determines 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 DC_BASE_MAX and can be queried with the U command. The min allowable value for obase is 0. If obase is 0, values are output in scientific notation, and if obase is 1, values are output in engineering notation. Otherwise, values are output in the specified base.
Outputting in scientific and engineering notations are non-portable extensions.
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 (see REGISTERS) that sets the precision of any operations (with exceptions). scale is initially 0. scale cannot be negative. The max allowable value for scale can be queried in dc(1) programs with the V command.
seed is a register containing the current seed for the pseudo-random number generator. If the current value of seed is queried and stored, then if it is assigned to seed later, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers that were generated after the value of seed was first queried.
Multiple values assigned to seed can produce the same sequence of pseudo-random numbers. Likewise, when a value is assigned to seed, it is not guaranteed that querying seed immediately after will return the same value. In addition, the value of seed will change after any sucessfull call to the ’ command or the “ command. The maximum integer returned by the ’ command can be queried with the W command.
The pseudo-random number generator, seed, and all associated operations are non-portable extensions.
All normal output is to standard output; all error output is to standard error.
Comments go from # until, and not including, the next newline. This is a non-portable extension.
The valid commands are listed below.
These commands are used for printing.
p Print the value on the top of the stack, without altering the stack. A newline is printed after the value. n Print the value on the top of the stack, popping it off, no newline after. P Pop off the value on top of the stack. If it it a string, it is simply printed without a trailing newline. Otherwise it is a number, and the integer portion of its absolute value is printed out as a "base (UCHAR_MAX+1)" byte stream. Assuming that (UCHAR_MAX+1) is 256 (as it is on most machines with 8-bit bytes), the sequence KSK0k1/_1Ss [ls*]Sxd0>x [256~Ssd0<x]dsxxsx[q]Sq[Lsd0>qaPlxx] dsxxsx0sqLqsxLxLK+k could also accomplish this function. (Much of the complexity of the above native-dc code is due to the ~ computing the characters backwards, and the desire to ensure that all registers wind up back in their original states.) f Print the entire contents of the stack without altering anything. This is a useful if you are lost or want to figure out what the effect of a command has been.
dc works with postfix notation; like many HP Calculators.
Basic arithmetic uses the standard + - / * symbols but entered after the digitsso entering:
100
0.5
*
p will return 50
+ Pop two values off the stack, add them, and push the result. The precision of the result is determined only by the values of the arguments, equal to the max scale of both operands; enough to be exact. - Pop two values, subtract the first one popped from the second, and push the result. The scale of the result is equal to the max scale of both operands. * Pop two values, multiply them, and push the result. 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. / Pop two values, divide the second one popped from the first one popped, and push the result. The number of fraction digits (scale) is specified by the precision value (scale). % Pop two values, compute the remainder of the division that the / command would do, and push that. 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 first value popped off of the stack must be non-zero. ~ Pop two values, divide the second one popped from the first. The quotient is pushed first, and the remainder is pushed next. The number of fraction digits used in the division is specified by the precision value. This is equivalent to x y / x y % except that x and y are only evaluated once. The first value popped off of the stack must be non-zero. This is a non-portable extension. ^ Pop two values and exponentiate, using the first value popped as the exponent and the second popped as the base. The fraction part of the exponent is ignored. The precision value is the number of fraction digits in the result. | Pop three values and compute a modular exponentiation. The first value popped is used as the reduction modulus; this value must be a non-zero number, and should be an integer. The second popped is used as the exponent; this value must be a non-negative number, and any fractional part of this exponent will be ignored. The third value popped is the base which gets exponentiated, which should be an integer. For small integers this is like the sequence Sm^Lm%, but, unlike ^, this command will work with arbitrarily large exponents. This is a non-portable extension. v Pop one value, compute its square root, and push that. The precision value specifies the number of fraction digits in the result. Most arithmetic operations are affected by the 'precision value', which you can set with the k command. The default precision value is zero, which means that all arithmetic except for addition and subtraction produces integer results. The value popped off of the stack must be non-negative. _ 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 if it is zero, it is pushed back onto the stack. Otherwise, its absolute value is pushed onto the stack. 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 precision of the second is set to the value of the first, whether by truncation or extension. The first value popped off of the stack 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 popped off of the stack 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 popped off of the stack 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. M The top two values are popped off of the stack. If they are both non-zero, a 1 is pushed onto the stack. If either of them is zero, or both of them are, then a 0 is pushed onto the stack. This is a non-portable extension. This is like the && operator in bc(1), and it is not a short-circuit operator. m The top two values are popped off of the stack. If at least one of them is non-zero, a 1 is pushed onto the stack. If both of them are zero, then a 0 is pushed onto the stack. This is a non-portable extension. This is like the || operator in bc(1), and it is not a short-circuit operator.
dc has a built-in pseudo-random number generator. These commands query the pseudo-random number generator. (See Parameters for more information about the seed value that controls the pseudo-random number generator.)
The pseudo-random number generator is guaranteed to NOT be cryptographically secure.
’ Generates an integer between 0 and DC_RAND_MAX, inclusive (see the LIMITS section). The generated integer is made as unbiased as possible, subject to the limitations of the pseudo-random number generator. This is a non-portable extension. “ Pops a value off of the stack, which is used as an exclusive upper bound on the integer that will be generated. If the bound is negative or is a non-integer, an error is raised, and dc(1) resets (see RESET) while seed remains unchanged. If the bound is larger than DC_RAND_MAX, the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of DC_RAND_MAX+1, and adding them together. Thus, the size of integer that can be generated with this command is unbounded. Using this command will change the value of seed, unless the operand is 0 or 1. In that case, 0 is pushed onto the stack, and seed is not changed.
The generated integer is made as unbiased as possible, subject to the limitations of the pseudo-random number generator.
This is a non-portable extension.
c Clear the stack, rendering it empty. d Duplicate the value on the top of the stack, pushing another copy of it. Thus, '4d*p' computes 4 squared and prints it. r Reverse the order of (swaps) the top two values on the stack. (This can also be accomplished with the sequence SaSbLaLb.) R Pops (“removes”) the top value from the stack.
These commands control the values of ibase (or input radix), obase (or output radix), scale (or precision), and seed. Also see the full definitions above.
The scale specifies the number of fraction digits to keep in the result of most arithmetic operations. The input radix controls the interpretation of numbers typed in; all numbers typed in use this radix. The output radix is used for printing numbers.
The scale must be zero or greater. The precision is always measured in decimal digits, regardless of the current input or output radix.
i Pop a value off the top of the stack and use it to set ibase (the input radix), which must be between 2 and 16, inclusive. If the value on top of the stack has any scale, the scale is ignored. o Pop a value off the top of the stack and use it to set obase (the output radix), which must be between 0 and DC_BASE_MAX, inclusive (see the LIMITS section and the NUMBERS section). If the value on top of the stack has any scale, the scale is ignored. k Pop a value off the top of the stack and use it to set the scale (precision), which must be non-negative. If the value on top of the stack has any scale, the scale is ignored. I Push the current ibase (input radix) onto the main stack. O Push the current obase (output radix) onto the main stack. K Push the current scale (precision) onto the main stack. J Push the current value of seed onto the main stack. This is a non-portable extension. T Push the maximum allowable value of ibase onto the main stack. This is a non-portable extension. U Push the maximum allowable value of obase onto the main stack. This is a non-portable extension. V Push the maximum allowable value of scale onto the main stack. This is a non-portable extension. W Push the maximum (inclusive) integer that can be generated with the ’ pseudo-random number generator command. This is a non-portable extension.
dc can work with both numbers and strings, and registers (see REGISTERS) can hold both strings and numbers. dc always knows whether the contents of a register 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.
The following commands control strings. [characters] Make a string containing characters (contained between balanced [ and ] characters), and push it on 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 256 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 Pop a value off the stack and executes it as a macro. Normally it should be a string; if it is a number, it is simply pushed back onto the stack. For example, [1p]x executes the macro 1p which pushes 1 on the stack and prints 1 on a separate line. Macros are most often stored in registers; [1p]sa stores a macro to print 1 into register a, and lax invokes this macro. >r Pop two values off the stack and compare them assuming they are numbers, executing the contents of register r as a macro if the original top-of-stack is greater. Thus, 1 2>a will invoke register a’s contents and 2 1>a will not. >res Like the above, but will execute register s if the comparison fails. If either or both of the values are not numbers, dc(1) will raise an error and reset (see RESET). This is a non-portable extension. !>r Pop 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. If either or both of the values are not numbers, dc(1) will raise an error and reset (see RESET). !>res Like the above, but will execute register s if the comparison fails. If either or both of the values are not numbers, dc(1) will raise an error and reset (see RESET). This is a non-portable extension. <r Pop 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. If either or both of the values are not numbers, dc(1) will raise an error and reset (see RESET). <res Like the above, but will execute register s if the comparison fails. If either or both of the values are not numbers, dc(1) will raise an error and reset (see RESET). This is a non-portable extension. !<r Pop two values off of the stack that must be numbers and compare them. If the first value is not less than the second (greater than or equal to), then the contents of register r are executed. If either or both of the values are not numbers, dc(1) will raise an error and reset (see RESET). !<res Like the above, but will execute register s if the comparison fails. If either or both of the values are not numbers, dc(1) will raise an error and reset (see RESET). This is a non-portable extension. =r Pop two values off of the stack that must be numbers and compare them. If the first value is equal to the second, then the contents of register r are executed. If either or both of the values are not numbers, dc(1) will raise an error and reset (see RESET). =res Like the above, but will execute register s if the comparison fails. If either or both of the values are not numbers, dc(1) will raise an error and reset (see RESET). This is a non-portable extension. !=r Pop two values off of the stack that must be numbers and compare them. If the first value is not equal to the second, then the contents of register r are executed. If either or both of the values are not numbers, dc(1) will raise an error and reset (see RESET). !=res Like the above, but will execute register s if the comparison fails. If either or both of the values are not numbers, dc(1) will raise an error and reset (see RESET). This is a non-portable extension. ? Read a line from the terminal and execute it. This command allows a macro to request input from the user. q exit from a macro and also from the macro which invoked it. If there are no macros, or only one macro executing, dc exits. Q Pop a value off the stack and uses it as a count of levels of macro execution to be exited. Thus, 3Q exits three levels. The Q command will never cause dc to exit. The value must be non-negative. , Push the depth of the execution stack onto the stack. The execution stack is the stack of string executions. The number that is pushed onto the stack is exactly as many as is needed to make dc(1) exit with the Q command, so the sequence ,Q will make dc(1) exit. This is a non-portable extension.
Z Pop a value off the stack. If it is a number, it will calculate the number of significant decimal digits and push the result. It will push 1 if the argument is 0 with no decimal places. If it is a string, it will push the number of characters the string has. X Pop a value off the stack. If it is a number, it will push the scale of the value onto the stack. If it is a string, it will push 0. u Pop one value off of the stack. If the value is a number, this pushes 1 onto the stack. Otherwise (if it is a string), it pushes 0. This is a non-portable extension. t Pop one value off of the stack. If the value is a string, this pushes 1 onto the stack. Otherwise (if it is a number), it pushes 0. This is a non-portable extension. z Push the current stack depth: the number of objects on the stack before the execution of the z command. yr Push the current stack depth of the register r onto the main stack. Because each register has a depth of 1 (with the value 0 in the top item) when dc(1) starts, dc(1) requires that each register’s stack must always have at least one item; dc(1) will give an error and reset otherwise (see RESET). This means that this command will never push 0. This is a non-portable extension.
These commands manipulate arrays.
:r Pop the top two values off of the stack. The second value will be stored in the array r (see REGISTERS), indexed by the first value. ;r Pop 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. Yr Push the length of the array r onto the stack. This is a non-portable extension.
These commands retrieve global settings. These are the only commands that require multiple specific characters, and all of them begin with the letter g. Only the characters below are allowed after the character g; any other character produces a parse error (see ERRORS).
gl Push the line length set by DC_LINE_LENGTH (see the ENVIRONMENT VARIABLES section) onto the stack. gx Push 1 onto the stack if extended register mode is on, 0 otherwise. See the Extended Register Mode subsection of the REGISTERS section for more information. gz Push 0 onto the stack if the leading zero setting has not been enabled with the -z or --leading-zeroes options (see OPTIONS), non-zero otherwise.
Registers are names that can store strings, numbers, and arrays. (Number/string registers do not interfere with array registers.)
dc provides at least 256 memory registers, each named by a single character.
You can store a number or a string in a register and retrieve it later.Each register is also its own stack, so the current register value is the top of the stack for the register. All registers, when first referenced, have one value (0) in their stack, and it is a runtime error to attempt to pop that item off of the register 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 exceptions are: a newline ('\n') and a left bracket ('['); it is a parse error for a newline or a left bracket 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) identifiers), and it is a parse error if the next non-space characters do not match that regex.
sr Pop the value off the top of the stack and store it into register r. lr Copy the value in register r and push it onto the stack. This does not alter the contents of r. Each register also contains its own stack. The current register value is the top of the register’s stack. Sr Pop the value off the top of the (main) stack and push it onto the stack of register r. The previous value of the register becomes inaccessible. Lr Pop 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 lr command.
When dc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen.
First, any macros that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all macros returned) is skipped.
Thus, when dc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see EXIT STATUS), it asks for more input; otherwise, it exits with the appropriate return code.
Most dc(1) implementations use char types to calculate the value of 1 decimal digit at a time, but that can be slow. This dc(1) does something different. It uses large integers to calculate more than 1 decimal digit at a time.
If built in a environment where DC_LONG_BIT (see the LIMITS section) is 64, then each integer has 9 decimal digits.
If built in an environment where DC_LONG_BIT is 32 then each integer has 4 decimal digits.
This value (the number of decimal digits per large integer) is called DC_BASE_DIGS.In addition, this dc(1) uses an even larger integer for overflow checking. This integer type depends on the value of DC_LONG_BIT, but is always at least twice as large as the integer type used to store digits.
If no files are given on the command-line and no files or expressions are given by the -f, --file, -e, or --expression options, then dc(1) reads from stdin.
However, there is a caveat to this. First, stdin is evaluated a line at a time. The only exception to this is if a string has been finished, but not ended. This means that, except for escaped brackets, all brackets must be balanced before dc(1) parses and executes.
Any non-error output is written to stdout. In addition, if history (see the HISTORY section) and the prompt (see the TTY MODE section) are enabled, both are output to stdout.
Note: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see EXIT STATUS) if it cannot write to stdout, so if stdout is closed, as in dc >&-, it will quit with an error. This is done so that dc(1) can report problems when stdout is redirected to a file.
If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect stdout to /dev/null.
Any error output is written to stderr.
Note: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see EXIT STATUS) if it cannot write to stderr, so if stderr is closed, as in dc 2>&-, it will quit
with an error. This is done so that dc(1) can exit with an error code when stderr is redirected to a file.If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect stderr to /dev/null.
Like bc(1), dc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both stdin and stdout are hooked to a terminal, but the -i flag and --interactive option can turn it on in other situations.
In interactive mode, dc(1) attempts to recover from errors (see RESET), and in normal execution, flushes stdout as soon as execution is done for the current input. dc(1) may also reset on SIGINT instead of exit, depending on the contents of, or default for, the DC_SIGINT_RESET environment variable (see the ENVIRONMENT VARIABLES section).
! Will run the rest of the line as a system command. Note that parsing of the !<, !=, and !> commands take precedence, so if you want to run a command starting with <, =, or > you will need to add a space after the !.
dc 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, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, 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 is invalid. 3 A runtime error occurred. Runtime errors include assigning an invalid number to any global (ibase, obase, or scale), giving a bad expression to a read() call, calling read() inside of a read() call, type errors (including attempting to execute a number), 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.
People: dc was written by Ken Thompson, Bob Morris and Lorinda Cherry [×]
100
0.5
*
p
will return 50Calculate a very large number correctly: (10100) + 1 - (10100) = 0
echo '10 100 ^ 1 + 10 100 ^ - p' | dc
will return 1
“The best way to destroy the capitalist system is to debauch the currency” ~ John Keynes
Local man page: dc - Command line help page on your local machine.
bc - Arbitrary precision calculator language.
cal - Display a calendar.
expr - Evaluate expressions.
units - Convert units from one scale to another.
wc - Print byte, word, and line counts.