Math operators provide the standard arithmetic operations as well as the INTEGER division, remainder, and modulo operators.
Several of these operations can generate errors. The following table outlines the possible errors.
Relational operators can be used on numbers or strings. Relational operators can be used in assignment statements, IF statements, and any other place a numeric expression is legal. For example:
X = 4*(Y>Z)+J*(A=B AND R<T)
Relational operators may be used on strings to compare the LEXICAL ORDER of the two strings. By default, ASCII values are used to determine relative order. "A" is less than "B". If two strings of different length are the same up to the end of the shorter string, then the shorter string is less than the longer string. For example, "ABCDE" < "ABCDEF". The LEXICAL ORDER IS statement affects the relational ordering of strings.
Number Manipulation
Notice the differences among CINT, FIX, and INT. CINT converts a REAL value to an INTEGER by substituting the closest INTEGER to the value. FIX returns the closest integral value between the REAL value and zero. INT returns the closest integral value between the REAL value and negative infinity. Also, CINT actually changes the type from REAL to INTEGER while INT and FIX return integral results, but the type is not changed. The following table helps illustrate the differences:
Automatic Type ConversionsConversions from REAL to INTEGER or LONG and from INTEGER or LONG to REAL are done automatically in HTBasic. Basic operations are done in INTEGER math if both operands are INTEGER or LONG. Otherwise, REAL math is used. For example:
INTEGER J ! J is now an integer type variable
X = 1.234 ! X is a real number (by default)
J = X ! The real value of X is converted to
! integer and assigned to J.
X = J ! This conversion is from integer to real
X = 1.0 ! Faster than X=1 (no convert required)
X = 1 ! This requires a convert to real
X = PI DIV 2.0*10 ! X will equal ten.
Execution PrecedenceMathematical precedence describes the order in which operators in an expression are evaluated. For example: 1+2*3+4 is evaluated as 1+(2*3)+4 because multiplication (2*3) has a higher precedence than addition (1+2). If the two operators are on the same row in the precedence chart below, the operations occur in left to right order (i.e. 1+2-3+4).
However, note that for compatibility with HP BASIC, HTBasic has a non-standard definition of a precedence that you should be aware of. Most computer languages place all monadic operators (operators that operate on one operand) at a higher precedence than dyadic operators (operators that operate on two operands). However, HTBasic places monadic + and - below some of the dyadic operators. For exasmple, HTBasic evaluates -4^0.5 as -(4^0.5) which is equal to -2, while most other computer languages would treat it as (-4)^0.5 and the square root of a negative number is an illegal operation.
|