ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ DB/C Newsletter ³
³ August 1993 ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
Editor's Notes
This month's newsletter contains a much needed tutorial on expressions.
Traditional DATABUS did not contain expressions, so many older programs (and
programmers) do not use them. This is unfortunate, because they are one of the
most powerful aspects of the language. This article should help clear up any
misunderstandings you may have with expressions.
We continue to look into the possibility of getting connected to the
Internet for support, etc. If you are currently a user of the Internet, I am
interested in how you use it and in what your opinion of it is. Just send a
message to the SYSOP telling me your experiences. Thanks in advance.
We are always looking for topics to include in the newsletter. If you
have any ideas, please let me know.
DNW
Expressions
Expression evaluation was introduced in DB/C release 6.0. In DB/C 6.0 and
DB/C 7.0, the use of expressions was limited to the MOVE statement and to a few
flow control statements like IF. In DB/C release 8.0, expression evaluation
has been expanded. Expressions can now be used anywhere a constant character
or numeric value can be used.
What are expressions? Many people are already familiar with numeric
expressions in the form of mathematical formulas. For example, a store charges
a customer an amount based on the price and quantity of an item purchased. The
total cost to the customer is the price of the item multiplied times its cost.
This formula for the total cost can be written with this expression:
(PRICE * QUANTITY)
where * means multiplication.
DB/C allows expressions to be used in place of a series of calculations.
The traditional DATABUS code for our example is:
MULTIPLY QUANTITY BY PRICE GIVING TOTAL
The equivalent code using expressions would be:
MOVE (QUANTITY * PRICE) TO TOTAL
This example is a simple demonstration of the use of expressions. We have
only substituted one line of code for another. Now lets make the example more
complicated to demonstrate the advantage of using expressions. The following
code will repeat the calculations from above but this time we will add in the
sales tax (assume a sales tax of 6.75%):
MULTIPLY QUANTITY TO PRICE GIVING TOTAL
MULTIPLY "1.0675" BY TOTAL
The equivalent instruction with expressions:
MOVE (QUANTITY * PRICE * 1.0675) TO TOTAL
Now, even with this simple example, we can see that using expressions
results in code that is simpler and easier to maintain than the traditional
programming style. Complex calculations that may take many instructions and
several temporary variables in traditional DATABUS can be reduced to one or two
lines of code with the use of numeric expressions.
DB/C provides additional capabilities that greatly expand the power of
expressions. Before we continue with a discussion of these capabilities we
must first cover some basic terminology. DB/C expressions consist of a
combination of parentheses, numeric variables, numeric literals, numeric
constants, character variables, character literals and operators. There are
four types of operators: character unary, character binary, numeric unary and
numeric binary operators.
Types of Operators
A complete list of expression operators can be found in the
DB/C release 8.0 Language Reference Manual on pages 5 through 9.
Numeric Binary operators
The '+' and '/' symbols are examples of numeric binary
operators for addition and division respectively. The
following expression:
(4 / 2 + 3)
means divide 4 by 2 and add 3 to the result. In this
case the '/' operator uses 4 and 2 for its operands.
The '+' operator use the result of 4 divided by 2 as its
first operand and 3 as its second operand.
Character Binary Operators
The '+' symbol is also a character binary operator. If
the operands for the '+' operator are both character
type then the '+' operator is called the concatenation
operator. The '+' operator concatenates two character
strings together. For example the following line of
code:
MOVE ("FILE." + EXTENSION) TO DEST
appends (or concatenates) the logical string in the
character variable extension to the characters "FILE."
and moves the result to the character variable dest.
Numeric Unary Operators
SIN, COS, LOG are all examples of numeric unary
operators. In the following example:
MOVE (SIN VAR1 + COS VAR1) TO RESULT
the variable RESULT is assigned the value of the
trigometric sine of the value VAR1 plus the trigometric
cosine of the value of VAR1.
CHAR is another numeric unary operator. The result of
the char operator is that it will cause the numeric
operand to be used as if it were a character variable.
This is useful for combining a character variable with a
numeric variable in one expression. For example:
VAR1 NUM "5"
MOVE ("ABC" + CHAR VAR1) TO RESULT
will put the string "ABC5" into the variable RESULT.
The following code would cause a compiler error because
the operand variable types conflict:
VAR1 NUM "5"
MOVE ("ABC" + VAR1) TO RESULT
Character Unary Operators
NUM and SQUEEZE are examples or character unary
operators. NUM is like the numeric unary operator CHAR
except that NUM causes its character operand to appear
to be a numeric variable. For example:
VAR1 INIT "3"
DISPLAY (2 * NUM VAR1)
will display 6.
SQUEEZE removes all the blanks from a character
variable.
VAR1 INIT "A B C D E "
DISPLAY ">", (SQUEEZE VAR1), "<"
will display: >ABCDE<
Operator Precedence
All expressions must contain at least one set of parenthesis
around the whole expression. Parenthesis may also be used
to force operator precedence. For example, in the expression:
(A + B / C)
the value of B is divided by C. The result is then added
to A because division operator has higher precedence than
the addition operator. In contrast, in the expression:
((A + B) / C)
the value A is added to B and the result is divided by C
because of the use of parentheses.
A complete list of operator precedence is listed on page
10 of the DB/C release 8.0 Language Reference Manual.
Precision and Rounding
In general, numeric expression evaluation produces a result
that contains enough digits so that no information is lost.
In each of the following formulas, the precision of the left
and right operands are "L1.R1" and "L2.R2" and the precision
of the result is "L3.R3".
Addition and subtraction produce a result whose precision
is defined by these formulas:
L3 = (the greater of L1 and L2) + 2
R3 = (the greater of R1 and R2)
Multiplication and exponentiation produce a result whose
precision is defined by these formulas:
L3 = L1 + L2
R3 = R1 + R2
Division produces a result whose precision is defined by
these formulas:
L3 = L1 + R2
R3 = R1 + R2
DB/C does not do rounding for any numeric operation.
Except for division and exponentiation, the is no potential
loss of precision, because there are always enough digits
to exactly represent the result. Because division and
exponentiation can produce infinitely long results, it is
not possible to always round correctly. Therefore, DB/C
doesn't round at all. Note, however, that the MOVE
instruction does round. See example 3 below.
Examples
1. This example might be used to generate source for a
program with 100 variables named NUM1 through NUM100.
VAR1 NUM 3
FILE FILE
SEQ NUM "-1"
FOR VAR1 FROM 1 TO 100
WRITE FILE, SEQ; ("NUM" + SQUEEZE CHAR VAR1 + " INT 5")
REPEAT
2. This example uses the resize operator (!) to print
a value with as if it were a NUM 9.2 variable.
TOTAL NUM 8
PRINT "THE TOTAL IS: ", (TOTAL ! 9.2)
3. This example shows the effect of no rounding in
expressions and rounding in the MOVE statement.
X NUM 2.3
Y NUM 2
Z NUM 2.2
MOVE 2 TO X
MOVE 3 TO Y
DISPLAY "X / Y = ", (X / Y)
MOVE (X / Y) TO Z
DISPLAY "Z = ", Z
The result displayed is:
X / Y = .666
Z = .67
With the knowledge of how expressions work, you can use them to simplify
your programming and make your life a little easier.
DB/C Class Schedule
The next DB/C class is scheduled for September 27-30, 1993. It is held
in the Oak Brook, Illinois office of Subject, Wills & Company. For more
information, contact Judi Tamkevic at (708) 572-0240.