|STAT Statistical Data Analysis
Free Data Analysis Programs for UNIX and DOS
by Gary Perlman
skip navigation Home | Preface | Intro | Example | Conventions | Manipulation | Analysis | DM | Calc | Manuals | History

Last updated:

CALC: Tutorial and Manual

Introduction

calc is a program for mathematical calculations for which you might use a hand-held calculator. calc supplies most of the operations common to programming languages and variables with constraint properties much like those in spreadsheets.

The arithmetical operators calc offers are

+   addition
-   subtraction and change-sign
*   multiplication
/   division
%   modulo division
^   exponentiation
Arithmetical expressions can be arbitrarily complex and are generally evaluated left to right. That is,
a + b - c
is the same as
(a + b) - c
Exponentiation is evaluated before multiplication and division which are evaluated before addition and subtraction. For example, the expression
a + b - c * d / e ^ 2
is parsed as
(a + b) - ((c * d) / (e ^ 2))
This default order of operations can be overridden by using parentheses.

calc supplies some transcendental functions: sqrt, log, exp, and abs, and the following trigonometric functions: sin, asin, cos, acos, tan, and atan, for which degrees are measured in radians.

Using CALC

To use calc, begin by typing

calc
at the command level, and calc will prompt you with
CALC:
You can supply inputs to calc from files specified by command line arguments. For example, typing
calc foo
will read from the file foo and then ask for input from you. Type in each of your expressions followed by RETURN and calc 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 calc prompt
CALC:
and what calc responds with is immediately after. A simple calculation is:
CALC: sqrt (12^2 + 5^2)
sqrt(((12 ^ 2) + (5 ^ 2)))     = 13

Expressions can be stored by assigning them to variables. For example you could type:

CALC: pi = 22/7
(22 / 7)      = 3.14286
CALC: pi
pi          = 3.14286
Variables can be used in expressions.
CALC: area = pi * r^2
(pi * (r ^ 2))      = UNDEFINED
CALC: area
area     = UNDEFINED
area is undefined because r has not been set. Once r is set, area will have a value because 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, which may have to be typed twice as ^V is used in some UNIX versions to quote characters.
CALC: ^V
pi       =      3.14286 = (22 / 7)
area     =    UNDEFINED = (pi * (r ^ 2))
r        =    UNDEFINED =
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 r is set to 5, the value of area is now defined.
CALC: r = 5
5        = 5
CALC: ^V
pi       =      3.14286 = (22 / 7)
area     =      78.5714 = (pi * (r ^ 2))
r        =            5 = 5
The effect of changing r on area can be observed because of the way area is defined.
CALC: r = 2
2         = 2
CALC: area
area      = 12.5714

A special variable named $ is always equal to the most recent result printed.

Setting Constant Values

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,

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
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 area2 was set without the # operator, it would be subject to any changes in area or to any changes in variables on which area depends.
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

Testing Conditions

Variables can be set based on a tested condition. For example, you may want a variable max to always be the maximum of a and b.

CALC: max = if a > b then a else b
(if (a > b) then a else b)    = UNDEFINED
max is undefined because a and b have not been set.
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
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 calc are:
==  test equality
!=  test inequality
>=  greater than or equal
<=  less than or equal
>   greater than
<   less than
while the logical operators are:
&   and
|   or
!   not
A more complicated expression involving these is:
if a > b & b > c then b
The else part of the conditional is optional, and if not present and the condition is false, the conditional is undefined.

Undefined Variables

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.

CALC: 1/0
(1 / 0)     = UNDEFINED
You can be confident that no operations will result in calc blowing up. Thus you could write the equation for the roots of a quadratic formula with the following definitions and always get reasonable answers.
x = 0
a = b = 1
c = -1
radical = sqrt (b^2 - 4*a*c)
equation = a*x^2 + b*x + c
derivative = 2*a*x + b
root1 = (-b + radical) / (2 * a)
root2 = (-b - radical) / (2 * a)

Control Characters

Non-mathematical operations are accomplished with control characters. To type a control character, say CTRL-p, while you hold down the key labeled CTRL you type a p. This will appear as ^P. Some control characters have special meanings, such as "stop the program" so you must be careful with them. On UNIX, you can avoid some problems with control characters by typing a ^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 ^V, you may have to type it twice. Unfortunately, these conventions are not universal.

The following control operations are available with calc.

^P  toggle the printing of expressions (UNIX only)
^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
    (^W is a synonym for ^V)
If you forget any of these commands, you can type a ? to get calc to remind you.

Table of calc Operations

    Operator         Associativity
             Precedence             Description

       $       const      none      numerical value of previous calculation
       #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 power a
     log(a)      12       none      natural logarithm of a
    sqrt(a)      12       none      square root of a
     sin(a)      12       none      sine of a in radians (cos & tan)
    asin(a)      12       none      arc sine of a (acos & atan)

© 1986 Gary Perlman
skip navigation Home | Preface | Intro | Example | Conventions | Manipulation | Analysis | DM | Calc | Manuals | History | Top