Futurebasic/Language/numeric expressions
Numeric Expressions
editRevised: May 30, 2000 (Release 3)
A numeric expression is anything that can be evaluated as a number. A number can be expressed in the following ways:
Simple expressions
edit- A numeric literal, a symbolic constant, or a numeric variable. See Appendix C:Data Types and Data Representation , for more information.
Examples:17.3 _true x& Z%(14)
- A reference to any user-defined function or Toolbox function that returns a numeric value.
Examples:FN theSum# FN GETCATINFO(@pb)
- A value returned by any built-in FB function whose name does not end with "
$
". (Note: the two exceptions to this are theUSING
function and theSTR#
function, both of which return a string value.)
Examples:LEN("Hello") DIALOG(0)
Data comparison expressions
editData comparison expressions always return the value -1 or 0. In many contexts, these values are interpreted as meaning "true" or "false," respectively. Data comparison expressions have the following forms:
Equality comparisons
editAn equality comparison consists of two expressions of "compatible types" separated by the "=
" operator or the "<>
" operator (you can also use "==
" as a synonym for "=
", and "!=
" as a synonym for "<>
"). The two operands must fall into one of the following categories:
- A pair of numeric expressions;
- A pair of string expressions (see Appendix E: String Expressions);
- Any pair of variables of the same type.
An equality comparison using "=
" (or "==
") is evaluated as -1 if the two operands have equal values; otherwise it's evaluated as 0. An equality comparison using "<>
" (or "!=
") is evaluated as -1 if the two operands are not equal; otherwise it's evaluated as 0.
Examples:
x& == LEN(acc$) * 3
"Bronson" <> theName$(7,4)
record1 = record2
Order Comparisons
editAn order comparison can test the relative "order" of two numeric operands, or of two string operands; that is, it tests whether one operand is greater than or less than the other. In the case of strings, string1$
is considered "less than" string2$
if it precedes string2$
alphabetically. More accurately, string comparison depends on the ASCII values of the characters in the strings. An order comparison takes the form expr1 operator expr2
, where expr1
and expr2
are both numeric expressions or both string expressions, and operator
is one of the operators in this table:
<img src="appendix/Appendix_D-_Numeric_Expr02.gif" alt="IMAGE imgs/Appendix_D-_Numeric_Expr02.gif" width=397 height=177>
Examples:
blt& > ham% + rye%
"hello" << MID$(testString$,x,5)
Expressions with Unary Operators
editA unary operator is an operator that takes only one operand. FB has three unary operators: "+
"; "-
"; "NOT
". The unary operator always appears on the left side of the operand; the operand can be any numeric expression.
<img src="appendix/Appendix_D-_Numeric_Expr03.gif" alt="IMAGE imgs/Appendix_D-_Numeric_Expr03.gif" width=467 height=121>
Examples:
+n!
-(x# + 12 / 7.3)
NOT found%
Compound Expressions
editA compound numeric expression is any list of numeric expressions separated by one or more of the operators in the table below. A compound expression has this form:
expr1 operator expr2 [operator expr3 ...]
Additionally, any expression which is surrounded by a pair of parentheses is also an expression. When you surround an expression with parentheses, the entire expression within parentheses is evaluated before any operator to the left or right of the parenthetical expression is applied. This is useful when you want to change the default order in which the operators are applied. For example:
3 * (7 + 1)
In the above expression, the "+
" operator is applied before the "*
" operator. 3 is multiplied by the sum of 7 and 1, giving a result of 24. But if the expression had been written like this:
3 * 7 + 1
then the "*
" operator would have been applied first. In this case, 1 is added to the product of 3 and 7, giving a result of 22.
<img src="appendix/Appendix_D-_Numeric_Expr05.gif" alt="IMAGE imgs/Appendix_D-_Numeric_Expr05.gif" width=466 height=530>
Examples:
7 + 3 + 6 * 18.7
x& AND (NOT BIT(7))
ZZ MOD (x% + 8)
Operator Precedence
editWhen an expression includes more than one operator, the order in which the operations are performed can affect the result. When an operator appears to the left or right of a parenthetical expression, all of the operations within the parentheses are performed first. When several operators all appear within the same matching pair of parentheses (or outside of all parentheses), the order in which their operations are performed is determined by their order of precedence, with "higher precedence" operations being performed before "lower precedence" ones. For example, consider this expression:
4 + 7 * 5
The "*
" operator has a higher precedence than the "+
" operator (see the table below). So, when this expression is evaluated, first 7 is multiplied by 5 to get 35; then that result is added to 4 to get the final answer of 39.
The following table lists the operators in order of their precedence, from highest to lowest. When an expression contains several operators at the same level of precedence (and within the same depth of parentheses), their operations are always performed from left to right.
<img src="appendix/Appendix_D-_Numeric_Expr07.gif" alt="IMAGE imgs/Appendix_D-_Numeric_Expr07.gif" width=351 height=198>
Example: Consider the following expression:
20 - 4 + 3 * (24 / (7 + 1) + 2)
The following shows the series of operations that FB performs to reduce this expression to its final value, 31.
Operation |
Resulting expression |
20-4 = 16 |
16 + 3 * (24 / (7 + 1) + 2) |
(7+1) = 8 |
16 + 3 * (24 / 8 + 2) |
24/8 = 3 |
16 + 3 * (3 + 2) |
(3+2) = 5 |
16 + 3 * 5 |
3*5 = 15 |
16 + 15 |
16 + 15 = 31 | 31 |
Static Integer Expressions
editMany FB statements require quantities that are expressed as static integer expressions. A static integer expression may be a simple or complex expression, but its operands are limited to the following:
- Integer literals;
- Symbolic constants;
- The
SIZEOF
,OFFSETOF
andTYPEOF
functions.
The following are examples of valid static integer expressions:
762
3 * _myConstant + SIZEOF(x&)
44 / 11
The following are not valid static integer expressions:
126 + x&
3.14159
SQR(49)
85 + FN Zilch(36)