[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]

Commands Reference, Volume 2


dc Command

Purpose

Provides an interactive desk calculator for doing arbitrary-precision integer arithmetic.

Syntax

dc [ File]

Description

The dc command is an arbitrary-precision arithmetic calculator. The dc command takes its input from the File parameter or standard input until it reads an end-of-file character. Once the dc command receives the input, it evaluates the value and writes the evaluation to standard output. It operates on decimal integers, but you can specify an input base, an output base, and a number of fractional digits to be maintained. The dc command is structured as a stacking, reverse Polish notation calculation.

The bc command is a preprocessor for the dc command. It provides infix notation and a syntax similar to the C language, which implements functions and control structures for programs.

Subcommands


c Cleans the stack: the dc command pops all values on the stack.
d Duplicates the top value on the stack.
f Displays all values on the stack.
i Pops the top value on the stack and uses that value as the number radix for further input.
I Pushes the input base on the top of the stack.
k Pops the top of the stack and uses that value as a nonnegative scale factor. The appropriate number of places is displayed on output and is maintained during multiplication, division, and exponentiation. The interaction of scale factor, input base, and output base is reasonable if all are changed together.
lx Pushes the value in the register represented by the x variable on the stack. The register represented by the x variable is not changed. All registers start with a value of 0.
Lx Treats the x variable as a stack and pops its top value onto the main stack.
o Pops the top value on the stack and uses that value as the number radix for further output.
O Pushes the output base on the top of the stack.
p Displays the top value on the stack. The top value remains unchanged.
P Interprets the top of the stack as a string, removes it, and displays it.
q Exits the program. If the dc command is running a string, it pops the recursion level by two.
Q Pops the top value on the stack and on the string execution level by that value.
sx Pops the top of the stack and stores it in a register named x, where the x variable can be any character.
Sx Treats the x variable as a stack. It pops the top of the main stack and pushes that value onto the stack represented by the x variable.
v Replaces the top element on the stack by its square root. Any existing fractional part of the option is taken into account, but otherwise, the scale factor is ignored.
x Treats the top element of the stack as a character string and runs it as a string of dc commands.
X Replaces the number on the top of the stack with its scale factor.
z Pushes the number of elements in the stack onto the stack.
Z Replaces the top number in the stack with the number of digits in that number.
Number Pushes the specified value onto the stack. A Number is an unbroken string of the digits 0 through 9. To specify a negative number, precede it with _ (underscore). A number may contain a decimal point.
+ - / * % ^ Adds (+), subtracts (-), multiplies (*), divides (/), remainders (%), or exponentiates (^ ) the top two values on the stack. The dc command pops the top two entries off the stack and pushes the result on the stack in their place. The dc command ignores fractional parts of an exponent.
[String] Puts the bracketed String parameter onto the top of the stack.
[= | > | < ] x
                          Pops the top two elements of the stack and compares them. Evaluates the register represented by the x variable as if it obeys the stated relation.
! Interprets the rest of the line as an operating system command.
? Gets and runs a line of input.
;: The bc command uses these characters for array operations.

Examples

  1. To use the dc command as a calculator, enter:

    You:    1 4 / p
    System: 0
    You:    1 k   [ Keep 1 decimal place ]s.
            1 4 / p
    System: 0.2
    You:    3 k   [ Keep 3 decimal places ]s.
            1 4 / p
    System: 0.250
    You:    16 63 5 / + p
    System: 28.600
    You:    16 63 5 + / p
    System: 0.235
    

    Comments may be used in the dc command as in the example. Comments are enclosed in brackets and may be followed by s. ( [ Comment] s. ) is ignored by the dc command. Comments enclosed in brackets only are stored on the top of the stack.

    When you enter the dc command expressions directly from the keyboard, press Ctrl-D to end the bc command session and return to the shell command line.

  2. To load and run a dc program file, enter:

    You:    dc prog.dc
            5 lf x p [ 5 factorial ]s.
    System: 120
    You:    10 lf x p [ 10 factorial ]s.
    System: 3628800
    

    This entry interprets the dc program saved in the prog.dc program file, then reads from the workstation keyboard. The lf x evaluates the function stored in register f, which could be defined in the prog.c program file as:

    [ f: compute the factorial of n ]s.
    [ (n = the top of the stack) ]s.
    [ If 1>n do b; If 1<n do r ]s.
     [d 1 >b d 1 <r] sf
    [ Return f(n) = 1 ]s.
     [d - 1 +] sb
    [ Return f(n) = n * f(n-1) ]s.
     [d 1 - lf x *] sr
    

    You can create dc program files with any text editor or with the -c (compile) flag of the bc command. When you enter the dc command expressions directly from the keyboard, press Ctrl-D to end the bc command session and return to the shell command line.

Files


/usr/bin/dc Contains the dc command.

Related Information

The bc command.


[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]