DS5000-BASIC 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 [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 ? or ^ 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
A bit-by-bit binary AND operator, although display is decimal.
EXAMPLE:
PRINT 3.AND.2
2
A bit-by-bit binary OR operator, although display is decimal.
EXAMPLE:
PRINT 1.OR.4
5
.XOR. LOGICAL EXCLUSIVE OR OPERATOR
A bit-by-bit binary XOR operator, although display is decimal.
EXAMPLE:
PRINT 7.XOR.6
1
COMMENTS 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 (OFFFFH) inclusive. If they are not, MCS BASIC-52 will generate a BAD ARGUMENT ERROR. All no n-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 DS5000-BASIC eliminates ALL spaces when it processes a user line and re-inserts the spaces when listing a program to the screen. When d oing this, DS5000-BASIC 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 are eli minated. The .OP. notation was chosen for the logical operators to alleviate the confusing display that result from a line entered as 10 B = A AND B and listed as 10 B = AANDB. This just looked confusing, and the dots were added to the logic al instructions. The previous example is listed as 10 B =A.AND.B, which is easier to read.
5.2.1 UNARY OPERATOR - GENERAL PURPOSE
ABS([expr])
Returns 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)
330
PRINT NOT(0)
65535
Returns the integer portion of the expression.
EXAMPLES:
PRINT INT(3.7)
PRINT INT(100.876)
3
100
Will return 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 returned 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 a nd 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. Note that the RND s tarts with the same "seed" when DS5000-BASIC is started and will produce exactly the same set of "random" numbers! Not very random. Uncertainty can be introduced by executing RND a random number of times. Of course, if you have a good mechani sm of introducing uncertainty, you might better use that process to develop random numbers.
EXAMPLES:
PRINT RND
.87309071
PI is not really an operator, it is a stored constant. In DS5000-BASIC, 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 reaso n DS5000-BASIC 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 desirable, 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
LOG([expr])
Returns the natural logarithm of the argument. The argument must be greater than 0. This calculation is carried out to 7 significant digits.
EXAMPLES:
PRINT 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(LOG(2))
2.7182818
2
5.2.3 UNARY OPERATORS - TRIG FUNCTIONS
SIN([expr])
Returns the SIN of the argument. The argument is expressed 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. The 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
.78539804
COMMENTS 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 REDUC ED 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 associated with this technique. The accuracy problem is noticed when the user ar gument 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 for t he TRIG functions as small as possible!
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. It's easy to illu strate what precedence is all about. Examine the following equation.
4+3*2 = ?
Should you add (4+3) then multiply 7 by 2, or should you multiply (3*2) then add 4? Well, the hierarchy of mathematics says that multiplication has precedence over addition, so you would multiply (3*2) first then add 4. Thus 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 becaus e multiplication has higher precedence. The precedence of operators from highest to lowest in DS5000-BASIC is as follows:
Relative to operator precedence, the rule of thumb should always be; when in doubt, use parentheses.
Relational expressions involve the operators =, <>,>,> = , <, and < =. These operators are typically used to "test" a condition. In DS5000-BASIC relational operators return a result of 65535 (0FFFFH) if the relational expressio n 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
0
PRINT 1>0
65535
PRINT A<>A
0
PRINT A=A
65535
It 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 te st a rather complex condition with ONE statement.
EXAMPLE:
>10 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 programme r 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.