8.3 Math Tools


A command-line, arbitrary-precision "reverse Polish notation" (RPN) calculator.

dc is based on the concept of a stack, which is central to the operations of modern digital computer. A computer stack is not unlike a stack of kitchen plates, the last to come on stack, is the first to go out (this is also known as LIFO="last-in, first-out"). This contrasts with a queue (another important concept) where the first in is the first out (FIFO).

You can perform operations only on the number(s) which is on the top of the stack. The two basic operations are: push and pop (put on the top of stack, and retrieve from the top of stack). Unary operations pop one value off the stack ("unary" means "requiring one operand"). Binary operations pop two values off the stack ("binary" means "requiring two operands"). Tertiary operations pop three values off the stack ("tertiary" means "requiring three operands"). In all cases, the result is always pushed back onto the top of stack.

RPN calculators (regular, hand-held) are very popular among technically oriented people and in academia. The RPN notation never requires parentheses.

History. The parentheses-free logic was developed by Polish mathematician Jan Lukasiewicz (1878-1956) before the WWII. Originally, the operator preceded the values. For computer applications, it's been modified so that the operator goes after the values, hence "reversed" in the name "reversed Polish notation".

To exercise some operations on stack, try this:

dc [start the arbitrary precision reverse Polish notation calculator]

1 [push "1" on the stack]

2 [push another number on the stack]

3 [push yet another number on the stack]

4 [push yet another number on the stack]

f [print the entire stack; you should see 1 2 3 4]

p [print the number on the top of the stack without affecting the stack; you should see 4]

+ [perform addition (binary operation), therefore pop two last values off the stack (4,3), and push the result (7) on the stack]

p [print the number on the top of the stack, i.e. the results of the last addition (7).].

p [print again the number on the top of the stack to see that the stack wasn't affected by printing (7)]

* [perform multiplication (binary operation), therefore pop two last values, and push the result (14)]

p [print the result of the multiplication (14)]

P [pop the last number off the stack (14)]

p [print the number on the top of the stack]

2000 [push a large integer on the stack]

k [set the precision to the value which is on the top of the stack, i.e. 2000]

1 [push another number on the stack]

f [print the content of the entire stack]

701 [push another number on the stack]

/ [divide last two numbers on the stack, i.e. "1/701" with 2000 decimal places of precision]

p [print the result of the last division]

q [quit the arbitrary precision reverse Polish notation calculator]

Please note that when using the reverse Polish notation (RPN) you never need parentheses. Try man dc to read about other capabilities of dc.


An interactive arbitrary-precision calculator. Type "quit" to exit bc. Type "scale=20" (or so) before doing floating point divisions or you end up with the quotient instead of the floating-point result of the division.



(in X terminal) Standard, GUI calculators.

e '2*3/4+sin(pi/2)'

The "e" expression evaluator did not come on my RH7.x CDs. Yet, it is my favourite of all the "command line" calculators. Try: http://www.softnet.tuc.gr/~apdim/projects/e/ to download.

expr 1 + 1 / 3

Evaluate an integer-type expression. The "expr" is not meant to be a calculator, but is mostly for flow control in shell scripts. The above example will return the result "1", which is correct because 1/3 is 0 as far as integer division is concerned.


Octave is a high-level interactive language for numerical computations, closely resembling "matlab". Included with any fine Linux distribution.


(in X terminal) Another large and sophisticated system for numerical computing, somewhat resembling "matlab", but with a rather clampsy graphical interface. Don't even try it unless you have rather sophisticated math needs else you won't appreciate it. It is included on RH7.0 "powertools" CD. The hompage is http://www-rocq.inria.fr/scilab/

A silly example session showing some matrix algebra. My input is shown bold.

-->a=[1 1 1;2 3 4]

a =

! 1. 1. 1. !

! 2. 3. 4. !

-->b=[1 1; 2 2;3 3]

b =

! 1. 1. !

! 2. 2. !

! 3. 3. !


c =

! 6. 6. !

! 20. 20. !


d =

1.0E+14 *

! 11.258999 - 3.3776997 !

! - 11.258999 3.3776997 !


head -c 8 /dev/random

cat /dev/random | od

cat /dev/urandom | memencode

(3 commands.) Examples on how to generate random characters on the Linux command line by reading from the device "random" or "urandom". The first command produces approximately 8 characters by reading from the device "random", which generates high quality (difficult to predict) random numbers. It will become slow once the "entropy" on your computer is exhausted (e.g., when producing lots of random characters). The solution then is to wait or type something on the keyboard, move your mouse, switch the terminal, make your hard drive to read or write, etc., to generate more random noise ("entropy"). The second command keeps producing random characters, but displays them as octal numbers (using the "octal dump", od, filter). It has to be interrupted with <Ctrl><c>. The third command uses the device "urandom", which is faster then "random" when generating lots of random characters. But when the system enthropy is low, the randomness of its output from "urandom" might be compromised, yet it probably is still good for all but most demanding applications. The output is filtered to the mime format (the Internet mail-oriented 7-bit encoding standard) so it is all printable ASCII. The detailed description of the theorry and implementation of the Linux algorithm for generating the random numbers can be found in the source code file://usr/src/linux/drivers/char/random.c on your Linux system.

factor 10533858466222239345

Find all the prime factors of an integer number. Factors of an integer are numbers by which the integer is divisible without a remainder. For example, the factors for 6 are: 1, 2, 3, and 6.


A programming language / environment for advanced statistical computing. Type "quit()" to exit.


Utility for creating graphs and plots. Very good for non-interactive (batch) work, but not very simple for interactive use. A good introduction to gnuplot can be found at http://www.duke.edu/~hpgavin/gnuplot.html.

Suggest a Site