Skip to content


jtappin edited this page Jan 24, 2013 · 12 revisions
Clone this wiki locally

RPN Calculator

The GtkFortran rpncalc program is intended to be both a demonstration of some of the capabilities of GtkFortran and also a usable calculator application.

It uses Reverse Polish logic (similar to calculators from HP) for 2 reasons:

  1. It's easier to implement.
  2. I prefer RPN calculators when using a physical calculator.

The stack is of (at least in theory) unlimited size (there is probably a limit on the length of a TreeView in Gtk+).


Calculator with stack displayed Calculator with stack concealed



On most Linux/Unix desktops, when installed a menu item to start the calculator will be added to the "Education" section of the start menu.

RPNcalc can also be started from the command line:

RPNcalc [-o|--open|-c|--closed] [{-r|--restore} <file>] [{-m|--registers} <number>] \
   [-R|-D|-G|--degrees|--radians|--grads] [-h|--help]
  • -o, --open: Start with the stack displays open (default)
  • -c, --closed: Start with the stack displays closed
  • -r, --restore file: Load the stack and memory contents from the given file.
  • -m, --registers number: Set the number of memory registers to create. The default is 10.
  • -R, --radians: Set the trigonometry unit to radians.
  • -D, --degrees: Set the trigonometry unit to degrees.
  • -G, --grads: Set the trigonometry unit to grads (1/100 of a right angle).
  • -h, --help: Print a help text and exit.

Entering values:

Values can be entered either using the keypad, or by typing into the entry box. Values entered from the keypad make sanity checks for 2 decimal points or a decimal entered after the exponent has been started and the change-sign key works in a reasonably intelligent way. When values are typed or pasted into the entry box, characters that cannot be part of a number are rejected with a warning.

A value may be transferred from the entry to the stack by pressing the keyboard Enter key while focus is on the entry window, or by clicking the Enter key on the keypad. The Duplicate key copies the entry box to the stack without clearing the entry box. If the contents of the entry box are not a valid number (i.e. a Fortran read statement cannot convert it to a floating point value) a message is displayed in the status bar and you may edit the entry box to correct the problem.


The operators (+, -, *, / and ** and the atan2 function) operate on the entry box and the top element on the stack if there is anything in the entry box. If the entry box is empty, then they operate on the top 2 elements of the stack.

Operators may also be accessed by typing the operator into the entry window (N.B. The exponentiation operator is ^ rather than ** for convenience of implementation). The addition and subtraction operators will only work in this way if a sign would not be a valid part of a number where they are entered, notably + or - in an empty entry box is not treated as an operator.

The result is placed on the top of the stack, and displayed in the result window.


The functions operate on a single value, which is taken from the entry box if that has content or from the top of the stack otherwise. The result is placed on the top of the stack, and displayed in the result window.

If the Inverse checkbox is set, then functions are replaced by their inverses (e.g. sin becomes asin). The less-used functions in the pulldown are not affected by this.

The Rad, Deg and Grad radio buttons are used to select Radians, Degrees or Grads for the trigonometric functions.

The HMS key is not a proper function, it doesn't remove or add anything to the stack. It displays the contents of the entry box or the top of the stack as if it were a number of hours converted to HH:MM:SS.sss format.

Some less-used functions are in the More> pulldown. The atan2 function computes atan(y/x) removing the quadrant ambiguities.

Stack operations:

  • CE clears the entry box, or if that is empty deletes the top entry on the stack.
  • CA clears the entry box and all entries on the stack.
  • The up button moves the selected item in the stack up one place. If the top item (or nothing) is selected then it is exchanges with the entry box.
  • The down button moves the selected entry on the stack down one place.
  • The roll down button, moves the last element of the stack to the top and all others down one place.


There are a number of built in fundamental physics constants that are build in to the calculator, these can be entered from the Phys pull-down menu.

Memory Registers:

The calculator also has 10 memory registers (numbered 0-9). This number can be altered on startup with the -m option. Memory registers can be added and removed in operation using the spin button below the registers display.

These can be accessed in one of two ways:

  1. Select a register in the registers tab, and then click a memory operation. In this case the value used will be the entry box or the top of the stack if the entry is empty.
  2. Enter a register number in the entry box and click the memory operation. The value used is the top of the stack.

The operations are:

  • STO: Store the value in the selected register.
  • RCL: Copy the selected register to the top of the stack
  • M+: Add the value to the selected register
  • M-: Subtract the value from the selected register
  • MCL: Clear the selected register.
  • MCA: Clear all registers


If the Live stats toggle is enabled, then a summary of the statistical properties of the contents of the stack is maintained in the Statistics tab of the display area. Values from the statistics can be copied to the stack by selecting the desired line then clicking the copy to stack button.

Save & Restore:

The stack, registers and entry box can be saved to and restored from a text file with the File->Save and File->Restore menu items.

The file format is a plain text file with the floating point values written in hexadecimal -- this allows the retention of full-precision but is endian-independent. Obviously any machines that do not use IEEE floating point values will not be able to read files from other machines. Also any machine with a c_double that is not 8-bytes will not work.


In the current version, there are 2 user-definable settings accessed through the Edit menu:

  • Result Format: Specify the format to use in the result box. You have the options to select one of the standard formats:

    • "Fixed": A fixed number of decimal places (set in the precision spin box). The actual format used is "(F0.)". WARNING: this may be a GNU extension.
    • "Sci": Scientific format. Specify the number of decimals, and the width of the exponent in the spin boxes. The total width is calculated automatically.
    • "Eng": Engineering format, similar to scientific, except that the exponent is always a multiple of 3.
    • "Free": Use a list-directed write (the default).

    • Alternatively you can type an explicit Fortran format statement into the combo box (with or without the enclosing parentheses). This may be any Fortran formatting code valid for a REAL type. Setting it to "*" or an empty string will use the default list-directed output (as will an invalid format).

  • The HMS display can be toggled to DMS format.


The menu items have accelerators to save mouse clicking:

  • Save -- ctrl-s
  • Restore -- ctrl-o
  • Quit -- ctrl-q
  • Set Format -- ctrl-f
  • Help -- ctrl-h
  • About -- ctrl-a
  • About gtk-fortran -- ctrl-shift-a
Something went wrong with that request. Please try again.