.so /usr/lib/tmac/tmac.e
.po 1i
.ll 6i
.lt 6i
.nr bi 5n   \" blocks indented 5n
.nr si 3n   \" indent text in sections 3n
.de $0      \" call this with each .sh (put title in index)
.(x
(\\$2) \\$1
.)x
..
.de BO
.if n \&\\$1\h'|0'\&\\$1\h'|0'\\$1\h'|0'\\$1\\$2
.if t .b \\$1 \\$2
..
.de EG
.(b C
\\$1
.)b
..
.de TL
.(b C
.sz 16
\fBCALC\fR: A Calculator Program
Tutorial Introduction and Manual

.sz 14
Gary Perlman
Cognitive Science Laboratory
University of California, San Diego
.sz 10
.he '\fBCALC\fR: A Calculator Program''Tutorial and Manual'
.fo ''- % -''
.)b
..
.TL
.pp
\fBCALC\fR
is a program for mathematical calculations for which you might use a
hand held calculator.
\fBCALC\fR
supplies most of the operations common to programming languages
and variables with properties much like those in Visicalc.
.pp
The arithmetical operators \fBCALC\fR offers are
.(b
+	addition
-	subtraction and change-sign
*	multiplication
/	division
%	modulo division
^	exponentiation
.)b
Arithmetical expressions can be arbitrarily complex
and are generally evaluated left to right.
That is,
.EG "a + b - c
is the same as
.EG "(a + b) - c.
Exponentiation is evaluated before multiplication and division
which are evaluated before addition and subtraction.
For example, the expression
.EG "a + b - c * d / e ^ 2
is parsed as
.EG "(a + b) - ((c * d) / (e ^ 2))
This default order of operations can be overridden by using parentheses.
.pp
\fBCALC\fR
supplies some transcendental functions:
.BO sqrt ,
.BO log ,
.BO exp ,
and
.BO abs .
.sh 1 "Using \fBCALC\fR"
.pp
To use
\fBCALC\fR,
begin by typing "\fBCALC\fR" at the UNIX command level, and
\fBCALC\fR
will prompt you with "CALC:\ ".
You can supply inputs to
\fBCALC\fR
from files specified by command line arguments.
For example, typing "calc\ foo" will read
from foo and then ask for input from you.
Type in each of your expressions followed by RETURN
and
\fBCALC\fR
will respond with how it parsed your expression followed by the result.
In all following examples, what you would type in is preceded by the
\fBCALC\fR
prompt "CALC:\ ", and what
\fBCALC\fR
responds with is immediately after.
A simple calculation is:
.(b
CALC: sqrt (12^2 + 5^2)
sqrt(((12 ^ 2) + (5 ^ 2)))     = 13
.)b
.pp
Expressions can be stored by assigning variables to them.
For example you could type:
.(b
CALC: pi = 22/7
(22 / 7)      = 3.14286
CALC: pi
pi          = 3.14286
.)b
Variables can be used in expressions.
.(b
CALC: area = pi * r^2
(pi * (r ^ 2))      = UNDEFINED
CALC: area
area     = UNDEFINED
.)b
.BO area
is undefined because
.BO r
has not been set.
Once
.BO r
is set,
.BO area
will have a value because
.BO area
is set to an equation rather than a particular value.
This can be observed by printing all the variables
so far introduced with ^V (CTRL-v), which may have to be typed twice
as ^V is used in some UNIX versions to quote characters.
.(b
CALC: ^V
pi       =      3.14286 = (22 / 7)
area     =    UNDEFINED = (pi * (r ^ 2))
r        =    UNDEFINED =
.)b
The variable table is formatted so that each variable's name is
on the left, followed by its current value, followed by its
current definition.
If
.BO r
is set to 5, the value of
.BO area
is now defined.
.(b
CALC: r = 5
5        = 5
CALC: ^V
pi       =      3.14286 = (22 / 7)
area     =      78.5714 = (pi * (r ^ 2))
r        =            5 = 5
.)b
The effect of changing
.BO r
on
.BO area
can be easily observed because of the way
.BO area
is defined.
.(b
CALC: r = 2
2         = 2
CALC: area
area      = 12.5714
.)b
.sh 1 "Setting Constant Values
.pp
Of course, there are times when you want to set a variable
to a value and not have it depend on the values of variables
at a later time.
To do this, you precede an expression with the number operator #.
For example,
.(b
CALC: area2 = # area
12.5716      = 12.5716
CALC: ^V
pi       =      3.14286 = (22 / 7)
area     =      12.5716 = (pi * (r ^ 2))
r        =            2 = 2
area2    =      12.5716 = 12.5716
.)b
.BO area2
does not depend on the variable to which it was set because
the number operator # only lets numbers through it rather than
expressions.
If
.BO area2
was set without the # operator,
it would be subject to any changes in
.BO area
or to any changes in variables on which
.BO area
depends.
.(b
CALC: area2 = area
area      = 12.5716
CALC: ^V
pi       =      3.14286 = (22 / 7)
area     =      12.5716 = (pi * (r ^ 2))
r        =            2 = 2
area2    =      12.5716 = area
.)b
.sh 1 "Testing Conditions
.pp
Variables can be set based on a tested condition.
For example, you may want a variable
.BO max
to always be the maximum of
.BO a
and
.BO b .
.(b
CALC: max = if a > b then a else b
(if (a > b) then a else b)    = UNDEFINED
.)b
.BO max
is undefined because
.BO a
and
.BO b
have not been set.
.(b
CALC: a = 21
21     = 21
CALC: b = 3^3
(3 ^ 3)    = 27
CALC: max
max      = 27
CALC: a = 50
50   = 50
CALC: max
max      = 50
.)b
The if-then-else expression allows variables to be set based on
conditions.
This condition can be made up with relational and logical operators.
The relational operators available with
\fBCALC\fR
are:
.(b
==	test equality
!=	test inequality
>=	greater than or equal
<=	less than or equal
>	greater than
<	less than
.)b
while the logical operators are:
.(b
&	and
|	or
!	not
.)b
A more complicated expression involving these is:
.EG "if a > b & b > c then b
The
.BO else
part of the conditional is optional, and if not present
and the condition is false, the conditional is undefined.
.sh 1 "Undefined Variables
.pp
Variables are undefined if they have not been set,
if they depend on variables that are undefined,
or if they are set to an expression involving an illegal operation.
.(b
CALC: 1/0
(1 / 0)     = UNDEFINED
.)b
You can be confident that no operations will result in
\fBCALC\fR
blowing up.
(The exception to this is writing an expression that indirectly
depends on itself, which causes big problems.)
Thus you could write the equation for the roots of a quadratic formula
with the following definitions and always get reasonable answers.
.(b
CALC: x = 0
CALC: a = b = 1
CALC: c = -1
CALC: radical = sqrt (b^2 - 4*a*c)
CALC: equation = a*x^2 + b*x + c
CALC: derivative = 2*a*x + b
CALC: root1 = (-b + radical) / (2 * a)
CALC: root2 = (-b - radical) / (2 * a)
.)b
.sh 1 "Control Characters
.pp
Non-mathematical operations are accomplished with control characters.
To type a control character, say control-p, while you hold down the
key labeled CTRL you type a "p".
This will appear as ^P.
Some control characters have very special meanings, such as
"stop the program" so you must be careful with them.
In general, you can avoid any problems with control characters
by typing a ^V (control-v) before them.
This character removes any special meaning associated with the
character immediately following it.
So to type ^P you could be extra safe and type ^V^P.
To type a control-v, you may have to type it twice.
Unfortunately, these conventions are not universal.
.pp
The following control operations are available with
\fBCALC\fR.
.(b
^P	change the printing option
^Rf	read the input from file f and return to current state
^V	print the variable table
^Wf	write the variable table to file f
	(actually, ^W is a synonym for ^V)
.)b
If you forget any of these commands,
you can type a ? to get \fBCALC\fR to remind you.
.bp
.sh 1 "Table of Operations

.(b M
.ta 1iC 2iC 3iC 4i
	OPERATOR		ASSOCIATIVITY
		PRECEDENCE		DESCRIPTION

	#a	1	none	numerical value of a
	a=b	2	right	a is set to expression b
	if a then b	3	left	if a != 0 then b else UNDEFINED
	else	4	left
	a|b	5	left	true if a or b is true
	a&b	6	left	true is a and b are true
	!a	7	none	true is a is false
	a==b	8	none	true if #a equals #b
	a!=b	8	none	true if #a is not equal #b
	a<b	8	none	true if #a is less than #b
	a>b	8	none	true if #a greater than #b
	a>=b	8	none	true if #a > #b | #a == b
	a<=b	8	none	true if #a < #b | #a == b
	a+b	9	left	a plus b
	a-b	9	left	a minus b
	a*b	10	left	a times b
	a/b	10	left	a divided by b
	a%b	10	left	a modulo b
	a^b	11	right	a to the b
	-a	12	none	change sign
	abs(a)	12	none	absolute value
	exp(a)	12	none	e to the a
	log(a)	12	none	natural logarithm of a
	sqrt(a)	12	none	square root of a
.)b
.ep
.fo ''''
.he ''''
.bp
.rs
.sp 1i
.TL
.sp 2v
.EG "TABLE OF CONTENTS
.xp
