dc: The Arbitrary Precision Reverse Polish Notation Calculator
dc
is a powerful command-line utility that functions as an arbitrary precision calculator. Unlike standard calculators that use infix notation (where operators are between operands, like 2 + 3
), dc
employs Reverse Polish Notation (RPN), also known as postfix notation. In RPN, operators follow their operands. This might seem unusual at first, but it allows for complex calculations to be performed efficiently without the need for parentheses.
dc
excels at calculations requiring high accuracy, as it’s not limited by the fixed precision of typical floating-point arithmetic. It’s particularly useful for tasks involving financial calculations, scientific computations, or any scenario where precise results are paramount.
Key Concepts of dc 🔗
- Stack-Based Operation:
dc
operates using a stack. Numbers are pushed onto the stack, and operations are performed on the numbers at the top of the stack. - Reverse Polish Notation (RPN): Operators are placed after the numbers they operate on. For example, to add 2 and 3, you would enter
2 3 +
. - Arbitrary Precision:
dc
can handle numbers with a virtually unlimited number of decimal places, limited only by system memory. - Scale: You can control the number of decimal places used in calculations through the
k
command.
Comparison with Other Tools 🔗
bc
(Basic Calculator): Another arbitrary precision calculator, butbc
uses a more traditional infix notation and provides a more programming-like environment with control structures and functions.dc
is generally considered more lightweight and suitable for quick command-line calculations.qalc
(Qalculate!): A more user-friendly calculator that supports infix notation, units, symbolic calculations, and more. While powerful, it might be overkill for simple arbitrary precision arithmetic.
Getting Started with dc 🔗
Start an interactive session:
dc
This will open the
dc
interpreter, and you can start entering commands. To exit, typequit
or pressCtrl+D
.Execute a script: You can save a series of
dc
commands in a file (e.g.,my_script.dc
) and execute it using:dc path/to/my_script.dc
Each command in the script will be executed sequentially.
Basic Calculations 🔗
Let’s illustrate with some examples:
Adding 5 and 10:
dc -e '5 10 + p'
Explanation:
5
: Pushes the number 5 onto the stack.10
: Pushes the number 10 onto the stack.+
: Pops the top two numbers (10 and 5) from the stack, adds them, and pushes the result (15) back onto the stack.p
: Pops the top value from the stack (15) and prints it to the standard output.
Subtracting 3 from 8:
dc -e '8 3 - p'
Explanation:
8
: Push 8.3
: Push 3.-
: Pop 3 and 8, subtract (8 - 3), push the result (5).p
: Print 5.
Multiplying 6 and 7:
dc -e '6 7 * p'
Explanation:
6
: Push 6.7
: Push 7.*
: Pop 7 and 6, multiply (6 * 7), push the result (42).p
: Print 42.
Dividing 20 by 4:
dc -e '20 4 / p'
Explanation:
20
: Push 20.4
: Push 4./
: Pop 4 and 20, divide (20 / 4), push the result (5).p
: Print 5.
Controlling Precision (Scale) 🔗
The k
command is used to set the number of decimal places for subsequent calculations.
Calculate 5 divided by 3 with 2 decimal places:
dc -e '2 k 5 3 / p'
Explanation:
2 k
: Sets the scale (number of decimal places) to 2.5
: Push 5.3
: Push 3./
: Divide 5 by 3. The result will be rounded to 2 decimal places.p
: Print the result (1.67).
Calculate the same with 7 decimal places:
dc -e '7 k 5 3 / p'
Output:
1.6666666
More Complex Examples 🔗
Calculate (10 + 5) * 2: In RPN, this becomes
10 5 + 2 *
:dc -e '10 5 + 2 * p'
Explanation:
10
: Push 10.5
: Push 5.+
: Pop 5 and 10, add (15), push the result.2
: Push 2.*
: Pop 2 and 15, multiply (30), push the result.p
: Print 30.
Calculate the square root of 16:
dc
uses thev
command for square root:dc -e '16 v p'
Output:
4
Calculate 2 to the power of 5 (2^5):
dc
uses the^
command for exponentiation:dc -e '2 5 ^ p'
Output:
32
Working with Negative Numbers: Negative numbers are entered using an underscore
_
before the number.dc -e '5 _3 / p'
Explanation:
5
: Push 5._3
: Push -3./
: Divide 5 by -3.p
: Print the result (with the current scale).
Further Exploration 🔗
dc
has many more features, including:
- Registers: For storing and retrieving values.
- Arrays: For storing sequences of numbers.
- Conditional Execution: Allowing for basic programming logic within scripts.
- Functions (Macros): For defining reusable sequences of commands.
For a comprehensive understanding of all its capabilities, refer to the official GNU dc
manual: https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html
By understanding the principles of RPN and the basic commands of dc
, you can leverage its arbitrary precision capabilities for a wide range of calculations directly from your command line.
I hope you enjoyed reading this post as much as I enjoyed writing it. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , and GitHub .