LinuxCommandLibrary

dc

Perform arbitrary-precision arithmetic

TLDR

Start an interactive session

$ dc
copy

Execute a script
$ dc [path/to/script.dc]
copy

Calculate an expression with the specified scale
$ dc [[-e|--expression]] '[10] k [5 3 /] p'
copy

Calculate 4 times 5 (4 5 *), subtract 17 (17 -), and [p]rint the output
$ dc [[-e|--expression]] '4 5 * 17 - p'
copy

Specify the number of decimal places to 7 (7 k), calculate 5 divided by -3 (5 _3 /) and [p]rint
$ dc [[-e|--expression]] '7 k 5 _3 / p'
copy

Calculate the golden ratio, phi: set number of decimal places to 100 (100 k), square root of 5 (5 v) plus 1 (1 +), divided by 2 (2 /), and [p]rint result
$ dc [[-e|--expression]] '100 k 5 v 1 + 2 / p'
copy

SYNOPSIS

dc [file]

PARAMETERS

file
    Read and execute commands from file after reading from standard input.

DESCRIPTION

dc is a reverse Polish notation calculator that supports arbitrary-precision integer arithmetic. It can also handle fractions, exponents, and some programming constructs like macros and conditional execution.

dc reads from standard input or a specified file, evaluates expressions, and prints results to standard output. It's particularly useful for performing calculations that exceed the capabilities of standard calculators or shell arithmetic, and for scripting complex numerical computations where precise control over precision is necessary.

While powerful, dc has a relatively steep learning curve due to its reverse Polish notation (RPN) syntax where operators follow their operands. bc (a preprocessor for dc) provides a more familiar infix notation.

CAVEATS

dc's reverse Polish notation can be confusing for users accustomed to infix notation. Error messages are often cryptic. It lacks built-in functions like trigonometric functions.

BASIC OPERATIONS

Numbers are pushed onto a stack. Operators act on the top one or two stack entries. '+' adds, '-' subtracts, '*' multiplies, '/' divides, '%' performs modulo operation, '^' exponentiates. 'p' prints the top of the stack without removing it. 'f' prints the entire stack. 'q' quits.

PRECISION

The precision (number of digits after the decimal point) can be controlled using the 'k' command. For example, '3 k' sets the precision to 3 decimal places. All subsequent calculations will be performed with this precision.

REGISTERS

dc provides registers for storing and retrieving values. 'sa' stores the top of the stack in register 'a', and 'la' loads the value from register 'a' onto the stack. Registers can be named with any character.

HISTORY

dc is one of the oldest Unix utilities, dating back to the first versions of the operating system. It was created by Lorinda Cherry and Robert Morris at Bell Labs. Its longevity is due to its simplicity, portability, and ability to perform arbitrary-precision arithmetic, making it suitable for a wide range of tasks.

Originally written in B, it was ported to C soon after the C programming language was invented. Its usage has declined with the advent of more user-friendly calculators and programming languages, but remains in use for specialized applications and scripting due to its availability on virtually all Unix-like systems.

SEE ALSO

bc(1)

Copied to clipboard