March 11, 2008

Fast, flexible, calculating from the command line

Author: Ben Morgan

GUI calculators may be user-friendly, but they don't offer much comfort for a command-line power user. Fortunately, console-based tools such as bc, Genius, and Calc offer distinct advantages over their GUI counterparts.

Command-line calculators offer most if not all of the features GUI calculators have, and have a lot more going for them:

  • They have interactive and non-interactive usage modes.
  • They can be used in a script, as well as run their own scripts.
  • They are efficient to use -- your hands remain on the keyboard.
  • They have many built-in functions and variables.
  • They support on-the-fly definition of new functions and variables.

While Genius is the most advanced (and probably stable) of the aforementioned console calculators, Calc is the most user-friendly, and provides more capabilities than the average user will need. You can find Calc in the repositories of many distributions, and it can also run under Windows.

Calc does not have a man page; instead, all of its detailed help is located in /usr/share/calc/help/, and can be viewed using any plain text viewer, or from within Calc. The command calc help opens an overview of the help in less; you can then choose the topic that interests you and run calc help topic. To view all the help, use calc help full. Calc provides more general resources and examples in /usr/share/calc.

Calc uses intuitive mathematical symbols and functions, such as + - * / ^ ** % or sqrt(x), sin(x), tan(x), which makes it easy to learn. In addition, because it is a C-style calculator, people familiar with C-style programming will find that the syntax of Calc will comes naturally (although there are differences; see calc help unexpected).

To start, type the command calc. Calc's prompt is a semicolon. The following Calc session will give you a taste of the capabilities of the tool:

C-style arbitrary precision calculator (version
Calc is open software. For license details type: help copyright
[Type "exit" to exit, or "help" for help.]

; ## 5 to the power of 2 is
; 5 ^ 2
; 5 ** 2
; ## For a comment, you need 2 ##
; /* But these comments work too */
; ## Floating-point divide
; 111 / 2
; ## Integer divide
; 111 // 2
; ## How to define a function
; ## The short way:
; define f(x) = x*2
f(x) defined
; ## The long way:
; define g(first, second) {
;; first -= second;
;; first ** 2;
;; first // 1.5;
;; first++;
;; print "done";
;; return first;
;; }
g(first,second) defined
; ## The functions in action
; ## The space is optional
; g( f(2), 3 )
; g( f(5), 1 )

With no arguments, Calc starts in an interactive shell. You can also start it with an expression to evaluate, such as calc 2+3. After evaluation it will exit, unless you also supply the -i argument, in which case it will drop to the interactive shell.

You can place collections of variables and functions into a resource file, often suffixed with .cal, which the program can read and use. The syntax for resource files is identical to that of the interactive shell. As an example, suppose you find yourself in chemistry class using elemental atomic masses often. You could make a resource file with variables, functions, and commands, such as the following, named

#!/usr/bin/calc -i read

H = 1.00794
He = 4.0026
Li = 6.941
Be = 9.012182
B = 10.811
C = 12.0107
N = 14.0067
O = 15.9994
## ... and so forth

print "You are using the periodic resource for calc."

You can then load a resource file with a command like calc -i read Alternatively, you can make it executable with chmod +x and then run it directly. Once you load the resource file, you have access to all the variables that you defined in it:

You are using the periodic resource for calc.
; ## And now I am in the interactive shell
; O
; C+O*2

Most people use Linux with X running, instead of living solely on the command line. A convenient way to use Calc in such an environment is to start it within a virtual terminal, such as aterm: aterm -e calc or aterm -e calc -i read /pathtoyourresource/ Be forewarned, however, that the latest version of Calc suffers from a bug that causes it to quit after an attempt to call a nonexistent function. Therefore, when you are exploring functions, run Calc within a terminal that will not exit when Calc does, so that you do not visually lose what you already typed.

If you're a command-line fan, you don't need to start a GUI to run a calculator. Programs like Calc put plenty of power at your fingertips without your ever having to move a mouse.


  • Tools & Utilities
Click Here!