Arithmetic operators in PocketBuilder

Description

The following table lists the arithmetic operators used in PocketBuilder.

Table 4-1: PocketBuilder arithmetic operators

Operator

Meaning

Example

+

Addition

Total=SubTotal+Tax

-

Subtraction

Price=Price–Discount

Unless you have prohibited the use of dashes in identifier names, you must surround the minus sign with spaces.

*

Multiplication

Total=Quantity*Price

/

Division

Factor=Discount/Price

^

Exponentiation

Rank=Rating^2.5

Usage

Operator shortcuts for assignments For information about shortcuts that combine arithmetic operators with assignments (such as ++ and +=), see Assignment.

Subtraction If the option Allow Dashes in Identifiers is checked on the Script tab in the Options dialog box, you must always surround the subtraction operator and the -- operator with spaces. Otherwise, PocketBuilder interprets the expression as an identifier.

For information about dashes in identifiers, see “Identifier names”.

Multiplication and division Multiplication and division are carried out to full precision (16–18 digits). Decimal numbers are rounded (not truncated) on assignment.

Calculation with NULL When you form an arithmetic expression that contains a NULL value, the expression’s value is null. Thinking of null as undefined makes this easier to understand.

For more information about null values, see “NULL values”.

Errors and overflows The following problems can occur when using arithmetic operators:

Examples

Subtraction This statement always means subtract B from A:

A - B

If DashesInIdentifiers is set to 1, the following statement means a variable named A-B, but if DashesInIdentifiers is set to 0, it means subtract B from A:

A-B 

Precision for division These examples show the values that result from various operations on decimal values:

decimal {4} a,b,d,e,f

decimal {3} c

a = 20.0/3           // a contains  6.6667

b = 3 * a            // b contains 20.0001

c = 3 * a            // c contains 20.000

d = 3 * (20.0/3)       // d contains 20.0000

e = Truncate(20.0/3, 4)   // e contains  6.6666

f = Truncate(20.0/3, 5)   // f contains  6.6667

Calculations with null When the value of variable c is null, the following assignment statements all set the variable a to null:

integer a, b=100, c


SetNULL(c)


a = b+c  // all statements set a to NULL

a = b - c

a = b*c

a = b/c

Overflow This example illustrates the value of the variable i after overflow occurs:

integer i

i = 32767

i = i + 1     // i is now -32768