**5.1 DUAL OPERAND OPERATORS**

MCS BASIC-52 contains a complete set of arithmetical and logical operators. Operators are divided into two groups, dual operand or dyadic operators and single operand or unary operators. The generalized form of all dual operand instructions is as follows:

[expr] OP [expr], where OP is one of the following operators:

EXAMPLE:

PRINT 3+2 5

EXAMPLE:

PRINT 100/5 20

Raises the first expression to the power of the second expression. The power any number can be raised to is limited to 255. The notation ** was chosen instead of the sometimes used ^ symbol because the "up arrow" symbol appears different on various terminals. To eliminate confusion the ** notation was chosen.

EXAMPLE:

PRINT 2**3 8

EXAMPLE:

PRINT 3*3 9

EXAMPLE:

PRINT 9-6 3

EXAMPLE:

PRINT 3.AND.2 2

EXAMPLE:

PRINT 1.OR.4 5

EXAMPLE:

PRINT 7.XOR.6 1Comments on logical operators .AND., .OR., and .XOR.

These operators perform a bit-wise logical function on valid integers. That means both arguments for these operators must be between 0 and 65535 (0FFFFH) inclusive. If they are not, MCS BASIC-52 will generate a BAD ARGUMENT error. All non-integer values are truncated, not rounded.

You may wonder why the notation .OP. was chosen for the logical functions.
The only reason for this is that MCS BASIC-52 eliminates all spaces when it
processes a user line and inserts spaces before and after statements when it
lists a user program. MCS BASIC-52 does not insert spaces before and after operators.
So, if the user types in a line such as 10 A = 10 * 10, this line will be listed
as 10 A= 10*10. All spaces entered by the user before and after the operator
will be eliminated. The .OP. notation was chosen for the logical operators because
a line entered as 10 B = A AND B would be listed as 10 B = AANDB. This just
looked confusing, so the dots were added to the logical instructions and the
previous example would be listed as 10 B=A.AND.B, which is easier to read.

**5.2.1 UNARY OPERATORS - GENERAL PURPOSE**

Retums the ABSOLUTE VALUE of the expression.

EXAMPLES:

PRINT ABS(5) PRINT ABS(-5) 5 5

Returns a 16 bit one's complement of the expression. The expression must be a valid integer (i.e. between 0 and 65535 (0FFFFH) inclusive). Non-integers will be truncated, not rounded.

EXAMPLES:

PRINT NOT(65000) PRINT NOT(0) 535 65535

Retums the integer portion of the expression.

EXAMPLES:

PRINT INT(3.7) PRINT INT(100.876) 3 100

Will retum a value of + 1 if the argument is greater than zero, zero if the argument is equal to zero, and - 1 if the argument is less than zero.

EXAMPLES:

PRINT SGN(52) PRINT SGN(0) PRINT SGN(-8) 1 0 -1

Returns the square root of the argument. The argument may not be less than zero. The result retumed will be accurate to within + / - a value of 5 on the least significant digit.

EXAMPLES:

PRINT SQR(9) PRINT SQR(45) PRINT SQR(100) 3 6.7082035 10

Returns a pseudo-random number in the range between 0 and 1 inclusive. The RND operator uses a 16-bit binary seed and generates 65536 pseudo-random numbers before repeating the sequence. The numbers generated are specifically between 0/65535 and 65535/65535 inclusive. Unlike most BASICS, the RND operator in MCS BASIC-52 does not require an argument or a dummy argument. In fact, if an argument is placed after the RND operator, a BAD SYNTAX error will occur.

EXAMPLES:

PRINT RND .30278477

PI is not really an operator, it is a stored constant. In MCS BASIC-52, PI
is stored as 3.1415926. Math experts will notice that PI is actually closer
to 3.141592653, so proper rounding for PI should yield the number 3.1415927.
The reason MCS BASIC-52 uses a 6 instead of a 7 for the last digit is that errors
in the SIN, COS
and TAN operators were found to be greater
when the 7 was used instead of 6. This is because the number PI/2 is needed
for these calculations and it is desireable, for the sake of accuracy to have
the equation PI/2 + PI/2 = PI hold true. This cannot be done if the last digit
in PI is an odd number, so the last digit of PI was rounded to 6 instead of
7 to make these calculations more accurate.

**5.2.2 UNARY OPERATORS - LOG FUNCTIONS**

Returns the natural logarithm of the argument. The argument must be greater than 0. This calculation is carried out to 7 significant digits.

EXAMPLES:

PPINT LOG(12) PRINT LOG(EXP(1)) 2.484906 1

This function raises the number "e" (2.7182818) to the power of the argument.

EXAMPLES:

PRINT EXP(1) PRINT EXP (L0G (2)) 2.7182818 2

**5.2.3 UNARY OPERATORS - TRIG FUNCTIONS**

Returns the SIN of the argument. The argument is expressied in radians. Calculations are carried out to 7 significant digits. The argument must be between +- 200000.

EXAMPLES:

PRINT SIN(PI/4) PRINT SIN(0) .7071067 0

Returns the COS of the argument. Thc argument is expressed in radians. Calculations are carried out to 7 significant digits. The argument must be between +- 200000.

EXAMPLES:

PRINT COS(PI/4) PRINT(COS(0)) .7071067 1

Returns the TAN of the argument. The argument is expressed in radians. The argument must be between +- 200000.

EXAMPLES:

PRINT TAN(PI/4) PRINT TAN(0) 1 0

Returns the ARCTANGENT of the argument. The result is in radians. Calculations are carried out to 7 significant digits. The ATN operator returns a result between -PI/2 (3.1415926/2) and PI/2.

EXAMPLES:

PRINT ATN(PI) PRINT ATN(1) 1.2626272 .78539804Comments on trig functions

The SIN, COS, and TAN operators use a Taylor series to calculate the function. These operators first reduce the argument to a value that is between 0 and PI/2. This reduction is accomplished by the following equation:

Reduced argument = (user arg/PI - INT(user arg/PI)) * PI

The reduced argument, from the above equation, will be between 0 and PI. The reduced argument is then tested to see if it is greater than PI/2. If it is, then it is subtracted from PI to yield the final value. If it isn't, then the reduced argument is the final value.

Although this method of angle reduction provides a simple and economical means
of generating the appropriate arguments for a Taylor series. there is an accuracy
problem assiociated with this technique. The accuracy problem is noticed when
the user argument is large (i.e. greater than 1000). That is because significant
digits, in the decimal (fraction) portion of reduced argument are lost in the
(user arg/PI - INT(user arg/PI)) expression. As a general rule, try to keep
the arguments tor the trig tunctions as small as possible!

**5.3 UNDERSTANDING PRECEDENCE OF OPFRATORS**

The hierarchy of mathematics dictates that some operations are carried out before others. If you understand the hierarchy of mathematics, it is possible to write complex expressions using only a minimum amount of parentheses. lt is easy to illustrate what precedence is all about, examine the following equation:

4+3*2 = ?

Should you add (4+3) then multiply seven by 2, or should you multiply (3*2) then add 4? Well, the hierarchy of mathematics says that multiplicatlon has precedence over addition, so you would multiply (3*2) first then add 4, So,

4+3*2 = 10

The rules for the hierarchy of math are simple. When an expression is scanned from left to right an operation is not performed until an operator of lower or equal precedence is encountered. In the example addition could not be performed because multiplication has higher precedence. The precedence of operators from highest to lowest in MCS BASIC-52 is as follows:

1) Operators that use parentheses "(" " )"

2) Exponentation "**"

3) Negation "-"

4) Multipication "*" and division "/"

5) Addition "+" and subtraction "-"

6) Relational expressions "=" "<>" ">" ">=" "<" "<="

7) Logical AND ".AND."

8) Logical OR ".OR."

9) Logical XOR ".XOR."

Relative to operator precedence, the rule of thumb should always be, when in doubt, use parentheses.

**5.4 HOW RELATIONAL EXPRESSIONS WORK**

Relational expressions involve the operators =, <>, >, >=, <, and <= . These operators are typically used to "test" a condition. In MCS BASIC-52 relational operators return a result of 65535 (0FFFFH) if the relational expression is true, and a result of 0, if the relation expression is false. But, where is the result returned? It is returned to the argument stack. Because of this, it's possible to actually display the result of a relational expression.

EXAMPLES:

PRINT 1=0 PRINT 1>0 PRINT A<>A PRINT A=A 0 65535 0 65535It may seem strange to have a relational expression actually return a result, but it offers a unique benefit in that relational expressions can actually be "chained" together using the logical operators .AND., .OR.. and .XOR.. This makes it possible to test a rather complex condition with one statement.

EXAMPLE:

>1O IF A<B .AND. A<>C .OR. A>D THEN. . . . . .Additionally, the NOT([expr]) operator can be used.

EXAMPLE:

>10 IF NOT(A>B) .AND. A<C THEN. . . .By "chaining" together relational expressions with logical operators, it is possible to test very particular conditions with one statement. When using logical operators to link together relational expressions, it is very important that the programmer pay careful attention to the precedence of operators. The logical operators were assigned lower precedence, relative to relational expressions, just to make the linking of relational expressions possible without using parentheses.

Copyright © Madis Kaal 2000-