1. Overview
In this tutorial, we’ll talk about calculators that we can use in the command-line interface. We’ll mainly look at the dc calculator and the bc calculator.
Both tools are arbitrary-precision decimal calculators. Arbitrary precision means that we can use numbers with an arbitrary amount of digits in the integer part, which are the digits before the decimal point. This is still limited by the memory of the system, but it isn’t limited to eight bytes like many other applications. Since both tools are decimal calculators, we can also use decimal numbers.
First, let’s explore both calculators and discuss the differences between them. Then, we’ll consider when to choose one over the other, and other alternatives to solve math problems in the command line.
2. dc Calculator
dc was one of the first Unix programs. It was developed in B, the predecessor of the C programming language. dc is available in most Linux distributions, and despite the strict syntax, we can use it since it’s a very powerful tool.
dc uses the reverse Polish notation. That’s why we need to deal with a strict syntax. This postfix notation is different to the infix notation we commonly use in mathematics since the operator follows the operands. For example, we can express the addition of 3 and 4 in reverse Polish notation as “3 4 +”. More complex operations require more complex expressions. To subtract 4 from 3 and then add 5, we write ” 3 4 – 5 +” instead of the infix expression “3 – 4 + 5”.
Moreover, to show the result in dc, we need to type p (print) before pressing Enter. We can use dc by piping the output of a command like echo:
$ echo '3 4 * p' | dc
12
We can also use string expansion with the <<< operator to input operations to dc:
$ dc <<< "2 4 + p"
6
There is also an interactive mode in dc. There, we can define variables in a stack with s+letter (where the letter represents the register name) and use them with l+letter:
$ dc
>> 4 sc
>> 3 lc * p
12
>> 2 lc + p
6
>> q
As we discussed, dc can have an arbitrary precision for integers. We can tweak the number of digits after the decimal point with k:
$ dc
>> 1 k
>> 1 4 / p
.2
>> 3 k
>> 1 4 / p
.250
When dividing 1 over 4, if we only keep one digit the result is 0.2 (dc makes no automatic rounding by default and truncates decimals), while if we ask for three digits we get 0.250.
3. bc Calculator
bc was created as a successor to dc to be used as a calculator application in computers. It was even created by the same developers, who refined the program to remove inconveniences that the users experienced. In fact, in the beginning, bc was simply a front-end that converted the bc notation into a notation that dc could use to get the result. There are multiple bc implementations, such as the POSIX bc, the OpenBSD bc, and the GNU bc.
bc uses the infix notation, which is the most common notation in mathematics. With this notation, we can express the addition of 3 and 4 as “3 + 4” and the subtraction of 4 from 3 and then the addition of 5 as “3 – 4 + 5”.
Since both tools are so closely related, we can use them similarly. bc also allows piping from echo and string expansion:
$ echo '3 * 4' | bc
12
$ bc <<< "2 + 4"
6
As with dc, there is also an interactive mode in bc. In it, we can define variables (c=4) and operate with the infix notation:
$ bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
>> c = 4
>> 3 * c
12
>> 2 + c
6
>> quit
There’re two ways to exit the interactive mode of bc: with the quit command and with Ctrl+D.
bc provides a library that sets up default parameters and implements useful mathematical functions, such as trigonometric functions. For example, invoking the library sets the number of decimal places to 20. We can activate this library with the -l option. Nevertheless, we can change the number of decimal locations with the scale option:
$ bc -l
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
>> 1 / 4
.25000000000000000000
>> scale = 1
>> 1 / 4
.2
>> scale = 3
>> 1 / 4
.250
Similar to dc, the bc tool has a customizable number of decimal digits.
4. Differences Between dc and bc
Both tools allow for arbitrary-precision decimal arithmetic. Both tools also accept code from the files that are used in the command line as arguments and can be customized with macros. Thus, the features of dc and bc are very similar.
dc is a legacy tool present almost everywhere. Therefore, we’ll be able to find in most (if not all) systems that we deal with.
On the other hand, bc* has three advantages over *dc:
- it has a library with support for standard math operations
- it follows the POSIX standards (so it’s more portable than dc)
- its infix notation seems more natural to use when we employ bc as a manual calculator
5. Alternatives to bc and dc
6. Conclusion
In this article, we’ve discussed dc and bc as command-line calculators. We understood their differences and when to use one over the other. Finally, we also looked at other ways to perform quick calculations on the command line.