
<<
Index
>>

FutureBasic

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
 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 userdefined function or toolbox function that returns a numeric value.
Examples:
fn TheSum
fn ControlIntegerValue(tag)
 A value returned by any builtin FutureBasic function whose name does not end with "
$
". (Note the two exceptions to this are the using
function and the str#
function, both of which return a string value.)
Examples:
len(@"Hello")
dblval(string)
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 [a]. 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:
 A pair of numeric expressions;
 A pair of string expressions (see Appendix E);
 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(string) * 3
"Bronson" <> theName$(7,4)
record1 = record2
II [b]. 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:
Operator  expr1 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.
Operator  Operator expr returns: 
+  expr 
  the negative (additive inverse) of expr 
Not  the 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. Example:
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. 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.
Math
+  Addition. 
  Subtraction. 
*  Multiplication. 
/  Division. If both operands are integer expressions, this operator does integer division. If either operand is a real number, the operator does floating point division. 
\  Floating Point Division. The operator always does floating point division. Integer operands are converted to floating point before the division. 
\\  Identical to / 
Math Shortcuts
+=  Add the expression from the right of the equal sign to the variable on the left. Example: (a += b) is the same as coding (a = a + b) 
=  Subtract the expression from the right of the equal sign from the variable on the left. Example: (a = b) is the same as coding (a = a  b) 
*=  Multiply the left expression by the right expression and store the result in the left expression. Example: (a *= b) is the same as coding (a = a * b) 
/=  Divide the left expression by the right expression and store the result in the left expression. Example: (a /= b) is the same as coding (a = a / b) 
Assignment and Comparison
=  Assignment. (Also works for comparison, however for clarity == is preferred when comparing values.) Example: x = 5 
==  Comparison. Example: if ( x == 5 ) then y = 2 * x 
Increment and Decrement
++  Increment a variable. Example: i++ 
  Decrement a variable. Example: i 
Exponentiation
^  Exponentiation (raising to a power). Example: 100^2 == 10000. 
Bitshifting
<<  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. 
Logic — Bitwise
and; &  Bitwise 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. 
nand; ^&  Bitwise Not And (Nand) 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. 
Logic — Boolean
&&  Logical And operator. See the description in the main part of the manual. 
  Logical Or operator. See the description in the main part of the manual. 
Modulus
mod  Modulus 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 Level
1  unary "+"; unary ""; not 
2  ^ 
3  *; /; \; \\; mod 
4  + (addition);  (substraction) 
5  <; <=; >; >=; =; ==; <>; != << (strings); >> (strings) 
6  << (shift left); >> (shift right) 
7  and; 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

204 = 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:
 Integer literals;
 Symbolic constants;
 The sizeof, offsetof and typeof 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)
Controlling Floating Point Number Conversion to a Pascal String
The global runtime variable "gFBFloatMaxDigits" governs the number of significant decimal ( i.e. fractional ) digits kept during conversion of a floating point value to a pascal string.
Typically the floating point value is converted using FB's str$() function.
Other FB keywords such as print # also consult gFBFloatMaxDigits.
gFBFloatMaxDigits' default value of 10 can be changed to suit but its upper limit is effectively governed by the precision of the float variable:
gFBFloatMaxDigits = 3 // or 15 or whatever
An app needing more than 15 decimal digits of precision should consider FB's DecimalNumber or Cocoa's NSDecimalNumber.
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.
Note: Converting floats (singles and doubles) to CFStrings is typically handled with string format specifiers and isn't covered here.