# Data Types, Literals and Variables

Numeric Values |

The numeric data type defines values that are used primarily in mathematical operations. In PxPlus, these values can appear in the form of literals, variables, expressions, functions and arrays from 1 to 3 dimensions. When numeric data is output, it can include formatting, such as commas (see 'TH'=), decimal points (see 'DP'=), and currency symbols (see 'CU'=). See SET_PARAM directive and System Parameters.

The most common format for a numeric value is as a simple number consisting of a sign, followed by series of digits and optional decimal point:

> 7 3.1415 -13.210 .333 -934

By default, PxPlus maintains a precision of 2 (digits to the right of the decimal). During calculations, numeric values are rounded to the currently set precision. The default can be reset (up to 18 digits) using the PRECISION directive.

The rounding of numeric values is controlled using the ROUND directive. If rounding is set to the default, a divide operation in PxPlus will maintain the precision of the starting value.

Example:

> 1014.475/100 becomes 10.14475 which gets rounded to 10.145

The automatic rounding of intermediate results can be turned off by setting the 'NR' parameter. Various other types of rounding can be controlled using the 'RN'= parameter.

Use the FLOATING POINT directive to set numeric data to scientific notation. Floating point values take the following format:

> {+-} x.xxxxx E {+-} nn

Where:
x.xxxxx is a number multiplied by 10 raised to the power of nn. The following numeric values are expressed in different formats:

| 3 | = 3E+00 | = 0.3E+01
| 2.78 | = 0.278E+01 | = 278E-02
| 1000 | = 1E+03 | = 0.1E+04

#### Note:
When using scientific notation, the + after the E is optional, and if omitted, the results will be the same as including the +; e.g. 0.2E10 is the equivalent of 0.2E+10.

## Numeric Variables

The initial value of any numeric variable is 0 (zero). Numeric variables that are named with a trailing % (percent sign) are restricted for use with integers only; e.g. COUNT%. If set to a fractional value, the fractional part will be truncated.

Example:

> x%=1.8
> ?x%
> 1

PxPlus includes a set of system variables that provide access to internally defined numeric values, such as time, memory size, etc. These may be referenced like any other variable but can never be altered by the program.

For the complete list, see System Variables.

## Numeric Arrays

Numeric arrays provide the ability to handle numeric lists, tables or matrices. Arrays are created using the DIM directive. This directive defines the array's name, number of dimensions (one, two or three), and minimum to maximum subscript in each dimension.

The names given to arrays are completely independent of the names associated with numeric variables; e.g. a variable named X1 would have no relationship to an array with the name X1. However, the conventions regarding the naming of numeric variables apply as well to array variables. Unless specified, arrays are zero-based.

DIM X[4] yields a one-dimensional array X with 5 elements:

X[0] | X[1] | X[2] | X[3] | X[4]

DIM X[1:4] defines a one-dimensional array X with 4 elements:

X[1] | X[2] | X[3] | X[4]

DIM Y[2,5] defines a two-dimensional array Y with 18 elements:

Y[0,0] | Y[0,1] | Y[0,2] | Y[0,3] | Y[0,4] | Y[0,5]
Y[1,0] | Y[1,1] | Y[1,2] | Y[1,3] | Y[1,4] | Y[1,5]
Y[2,0] | Y[2,1] | Y[2,2] | Y[2,3] | Y[2,4] | Y[2,5]

DIM Y[1:2,1:5] defines a two-dimensional array with 10 elements:

Y[1,1] | Y[1,2] | Y[1,3] | Y[1,4] | Y[1,5]
Y[2,1] | Y[2,2] | Y[2,3] | Y[2,4] | Y[2,5]

Access to an entry within a numeric array is specified using the array name, followed by array subscripts (contained within square or round parentheses). The subscripts may be specified as Literals, Variables or Numeric Expressions.

Example:

> A(3) CUST[6,A] TABLE(A*5) Z(3,A(M,N,O))

Attempting to use non-integer subscript results in an Error #41: Invalid integer encountered (range error or non-integer). Specifying subscripts on a variable for which no array has been defined yields an error.

Use the DIM( ) function to determine information about array dimensions.

Example:

> DIM X[1:10]
> PRINT DIM(READ NUM(X)) ! Read total number of elements
> 10
> PRINT DIM(READ MIN(X)) ! Read minimum element number
> 1
> PRINT DIM(READ MAX(X)) ! Read maximum element number
> 10

To access a range of entries, specify the array name, followed by subscripts ranging from: to (or ALL) enclosed in braces instead of parentheses.

Example:

| Assigning a value to an entire array: | LET A{ALL}=10
| Assigning a value to a range of array elements: | LET A{1:5}=10
| Copying the contents of one array into another: | LET A{ALL}=B{ALL}
| Assigning a range of values from one array to another: | LET A{1:5}=A{6:10}

The element positions in an array can be shifted/rearranged using a combination of subscripts (literals, variables, expressions and ranges).

Example:

| Shift up by deleting at subscript P: | X{P:ElementCount}=X{P+1:ElementCount+1}
| Shift down by inserting at subscript P: | X{ElementCount+1:P+1}=X{ElementCount:P}
|  | X[P]=NewValue

## Dynamic Arrays

The DIM statement can also be used to create dynamic numeric arrays by using an * (asterisk) as subscript_1 in the DIM directive. This type of array will have no upper bound; that is, elements can be appended dynamically without the need to predefine the number of subscripts in the array. Dynamic arrays can only contain a single dimension and always have a base offset of 1 (i.e. 1 is the first element in the array).

When assigning values to an array element, if the element does not exist but the index specified is one above the current maximum subscript, the element will be appended to the array. Alternately, the subscript of [*] may be used to explicitly append to the array.

Referencing invalid elements in the array (elements less than 1 or more than 1 beyond the current maximum) will result in a subscript error, as per normal arrays.

Example:

The following would be used to define a dynamic array X:

> DIM X[*]

Once defined, elements can be appended to the array either by sequential numbering the subscript starting with 1 or by using an * (asterisk) as the subscript. For example, to append an element to the end of the array X as defined above, you would simply code X[*] = value. This would append a new element to the array and assign it the value specified.

You can code the specific element in the code and if the subscript is equal to the next higher element in the array, it will be appended. Attempting to append to dynamic arrays with an element that is greater than the maximum plus 1 will result in an Error #42: Subscript out of range/Invalid subscript.

Example:

> DIM CUST$[*]
> SELECT * FROM "Custfile" WHERE Amt_OWING>1000
> CUST$[*] = CustId$
> NEXT RECORD

The above example will load an array with the customer IDs for all customers owing more than $1,000. Using Dynamic arrays simplifies the application logic when the number of elements needed is not known in advance.

#### Note:
When arrays are defined as static, you cannot switch to dynamic.

(Dynamic arrays were added in PxPlus v10.00.)

## Numeric Expressions

A numeric expression can consist of numeric constants, variables, functions, and/or other numeric expressions each separated by arithmetic or logical operators. See Footnotes below for information on the listed numeric operations.

The following operators are grouped by order of precedence (see Footnote c):

Auto-Increment/Decrement (see Footnote d)

++ | ++var1 pre-increments by 1, var1++ post-increments by 1.
- - | --var1 pre-decrements by 1, var1-- post-decrements by 1.

Exponentiation (see Footnote e)

^ | A ^ B raises A to the power of B (** is equivalent to ^).

Multiplication, Division, Modulus (see Footnote e)

* | A * B multiplies A by B.
/ | A / B divides A by B.
| | A | B remainder resulting when A is divided by B.

Addition, Subtraction (see Footnote e)

+ | A + B adds A to B.
- | A - B subtracts B from A.

Relational (see Footnote f)

= | A = B yields 1 if A and B are equal, else yields 0 zero.
< | A < B yields 1 if A is less than B, else yields 0 zero.
> | A > B yields 1 if A is greater than B, else yields 0 zero.
<> | A <> B yields 1 if the A and B are not equal, else yields 0 zero.
<= | A <= B yields 1 if A is less than or equal to B, else yields 0 zero.
>= | A >= B yields 1 if A is greater than or equal to B, else yields 0 zero.

Logical (see Footnote g)

AND | A AND B yields 1 if both values are non-zero, else yields 0 zero.
OR | A OR B yields 1 if either values are non-zero, else yields 0 zero.

## Footnotes

c - Precedence

If two operators of equal precedence occur, execution takes place left to right. In the following expression …

> A + B - C * D

… C and D are first multiplied together and the result saved; A and B are then added together; and finally, the saved value (C*D) is subtracted.

Round parentheses ( ) may be used to change the order of evaluation. The expressions within parentheses are evaluated first.

Where parentheses are nested, as in the following expression …

> (A ^ (B * (A + 2)))

… the innermost parenthesized expression (A + 2) is evaluated first. The use of parentheses is encouraged in complex expressions in order to make these expressions more readable and easier to understand.

d - Auto-Increment/Decrement

If an error occurs during execution of a directive, no increment or decrement (++ or - -) takes place. This is true for both pre- and post- operations.

With pre-increment/decrement, the value returned is the value of the variable after the increment or decrement. With post-increment/decrement, the value returned is the value of the variable before the increment or decrement. After your directive is executed, your variable is incremented or decremented by 1.

Example:

> A=0, Y$=""
> READ (1,IND=A++,ERR=*NEXT)X$; Y$+=X$; GOTO *SAME

e - Assignment Operators

PxPlus supports an alternate notation for defining numeric expressions that result in assignments. A combination of a numeric operator ( + - * / | ^ ) with an = equals sign is used to form shorthand expressions; e.g. A+=1 is equivalent to the expression A=A+1, and B^=A is equivalent to the expression B=B^A. See Assignment Operators.

f - Relational Operators

<>, <=, and >= may be entered as ><, =<, and => respectively.

g - Logical Operators

When PxPlus encounters either an AND or an OR logical operator, it attempts to perform a shortcut in the evaluation of the expression. If the value to the left of an AND operator is zero (false), then the expression/value on the right is skipped and the relationship returns 0 (zero). If the value to the left of an OR is non-zero, then the expression/value on the right is skipped and the relationship returns 1.

## Numeric System Functions

PxPlus includes various internal functions that return numeric values based on the parameters provided. System functions can be used to evaluate or convert specific values, but many of them perform built-in mathematical (arithmetical or algebraic) calculations. These include trigonometry ( SIN( ), COS( ), TAN( ), ASN( ), ACS( ), ATN( ) ), logical comparison ( AND( ), IOR( ), XOR( ), NOT( ) ), and several other numeric operations ( EPT( ), EXP( ), LOG( ), MAX( ), MIN( ), MOD( ), PRC( ), SQR( ) ).

See System Functions.