FutureBasic Logo

<<    Index    >> FutureBasic 5

Appendix D - Numeric Expressions   appendix



A numeric expression is anything that can be evaluated as a number. A number can be expressed in the following ways:
I. Simple expressions
II. Data comparison expressions
Data 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:
II.1 Equality comparisons
An 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:
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

II.2 Order comparisons
An 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:

Operatorexpr1 operater expr2 returns -1 if and only if:
>expr1 is greater than expr2
>=, =>expr1 is greater than or equal to expr2
<expr1 is less than expr2
<=, =<expr1 is less than or equal to expr2
>> (strings only)expr1 is greater than expr2 without regard to letter case
<< (strings only)expr1 is less than expr2 without regard to letter case

Examples:
blt& > ham% + rye%
"hello" << mid$(testPascalString,x,5)

III. Expressions with Unary Operators
A unary operator is an operator that takes only one operand. FutureBasic has three unary operators: "+"; "-"; "not". The unary operator always appears on the left side of the operand; the operand can be any numeric expression.

OperatorOperator expr returns:
+expr
-the negative (additive inverse) of expr
Notthe binary 1's complement of expr.
See Not in the main part of the manual.

Examples:
+n!
-(x# + 12 / 7.3)
not found%

IV. Compound Expressions
A 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.

OperatorDescription
+Addition.
++Increment a variable
+=Add the expression from the right of the equal sign to the variable on the left.
-Subtraction
--Decrement a variable
-=Subtract the expression from the right of the equal sign from the variable on the left.
*Multiplication.
/If both operands are integer expressions, this operator does integer division. If either operand is a real number, the operator does floating point division.
\The operator always does floating point division. Integer operands are converted to floating point before the division.
\\Identical to /
^Exponentiation (raising to a power).
=; ==Comparison.
<<The first operand is shifted left by the number of bit positions specified by the second operand. Both operands must be integral values. A left shift by n is equivalent to multiplying by 2^n. The result is undefined if n is negative or greater than the width in bits of the first operand.
>>The first operand is shifted right by the number of bit positions specified by the second operand. Both operands must be integral values. A right shift by n is equivalent to dividing by 2^n with rounding towards minus infinity. The result is undefined if n is negative or greater than the width in bits of the first operand.
And; &&Bitwise And operator. See the description in the main part of the manual.
Nand; ^&Bitwise Not And operator. See the description in the main part of the manual.
Or; ||Bitwise Or operator. See the description in the main part of the manual.
Nor; ^|Bitwise Not Or operator. See the description in the main part of the manual.
Xor; ^^Bitwise Xor operator. See the description in the main part of the manual.
ModModulus operator. See the description in the main part of the manual.

Examples:
7 + 3 + 6 * 18.7
x& and (not bit(7))
ZZ mod (x% + 8)


Operator Precedence
When 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.

Precedence levelOperator
1unary "+"; unary "-"; Not
2^
3*; /; \; \\; Mod
4+ (addition); - (substraction)
5<; <=; >; >=; =; ==; <>; != << (strings); >> (strings)
6<< (shift left); >> (shift right)
7And; Or; Xor; Nand; Nor

Example: Consider the following expression:
20 - 4 + 3 * (24 / (7 + 1) + 2)
The following shows the series of operations that FutureBasic 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
Many FutureBasic 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:
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)


Floating Point Number Display
Controlling the number of floating point digits displayed is performed by setting the global runtime variable "gFBFloatMaxDigits" to the desired value. It governs the number of significant digits kept during conversion of a floating point value to a string. This conversion is the basis of the str$() function. An identical conversion occurs when you print a floating point value.
The default value of gFBFloatMaxDigits (10) can be changed to suit:
gFBFloatMaxDigits = 3 // or 15 or whatever
Changing the value of gFBFloatMaxDigits has no effect on the precision of numerical calculations.
For more precise control of number display, see the using function.