Recently, I was doing some math on my PC, tapping away on the virtual keypad of my calculator app. The thought dawned on me how silly it was to have in hand a device of immense computing power, yet pretend to use a pocket calc of limited capability. Why do a silly thing like that?
It is an easy and simple matter to do math on the command line. Why not release power of Python and C to do any sort of quick math I may need from a calculator? Read below to see how I set up two calculators: one for the command line and another to run in Rofi. If you already have Catbird Linux 4.2 or later, you already have both.
Rofi-calc is a plugin which provides calculator functions in a Rofi popup. On Arch Linux, OpenBSD, and OpenSUSE, it may be easily installed from the standard repositories. I was not as lucky with Ubuntu 20.04, where it is necessary to download, compile, and install the plugin. It is not difficult; I already had git and the build-essential tools.
If you don't have them, install the necessary packages:
apt update; apt install -y git build-essential autotools rofi-dev qalc libtool
Then clone the git repository, build, and install Rofi:
git clone --depth=1 https://github.com/svenstaro/rofi-calc cd rofi-calc autoreconf -i mkdir build cd build/ ../configure make make install
When installed, the following code should be set up as a keybind or in a small script. I use a simple little script named "roficalc" with this line:
rofi -show calc -modi calc -no-show-match -no-sort
Rofi should pop up and present a calculator. On Catbird Linux, the calculator will look like this...
Setting up a command line calculator with Python is even easier than setting up Rofi-calc. In fact, it is so useful that I may replace the graphical calculator in Rofi with the Python REPL.
Wait... What is a REPL? "REPL" is an acronym for "Read - Evaluate - Print - Loop." It is a way to interact through the terminal with the code interpreter. You enter a line of something, such as 4 * x**2 + 8 * y = 8 and get a calculated reply from the computer. There are plenty of different sorts of REPLS, to work with different languages. Here, we will use Python.
For more broad calculating power, some modules need to be imported. It is a simple matter to set up the Bash shell with some code in .bashrc (in the home directory) to load math and statistics. Here is some code:
# Python REPL calculator alias calc='python3 -ic "from math import *; from statistics import *"'
After entering the above code into .bashrc, reload the config by entering the following on the command line:
. .bashrc
Try some math. Let's find the mean of a series of numbers and then try some simple operations. "$" in the prompt means the session is as the normal user. Use Python syntax for variables, exponents, and so forth. To exit the REPL, use Ctrl - D.
$ calc >>> mean([20, 30, 40, 50, 60]) 40 >>> x=3 >>> 4 * x**2 + 15 51
(grin) It works...
There are other Python modules available; some are not so useful for a simple calculator. My suggestion is to use a script for more complicated work. For all of the quick number crunching one would use a handheld calculator (or app) for, the REPL calculator does fine.