HTBasic Help
×
Menu
Index

Math and Relational Operators

 
Math operators provide the standard arithmetic operations as well as the INTEGER division, remainder, and modulo operators.
 
Operator
Meaning
Example
Result
+
Addition (dyadic/binary)      
3+4
7
+
Positive (monadic/unary)    
+4
4
-
Subtraction (dyadic/binary)
3-4
-1
-
Negation (monadic/unary)
-3
-3
*
Multiplication
3*4
12
/
Division
3/4
0.75
^
Exponentiation
3^4
81
DIV
Integer Division
4 DIV 3
1
MOD
Remainder
4 MOD 3
1
MODULO
Modulo
4 MODULO 3
1
 
Several of these operations can generate errors. The following table outlines the possible errors.     
 
Math Operation
Cause of Error
Example
Integer +-* DIV
Result too big
32760+32760
Long +-* DIV
Result too big
2,147,483,647+1
DIV
Divide by zero
1 DIV 0
Real +-*/
Result too big
1E200*1E200
Real +-*/
Result too small
1E-200*1E-200
/
Divide by zero
1/0
MOD/MODULO
MOD by 0
1 MOD 0
A^B
Result too big
1E200^1E200
A^B
A<0 and B non-integer        
(-2)^6.5
A^B
A=0 and B<0
0^(-1)
 
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.
 
Operator
Meaning
Example
Result
<
Less than
3<4
1
<=
Less than or equal
3<=4
1
=
Equals
"3"="4"
0
>=
Greater than or equal
3>=4
0
>
Greater than
"3">"4"
0
<>
Not equal
3<>4
1
 
Number Manipulation 
 
Operator
Functionality
ABS
Absolute value of an expression
CINT
Convert to Integer
DROUND
The number rounded to specified number of digits
FIX
Discard fractional part of a number
FRACT
Fractional part of a number
INT
Greatest integer part of a real number
MIN
Smallest number from list of values and arrays
MAX
Largest number from list of values and arrays
PROUND
The number rounded to the specified decimal place
RES
Result of last live keyboard expression
RND
Random number
SGN
Arithmetic sign of an expression
 
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:
 
X
CINT(x)
FIX(x)
INT(x)
2.6
3
2.0
2.0
2.2
2
2.0
2.0
-2.2
-2
-2.0
-3.0
-2.6
-3
-2.0
-3.0
 

Automatic Type Conversions

Conversions 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 Precedence

Mathematical 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).
     
Precedence
Operators/Functions
1
Parentheses () and sub-strings []
2
Functions: built in and user defined.
3
Exponentiation Operator ^
4
Multiplicative Operators *,/,DIV,MODULO,MOD
5
Monadic + and -
6
Dyadic + and -
7
String Concatenation &
8
Relational Operators =,<>,<,>,<=,>=
9
Monadic Logical Operator NOT
10
Logical Operator AND
11
Logical Operators OR and EXOR
 
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.