4.2 Evaluations

In BETA, the term evaluation is used to refer to expressions, assignment statements and procedure calls.

For evaluations arithmetic, boolean and relational operators are provided. The operator precedence is similar to C with the AND and OR operators considered to be on a par with * and + respectively. This means that parentheses will often be needed to obtain the desired result. The priorities are shown below:

Relative Priority

Class

Operators

least

relational

=, <>, >, >=, <, <=

 

additive

+, -, or, xor

 

multiplication

*, /, div), mod, and

highest

unary

not, -, +

When a real value is assigned to an integer the fractional part is removed irrespective of the sign of the value. Various functions for manipulating reals, including rounding, log, log10, sin, cos, tanh, sqrt, and power are available in the math library. This library also contains predefined constants such as PI, e, pihalf (PI/2), log2e (log(e) to base 2), log10e (log(e) to base 10), ln2 (natural log of 2), etc.

The max, min and abs functions are not generic; they do not work properly with reals since they will convert input values to integers. Instead the functions fmax, fmin and fabs in the math library could be used.

Finally, since characters are type compatible with integers and there is no type control at this level, some interesting evaluations may be done, such as:

(i+1) * ('a' * i) / 4.33->putint

The program shown in Program 3 shows some of the things discussed here. First, it includes both the numberio and the math fragments required for the numeric work. It also shows our first procedure, tab, introduced to help simplify formatting the output.

Program 4: ExploreTypes.bet

ORIGIN '~beta/basiclib/betaenv';
INCLUDE '~beta/basiclib/numberio';
INCLUDE '~beta/basiclib/math';
---- program: descriptor ----
(# 
   
   (*  p3.bet: Exploring types and functions
    ***********************************************************)
   
   i,j,k: @integer;
   x,y,z: @real;
   tab: (# do '    '->puttext #);
   
do '\nEnvironment constants: \n\n'->puttext ;
   '\t MaxInt = '->puttext; maxint->putint;
   '\n\t MinInt = '->puttext; minint->putint;
   '\n\t MaxReal= '->puttext; maxreal->putreal (# do exp->style #);
   '\n\t MinReal= '->puttext; minreal->putreal (# do exp->style #);
   3->i; -10->j;
   '\n\n *** Simple functions *** \n'->puttext;
   '\n    I     J   max(I,J)  min(I,J)  abs(J) \n'->puttext;
   tab ;
   i->putint; tab;
   j->putint; tab;
   (i,j)->max->putint; tab; tab;
   (i,j)->min->putint; tab; tab;
   j->abs->putint;
   '\n\n *** Reals ***\n'->puttext;
   '\nX           Y           fmax(X,Y)       fmin(X,Y)       fabs(-3.6) \n'->puttext;
   3.01->x;  3.78->y;
   x->putreal;  tab;
   y->putreal;  tab;
   (x,y)->fmax->putreal; tab; tab;
   (x,y)->fmin->putreal; tab; tab;
   -3.6->fabs->putreal;
   '\n\nPi = '->puttext; pi->putreal; 
   '\ncos(Pi/4) = '->puttext;
   (Pi div 4)->cos->putreal;
   '\n\nMixing types: (i+1) * (\'a\' * i) / 4.33'->
   putText; ' = '->putText;
   (i+1) * ('a' * i) / 4.33->putint;
   newline;
#)

In BETA, procedures, types and objects are treated in a unified manner as variations of a single concept, the pattern. The general syntax for such a pattern declaration is:

<names>: <descriptor>

In the simplest case, the object descriptor is what we have called a block which can contain declarations and imperatives. In the case of procedures which need parameters or functions which return results, there can also be input and output parameters but we leave that for later.

In our example, tab is defined by:

tab: (# do '    ' -> puttext #);

This has neither parameters nor a result. tab merely stands for the more lengthy statement which outputs a string of 4 blanks. Contrast this declarations with that of the variables for i,j,...,z which use @ to indicate that space is to be reserved for the variables.

Returning to our program, we first print out the maxint and minint constants defined for both integers and reals. Note the use of control characters, e.g. '\n' in the titles to do some formatting.

Next, we show the use of the min, max and abs functions on integers. We also use the tab procedure to space out the printed results.

After that we apply the fmin, fmax and fabs to reals.

Then we print PI, one of the built-in constants defined in math, as well as the value of cos(45 degrees). Note that angles must be expressed as radians.

To finish off, there is a mixed evaluation with integers, booleans, characters and real.

The results of executing ExploreTypes are shown below:

Environment constants: 

         MaxInt = 2147483647
         MinInt = -2147483648
         MaxReal= 1.797693e+308
         MinReal= 2.225074e-308

 *** Simple functions *** 

    I     J   max(I,J)  min(I,J)  abs(J) 
    3    -10    3        -10        10

 *** Reals ***

X           Y           fmax(X,Y)       fmin(X,Y)       fabs(-3.6) 
3.010000    3.780000    3.780000        3.010000        3.600000

Pi = 3.141593
cos(Pi/4) = 0.707107

Mixing types: (i+1) * ('a' * i) / 4.33 = 268

For more sophisticated examples on use of reals and math functions, the reader should look at the demonstrations programs:

These are located in the reals demo directory of basiclib.


Libraries Tutorial
© 1994-2004 Mjølner Informatics
[Modified: Thursday January 16th 2003 at 10:23]