dc (computer program)

Original author(s) Robert Morris(AT&T Bell Laboratories) Various open-source and commercial developers B Unix, Unix-like, Plan 9 Cross-platform Command

dc (desk calculator) is a cross-platform reverse-Polish calculator which supports arbitrary-precision arithmetic. Written by Robert Morris while at Bell Labs, it is one of the oldest Unix utilities, preceding even the invention of the C programming language. Like other utilities of that vintage, it has a powerful set of features but terse syntax. Traditionally, the bc calculator program (with infix notation) was implemented on top of dc.

This article provides some examples in an attempt to give a general flavour of the language; for a complete list of commands and syntax, one should consult the man page for one's specific implementation.

History

dc is the oldest surviving Unix language program. When its home Bell Labs received a PDP-11, dc—written in B—was the first language to run on the new computer, even before an assembler. Ken Thompson has opined that dc was the very first program written on the machine.

Basic operations

To multiply four and five in dc (note that most of the whitespace is optional):

$cat << EOF > cal.txt 4 5 * p EOF$ dc cal.txt
20
$The results are also available from the commands:$ echo "4 5 * p" | dc

or

$dc - 4 5*pq 20$ dc
4 5 *
p
20
q

$dc -e '4 5 * p' This translates into "push four and five onto the stack, then, with the multiplication operator, pop two elements from the stack, multiply them and push the result onto the stack." Then the p command is used to examine (print out to the screen) the top element on the stack. The q command quits the invoked instance of dc. Note that numbers must be spaced from each other even as some operators need not be. The arithmetic precision is changed with the command k, which sets the number of fractional digits (the number of digits following the point) to be used for arithmetic operations. Since the default precision is zero, this sequence of commands produces 0 as a result: 2 3 / p By adjusting the precision with k, an arbitrary number of decimal places can be produced. This command sequence outputs .66666. 5 k 2 3 / p To evaluate ${\sqrt {\left(12+\left(-3\right)^{4}\right) \over 11}}-22$ : (v computes the square root of the top of the stack and _ is used to input a negative number): 12 _3 4 ^ + 11 / v 22 - p To swap the top two elements of the stack, use the r command. To duplicate the top element, use the d command. Input/output To read a line from stdin, use the ? command. This evaluates the line as if it were a dc command, and so it is necessary that it be syntactically correct and presents a potential security problem because the ! dc command enables arbitrary command execution. As mentioned above, p prints the top of the stack with a newline after it. n pops the top of the stack and prints it without a trailing newline. f prints the entire stack with one entry per line. dc also supports arbitrary input and output radices. The i command pops the top of the stack and uses it for the input base. Hex digits must be in upper case to avoid collisions with dc commands and are limited to A-F. The o command does the same for the output base, but keep in mind that the input base affects the parsing of every numeric value afterwards so it is usually advisable to set the output base first. Therefore 10o sets the output radix to the current input radix, but generally not to 10 (ten). Nevertheless Ao resets the output base to 10 (ten), regardless of the input base. To read the values, the K, I and O commands push the current precision, input radix and output radix on to the top of the stack. As an example, to convert from hex to binary:$ echo 16i2o DEADBEEFp | dc
11011110101011011011111011101111

Language features

Registers

In addition to these basic arithmetic and stack operations, dc includes support for macros, conditionals and storing of results for later retrieval.

The mechanism underlying macros and conditionals is the register, which in dc is a storage location with a single character name which can be stored to and retrieved from: sc pops the top of the stack and stores it in register c, and lc pushes the value of register c onto the stack. For example:

3 sc 4 lc * p

Registers can also be treated as secondary stacks, so values can be pushed and popped between them and the main stack using the S and L commands.

Strings

String values are enclosed in [ and ] characters and may be pushed onto the stack and stored in registers. The a command converts the low order byte of the numeric value into an ASCII character, or if the top of the stack is a string it replaces it with the first character of the string. There are no ways to build up strings or perform string manipulation other than executing it with the x command, or printing it with the P command.

The # character begins a comment to the end of the line.

Macros

Macros are then implemented by allowing registers and stack entries to be strings as well as numbers. A string can be printed, but it can also be executed (i.e. processed as a sequence of dc commands). So for instance we can store a macro to add one and then multiply by 2 into register m:

[1 + 2 *] sm

and then (using the x command which executes the top of the stack) we can use it like this:

3 lm x p

Conditionals

Finally, we can use this macro mechanism to provide conditionals. The command =r pops two values from the stack, and executes the macro stored in register r only if they are equal. So this prints the string equal only if the top of the stack is equal to 5:

[[equal]p] sm 5 =m

Other conditionals are >, !>, <, !<, !=, which execute the specified macro if the top two values on the stack are greater, less than or equal to ("not greater"), less than, greater than or equal to ("not less than"), and not equals, respectively. Note that the order of the operands in inequality comparisons is the opposite of the order for arithmetic; 5 3 - evaluates to 5 - 3 = 2, but 5 3 <t runs the contents of the t register because 3 < 5.

Loops

Looping is then possible by defining a macro which (conditionally) reinvokes itself. A simple factorial of the top of the stack might be implemented as:

# F(x): return x!
# if x-1 > 1
#     return x * F(x-1)
# otherwise
#     return x
[d1-d1<F*]dsFxp

The 1Q command exits from a macro, allowing an early return. q quits from two levels of macros (and dc itself if there are less than two levels on the call stack). z pushes the current stack depth before the z operation.

Examples

Summing the entire stack

This is implemented with a macro stored in register a which conditionally calls itself, performing an addition each time, until only one value remains on the stack. The z operator is used to push the number of entries in the stack onto the stack. The comparison operator > pops two values off the stack in making the comparison.

dc -e "1 2 4 8 16 100 0d[+2z>a]salaxp"

And the result is 131.

Summing all dc expressions as lines from file

A bare number is a valid dc expression, so this can be used to sum a file where each line contains a single number.

This is again implemented with a macro stored in register a which conditionally calls itself, performing an addition each time, until only one value remains on the stack.

cat file | dc -e "0d[?+2z>a]salaxp"

The ? operator reads another command from the input stream. If the input line contains a decimal number, that value is added to the stack. When the input file reaches end of file, the command is null, and no value is added to the stack.

{ echo "5"; echo "7"; } | dc -e "0d[?+2z>a]salaxp"

And the result is 12.

The input lines can also be complex dc commands.

{ echo "3 5 *"; echo "4 3 *"; echo "5dd++"; } | dc -e "0d[?+2z>a]salaxp"

And the result is 42.

Note that since dc supports arbitrary precision, there is no concern about numeric overflow or loss of precision, no matter how many lines the input stream contains, unlike a similarly concise solution in AWK.

Downsides of this solution are: the loop stops on encountering a blank line in the input stream (technically, any input line which does not add at least one numeric value to the stack); and, for handling negative numbers, leading instances of '-' to denote a negative sign must be change to '_' in the input stream, because of dc's nonstandard negative sign. The ? operator in dc does not provide a clean way to discern reading a blank line from reading end of file.

Unit conversion

As an example of a relatively simple program in dc, this command (in 1 line):

dc -e '[[Enter a number (metres), or 0 to exit]psj]sh[q]sz[lhx?d0=z10k39.370079*.5+0k12~1/rn[ feet ]Pn[ inches]P10Pdx]dx'

converts distances from metres to feet and inches; the bulk of it is concerned with prompting for input, printing output in a suitable format and looping around to convert another number.

Greatest common divisor

As an example, here is an implementation of the Euclidean algorithm to find the GCD:

dc -e '??[dSarLa%d0<a]dsax+p'                  # shortest
dc -e '[a=]P?[b=]P?[dSarLa%d0<a]dsax+[GCD:]Pp' # easier-to-read version

Factorial

Computing the factorial of an input value, $n!=\prod _{i=1}^{n}i$ dc -e '?[q]sQ[d1=Qd1-lFx*]dsFxp'

Quines in dc

There exist also quines in the programming language dc; programs that produce its source code as output.

dc -e '[91Pn[dx]93Pn]dx'

dc -e '[91PP93P[dx]P]dx'

Printing all prime numbers

echo '2p3p[dl!d2+s!%0=@l!l^!<#]s#[s/0ds^]s@[p]s&[ddvs^3s!l#x0<&2+l.x]ds.x' | dc

This program was written by Michel Charpentier. It outputs the sequence of prime numbers. Note that it can be shortened by one symbol, which seems to be the minimal solution.

echo '2p3p[dl!d2+s!%0=@l!l^!<#]s#[0*ds^]s@[p]s&[ddvs^3s!l#x0<&2+l.x]ds.x' | dc

Integer factorization

dc -e '[n=]P?[p]s2[lip/dli%0=1dvsr]s12sid2%0=13sidvsr[dli%0=1lrli2+dsi!>.]ds.xd1<2'

This program was also written by Michel Charpentier.

There is a shorter

dc -e "[n=]P?[lfp/dlf%0=Fdvsr]sF[dsf]sJdvsr2sf[dlf%0=Flfdd2%+1+sflr<Jd1<M]dsMx"

and a faster solution (try with the 200-bit number 2200-1 (input 2 200^1-)

dc -e "[n=]P?[lfp/dlf% 0=Fdvsr]sFdvsr2sfd2%0=F3sfd3%0=F5sf[dlf%0=Flfd4+sflr>M]sN[dlf%0=Flfd2+sflr>N]dsMx[p]sMd1<M"

Note that the latter can be sped up even more, if the access to a constant is replaced by a register access.

dc -e "[n=]P?[lfp/dlf%l0=Fdvsr]sF2s2dvsr2sf4s4d2%0=F3sfd3%0=F5sf[dlf%l0=Flfdl4+sflr>M]sN[dlf%l0=Flfdl2+sflr>N]dsMx[p]sMd1<M"

Diffie–Hellman key exchange

A more complex example of dc use embedded in a Perl script performs a Diffie–Hellman key exchange. This was popular as a signature block among cypherpunks during the ITAR debates, where the short script could be run with only Perl and dc, ubiquitous programs on Unix-like operating systems:

#!/usr/bin/perl -- -export-a-crypto-system-sig Diffie-Hellman-2-lines
($g,$e, $m) = @ARGV,$m || die "$0 gen exp mod\n"; print echo "16dio1[d2%Sa2/d0<X+d*La1=z\U$m%0]SX$e"[$g*]\EszlXx+p | dc

A commented version is slightly easier to understand and shows how to use loops, conditionals, and the q command to return from a macro. With the GNU version of dc, the | command can be used to do arbitrary precision modular exponentiation without needing to write the X function.

#!/usr/bin/perl

my ($g,$e, $m) = map { "\U$_" } @ARGV;
die "$0 gen exp mod\n" unless$m;

print echo $g$e \$m | dc -e '
# Hex input and output
16dio
# Read m, e and g from stdin on one line
?SmSeSg

# Function z: return g * top of stack
[lg*]sz

# Function Q: remove the top of the stack and return 1
[sb1q]sQ

# Function X(e): recursively compute g^e % m
# It is the same as Sm^Lm%, but handles arbitrarily large exponents.
# Stack at entry: e
# Stack at exit: g^e % m
# Since e may be very large, this uses the property that g^e % m ==
#     if( e == 0 )
#         return 1
#     x = (g^(e/2)) ^ 2
#     if( e % 2 == 1 )
#         x *= g
#     return x %
[
d 0=Q   # return 1 if e==0 (otherwise, stack: e)
d 2% Sa # Store e%2 in a (stack: e)
2/      # compute e/2
lXx     # call X(e/2)
d*      # compute X(e/2)^2
La1=z   # multiply by g if e%2==1
lm %    # compute (g^e) % m
] SX

le          # Load e from the register
lXx         # compute g^e % m
p           # Print the result
';