The Smart-Calc


1. Outlook

"Smart-Calc" is a simple scientific calculator using reverse Polish notation. The calculator is built on clisp using the gtk-server. Reverse Polish notation is a numbers-first-then-operator notation. The merit of this notation is that successive calculations can be done smoothly, as this notation does not discriminate input and output.

The calculator widget consists of five parts:
an entry box, a queue window, a memory window, an echo label, and a status label (Fig. 1).

This calculator covers fractional numbers, complex numbers, and long numbers as well as conventional integer and floating point. Any kinds of LISP functions including user defined ones which take numbers as arguments are available. The log of your calculation is saved in the file defined by *smart-calc-log*

As the Smart-Calc is designed for keyboard typing, its appearance is somehow different from those of conventional calculators with full of buttons. As a consequence of the simple GUI, uses cannot see the commands. Command "help" is prepared for this reason. Type "help" then the list of commands appears in a separated window.

The motivation I wrote this program is that I am not satisfied with conventional scientific calculators. I am happy using this program. I hope many people enjoy it, which is the reason to ask Peter introducing my program on his homepage.

Figure 1. The calculator widget of the Smart-Calc. (a) entry box, (b) queue window, (c) memory window, (d) echo label, and (e) status label.

2. Getting started

2.1. Preparation

The script requires the gtk-server for GTK2.x.

This document requires some knowledge about LISP, clisp and the gtk-server. See following links to know about them.

  1. LISP: LISP Primer , lisp.org, franz.com, and Paul Graham
  2. clisp
  3. the gtk-server
If clisp and the gtk-server are installed in your system, do as follows:
  1. Go to the directory where smart-calc.lisp is saved.
  2. Execute clisp from that directory
  3. Compile smart-calc.lisp; (compile-file "smart-calc.lisp")
  4. Load the *.fas file; (load "smart-calc")
Then the widgets shown in Figure 1 should appear. If not, please try demo script in the gtk server. If the demo script works and the smart-calc not, please contact me.

You can make a nice shortcut by using run.exe or run.vbs, then the program behaves like as a stand alone program.

2.2. Using the Smart-Calc

As the Smart-Calc uses reverse Polish notation, you should load numbers first then give an operator. Table 1 shows your entry, the queue window, the memory window, and comments for calculating "2+3+4".

Table 1. Calculating 2+3+4.
entry queue memory comments
2 2 0 load a number
3 2 3 0 appended the second number
4 2 3 4 0 the third number
+
9
0
add the numbers

Now, let's continue calculation using the answer of 2+3+4. It is quite easy, just enter 5 and * as shown in Table 2.


Table 2. Continue calculation using the previous result "9".
entry queue memory comments
+ 9 0 result of the previous calculation
5 9 5 0 append 5
* 45 0 then *
m+ 45 45 keep it in the memory

To keep the result in the memory, enter "m+" then memory value becomes "45".
Enter "bye" to exit. You will find the log of this calculation in the file defined by *smart-calc-log* ("smart-calc.log", if default).

3. Numbers and constants

When you enter a number or a constant defined in the LISP system or in the *smart-calc-constants*, it is appended at the end of the queue.
To enter complex numbers, you can write "[REAL, IAMG]" as well as "#C(REAL IAMG)", formal expression of complex numbers in LISP.

4. Numerical functions

Any kind of functions that takes numbers as arguments can be used in this calculator. The value returned by function is not restricted to numbers. However, the values used in the successive calculations are numbers and symbols bound to numbers by LISP system or by *smart-calc-constants*. Others are filtered off before sending to numerical functions. (See section 8.3.)

Following the list of equipped functions in the LISP system.

+, -, *, /, floor, ceiling, round, truncate,
mod, max, min, gcd, lcm, exp, expt, log, sqrt,
sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh,
random etc.
See lisp.org or franz.com for detailed information. User defined functions can be used as well.

The given operator (function) is applied to the numbers in the queue. If the argument of the operator does not match the queue, the calculator does nothing expect showing "error" in the status label.

5. Commands to manipulate the queue and the memory

Following is the list of commands.

help
show help window
const (or constants)
show constants window
push Number (or Constant)
add a number or a constant at the beginning of the queue.
del
delete the last item of the queue.
pop
delete the first item of the queue.
c!
clear the queue.
<
call previous queue if exists.
>
call forward queue if exists.
filter
filter out non-numerical item.
refresh
refresh queue and memory window.
mr
append the memory value at the end of the queue.
mc!
set memory value 0.
bye
exit from the Smart-Calc.
You can also do it by clicking the right top button of the calculator window.

If the queue consists of one number, following four operators are available.
m+
set Nm = Nm + Nq
m-
set Nm = Nm - Nq
m*
set Nm = Nm * Nq
m/
set Nm = Nm / Nq
where, Nm and Nq represent the values of memory and queue, respectively.

6. Editing *smart-calc-constants*

The *smart-calc-constants* is an association list, which consists of two or three membered lists of symbols, their values, and their units. The element list looks like "(|c| 2.99792458d8 "m s-1")". The term which represents the constant should be bound up by two | 's, instead of double quotations. LISP system regards |word| as symbol and it searches the association list much quicker. The unit (the third item) should be string (enclosed by double quotations). It can be omitted in case of mathematical constants.

7. User defined functions.

You can define your own functions using "defun", which is a typical way of defining LISP functions. See LISP Primer, lisp.org, franz.com, or lines 179--226 of "smart-calc.lisp". You can use your functions without additional declaration.

If the function returns a list, strings can be included for easy reading. See section 8.3 and lines 208--217 of smart-calc.lisp.

8. Examples

I show three examples.

8.1. Euler's formula eπ i = -1

Table 3 shows the procedure of calculating eπ i.
Table 3. Calculating eπ i
entry queue memory comments
pi PI 0 you can enter a symbol bound to a number
i PI i 0 "i" is defined in the *smart-calc-constants*
* [0.0L0, 3.1415926535897932385L0] 0 i * π
exp [-1.0L0, -5.016557612668332023L-20] 0 the result is very close to "-1"

8.2. Calculating the value of F(x) = a x3 + b x2 + c x + d

Function "poly" is defined in the Smart-Calc as an example. The first argument of the poly is the value of x and rests are parameters. Thus, the value of F(x) is calculated by the poly like as follows:
(poly x a b c d)
Let's take a= 2/7, b= 5/17, c = 2/3, and d= 13/23 as parameters for instance.
The calculating procedures for F(0) and F(-1) are shown in Table 4.

Table 4. Calculating F(x) = 2/7 * x3 + 5/17 * x2 + 2/3 * x + 13/23 (x=0, -1)
entry queue memory comments
2/7 2/7 0 loading parameters
5/17 2/7 5/17 0 loading parameters
2/3 2/7 5/17 2/3 0 loading parameters
13/23 2/7 5/17 2/3 13/23 0 loading parameters
push 0 0 2/7 5/17 2/3 13/23 0 loading x value at the top of the queue
poly 13/23 0 calculating F(0)
< 0 2/7 5/17 2/3 13/23 0 calling back the previous queue
pop 2/7 5/17 2/3 13/23 0 deleting the x value
push -1 -1 2/7 5/17 2/3 13/23 0 assign x = -1
poly -764/8211 0 then calculate F(-1)
float -0.09304591 0 convert to float

8.3 Averages and standard deviations

Functions "ave", "sd", and "stat" are included in the smart-calc for calculating average and standard deviation (S.D.). Table 5 shows the procedure of calculating the average and S.D. for a data set {2.2, 2.4, 3.2, 4.3, 3.8, 3.7, 2.9}.

Table 5. Calculating the average and S.D. for the data set {2.2, 2.4, 3.2, 4.3, 3.8, 3.7, 2.9}
entry queue memory comments
--- 2.2 2.4 3.2 4.3 3.8 3.7 2.9 0 loading numbers are skipped
ave 3.2142856 0 calculating the average
< 2.2 2.4 3.2 4.3 3.8 3.7 2.9 0 calling the previous queue
sd 0.7690439 0 calculating the S.D.
< 2.2 2.4 3.2 4.3 3.8 3.7 2.9 0 calling the previous queue
stat ave. 3.2142856 S.D. 0.7690439 0 the other way to calculate ave. and S.D.
+ 3.9833295 0 "ave." and "S.D." are skipped to calculate '+

9. Summary

The Smart-Calc is a simple GUI for numerical calculations attached on clisp using the gtk-server. As Common LISP deals with fractional numbers, complex numbers, and long numbers, which is one of the advantages of this language, this simple calculator can also deal with these numbers. The Smart-Calc is much easier to use than conventional scientific calculators. The Smart-Calc is really convenient for me. I hope it is useful for other people as well.

Takafumi SHIDO
shidot@gtk-server.org