dc
Perform arbitrary-precision arithmetic
TLDR
Start an interactive session
Execute a script
Calculate an expression with the specified scale
Calculate 4 times 5 (4 5 *), subtract 17 (17 -), and [p]rint the output
Specify the number of decimal places to 7 (7 k), calculate 5 divided by -3 (5 _3 /) and [p]rint
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
SYNOPSIS
dc [file ...]
DESCRIPTION
dc (Desk Calculator) is a powerful and versatile reverse-polish notation (RPN) calculator included in Unix-like operating systems. Unlike traditional infix calculators, dc requires operands to be entered before the operator, pushing numbers onto a stack and then applying operations to the top elements. This stack-based approach allows for complex computations without the need for parentheses.
A key feature of dc is its support for arbitrary precision arithmetic, meaning it can handle numbers of any size, limited only by available memory. This makes it suitable for scientific calculations and financial modeling where high precision is crucial.
Beyond basic arithmetic (+, -, *, /), dc offers extensive capabilities including stack manipulation (duplication, swapping), register storage for variables, input/output operations, and macro definition for scripting complex sequences of commands. It operates by reading commands from standard input or specified files, making it highly scriptable and often used as a backend for the bc command, which provides a more familiar infix interface.
CAVEATS
The use of Reverse Polish Notation (RPN) can be counter-intuitive for users accustomed to infix notation, requiring a different way of thinking about calculations.
Debugging complex dc scripts or macros can be challenging due to its non-interactive nature (unless explicitly managed) and the stack-based operations, which can be hard to trace without careful logging.
KEY FEATURES AND OPERATIONS
dc's power comes from its extensive set of single-character commands. These include:
Arithmetic: +, -, *, /, % (modulo), ^ (exponentiation), ~ (division with remainder).
Stack Manipulation: p (print top), n (print and pop), f (print stack), d (duplicate), r (reverse top two).
Registers: sx (store top of stack into register x), lx (load register x onto stack). Registers are single lowercase letters.
Input/Output: ? (read line from stdin), k (set input base), o (set output base), P (print top as ASCII string).
Macros and Control Flow: [string] (define macro), x (execute macro), =x (conditional execution if equal), <x (conditional if less than).
HISTORY
dc is one of the oldest Unix utilities, dating back to the first versions of Unix developed at Bell Labs in the early 1970s. It was created by Robert Morris and Lorinda Cherry. Its arbitrary precision arithmetic capabilities were revolutionary for its time, providing a powerful tool for complex mathematical computations within the Unix environment. It was also designed to serve as the backend for bc, allowing bc to inherit its precision and stack-based power while offering a more conventional infix syntax to users. Its core design has remained remarkably consistent over decades.