HTBasic Help
×
Menu
Index

Precedence

 
Mathematical precedence describes the order in which operators in an expression are evaluated. Some cheap calculators execute each operation as it is entered. If you are used to this type of calculator, you may be confused by the concept of precedence. For example, the correct answer to the formula:
 
1+2*3+4
 
is 11, not 13. This is 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, the operations occur in left to right order (i.e. 1+2-3+4).
 
HP BASIC (and HTBasic) has an odd quirk in its definition of precedence which you should be aware of. Most computer languages place all monadic operators (operators which operate on one operand) at a higher precedence than dyadic operators (operators which operate on two operands). However, HTBasic and HP BASIC place monadic + and - below some of the dyadic operators. The following is one example of an expression that will evaluate differently because of this:
 
-4^0.5
 
With HTBasic, this is equivalent to -(4^0.5) which is equal to -2. With most other computer languages, this is equivalent to (-4)^0.5 which is an illegal operation.