dc Command in Linux



dc also known as a desk calculator is an arbitrary -precision arithmetic calculator that uses Reverse Polish Notation (RPN).

The arbitrary-precision arithmetic calculator is a tool that performs highly precise calculations on large numbers which is impossible to achieve on a fixed-precision calculator.

On the other hand, the Reverse Polish Notation (RPN) is a mathematical notation in which the operator comes after the operands. For example, the expression 1 + 2 is written in infix arithmetic notation. To convert this expression to Reverse Polish Notation (RPN) or postfix, it will be written as 1 2 +.

Table of Contents

Syntax for dc Command

The general syntax of dc command is as follows −

dc [option] [script_expression] / [file] [command]

The dc command comes with various flags and options to enhance the command’s functionality.

Replace [option] with the command flags or options. The [script_expression] and [file] will be replaced with arithmetic expression and file name respectively. Replace [command] with subcommands to print stack elements, and perform other operations.

The dc command can also be used interactively; execute dc without any option in the terminal −

dc

Or, use here string operator (<<<) −

dc <<< "string"

Options for dc Command

The options and flags for the dc command are listed below −

Flag Options Description
-e --expression="expression" Evaluates expression string
-f --file="filename" Evaluates the expression in a file
-h --help Prints the help
-V --version Prints the version of dc command

Subcommands for dc Command

To control the dc command interactive session and print the output various subcommands are used. The category-wise subcommands are listed below −

Printing Subcommands −

Subcommand Description
p Prints the top element in the stack with a new line
n Prints the top element in the stack without a new line
P Pops the element off from the top of the stack and discard it
f Print the content of the entire stack

Controlling Subcommands −

Subcommand Description
c Clears the stack
d Duplicates the top element in the stack
r Reverse the order of the top two elements in the stack

Parameter Subcommands −

Subcommand Description
i Sets the input radix
I Pushes the current input radix on the top of the stack
o Sets the output radix
O Pushes the current output radix on the top of the stack
k Sets the precision for arithmetic operations
K Pushes the current precision on the top of the stack

Status Inquiry Subcommands −

Subcommand Description
z Pushes the current stack depth (number of elements in the stack)
Z Pushes the number of digits of the top value of the stack
X Pushes the number of fraction digits from the top element of the stack

Register Subcommands −

Subcommand Description
sr Stores an element from the top of the stack in the register r
lr Copies the value in the register r and pushes it onto the main stack
Sr Stores a value to the top of the register stack, making the previous value inaccessible in the register
Lr Copies the values from the top of the register stack the pushes it to the main stack, making the previous value in the register accessible

Arithmetic Operators −

Operator Description
+ Pops two values off the stack and pushes the result
- Pops two values off, subtracts the first one from the second, and pushes the result
* Pops two values off, multiply them, and pushes the result
/ Pops two values off, divides the second one from the first, and pushes the result
% Pops two values off, calculates the remainder, and pushes the result onto the stack
~ Pops two values off, divides the second one from the first, and pushes the quotient first and then the remainder
^ Pops two values off, use the first one as the exponent and the second as the base
v Pops one value off and calculates the square root

Using dc Command

This section will demonstrate the usage of the dc command with examples −

Using dc Command with expression Option

To use the dc command in a non-interactive way, use the --expression option or -e flag.

Let’s evaluate (2 x 8) + 5 expression −

dc --expression="2 8 * 5 + p"
dc Command Linux 1

The above command can also be used with the -e flag.

dc -e "2 8 * 5 + p"
dc Command Linux 2

Performing Arithmetic Operations

To add two operands, push operands first and then the operator onto the stack. Use the p subcommand to push and print the result.

dc -e "8 4 + p"
dc Command Linux 3

Similarly, for subtraction, multiplication, and division, use -, *, and / operators respectively −

dc -e "8 4 - p"
dc -e "8 4 * p"
dc -e "8 4 / p"
dc Command Linux 4

The negative numbers in the dc command are represented by an underscore (_) instead of the minus sign (-).

dc Command Linux 5

Use the v subcommand to get the square root of the top element in the stack.

For example, let’s take the square root of 8.

dc -e "8 v p"
dc Command Linux 6

If the square root is a mixed value then you will only get a decimal value in the output. The precision is set explicitly using the k subcommand.

The modulus (%) operator calculates the remainder.

dc -e "10 3 % p"
dc Command Linux 7

The ^ operator is used to perform exponentiation −

dc -e "2 4 ^ p"
dc Command Linux 8

Here, 2 is the base and 4 is the exponent.

Note that you cannot use mixed numbers as exponent.

Setting Precision

By default, the dc command does not display fractional parts in the output. Let's set the precision to 4 decimal places using the k subcommand.

dc -e "4 k 8 v p"
dc Command Linux 9

Now, we get a more precise value of the square root of 8.

Using dc Command with Radix

The radix is another term for the base, it can be 2 (binary), 8 (octal), 10 (decimal), and 16 (hexadecimal).

Let’s set 2 as the radix for subsequent stack elements.

dc -e "2i 101 100 + p"
dc Command Linux 10

The 101 is 5 in decimal and 100 is 4 in decimal, so therefore the output will be 9 after addition.

To get the output in binary, set the output radix as 2.

dc -e "2i 101 100 + 2o p"
dc Command Linux 11

Now, let’s set the input radix to hexadecimal and get the output in binary −

dc -e "16i A C + 2o p"
dc Command Linux 12

Here, the A and C are equivalent to decimals 10 and 12. Adding them will get 22 and the binary of 22 is 10110.

Modifying the Stack

To list all the elements of the stack, use the f subcommand.

dc -e "8 10 11 f"
dc Command Linux 13

To clear the stack, use the c subcommand. In the following example, three elements are pushed onto the stack, then the c subcommand removes all elements. The f subcommand is used to list the elements of the stack.

dc -e "8 10 11 c f"
dc Command Linux 14

The c subcommand will clear the stack, so no element will appear in the output.

To reverse the top two elements of the stack, the r command is used.

dc -e "8 10 11 r f"
dc Command Linux 15

To print the top value of the stack without a new line, use the n command.

dc Command Linux 16

To duplicate the top element of the stack, use the d subcommand.

dc Command Linux 17

Using Registers

Registers are used to store the elements of the stack. To store the top element of the stack to the register r, use the sr command.

dc -e "8 10 11 sr f"
dc Command Linux 18

The top element will be popped off and stored in the register r, to verify print the whole stack.

To print the stored value from the register, use the lr command. It essentially pushes the element to the top of the stack. To print it use p.

dc -e "8 10 11 sr lr p"
dc Command Linux 19

Calculating Expression from File

To calculate the expression from a file, the --file option or -f flag is used with the filename.

dc -f "myfile.txt"
dc Command Linux 20

Storing String Values to Stack

Enclose the string in the square brackets ([]), to push the string elements to the stack −

dc -e "1 [two] 3 [four] f"
dc Command Linux 21

Using Macros

A macro is a set of instructions that is used to automate the repetitive task in a program. The macros can also be defined with the dc command. Place any set of the commands in the square brackets.

dc -e "1 2 [1 2 +]x f"
dc Command Linux 22

In the above command, [1 2 +] is a macro, and the x subcommand executes it, which results in 3. So, when you print the whole stack the stack (3 2 1) will be displayed.

Getting Stack Status

To get the total number of items in the stack the z subcommand is used.

dc -e "8 10 11 z p"
dc Command Linux 23

To print the number of the digits of the top value of the stack, use Z.

dc -e "13 231 2323 Z"
dc Command Linux 24

Using dc Command Interactively

First, let’s interactively use the dc command. Open the terminal and execute the dc command −

dc

A session will begin without any prompt.

Now type a number (12) and press Enter to push the first element to the stack. To push another element (10), follow the same procedure.

Let’s add these two elements, type + and press Enter. The elements of the stack will be popped off and the result will be pushed in the stack. At the moment the stack will have only one element (the result). Use the p subcommand to print the result.

dc Command Linux 25

You can perform other operations in the same manner.

Conclusion

The dc command in Unix and Unix-like operating systems stands for desk calculator, which uses Reverse Polish Notation (RPN). It is primarily used to perform complex and precise calculations. The dc command can be used interactively in the command-line interface or as a one-liner with the --expression option. This guide covered the basic syntax, options, subcommands, and various examples of using the dc command.

Advertisements