F Code - Mathematical Functions

Overview

The F code processors perform specified operations on one or more operands that have been pushed on a stack.

The F code processors use the Reverse Polish notation system. Reverse Polish is a postfix notation system where the operator follows the operands. The expression for adding two elements is "a b +".  (The usual algebraic system is an infix notation where the operator is placed between the operands, for example, "a + b".)

Note

Many users find Reverse Polish notation difficult to understand. It is therefore strongly recommended that you use the A conversion code in preference to the F code.

There are three forms of the F code:

F Uses only the integer parts of stored numbers unless a scaling factor is included. This code uses a non-standard order of operation unless a custom option changes it.

FS Uses only the integer parts of stored numbers. It uses the order of operation specified in the SMA standard.

FE Uses both the integer and fraction parts of stored numbers. It uses the order of operation specified in the SMA standard.

The F code has operators to push operands onto the stack. Operands that are pushed onto the stack may be constants, attribute values, system parameters such as date and time, and counters such as item counters.

Other operators perform arithmetic, relational, and logical operations on stack elements. There are also concatenation and string operators.

Limitation

Input conversion is not allowed: the processor displays an error message if attempted.

Syntax

F{n};elem{;elem}...

or

FS;elem{;elem}...

or

FE;elem{;elem}...

Syntax Elements

n is a number (1-6) to convert a stored value to a scaled integer. That is, the value's explicit or implied decimal point is moved n digits to the right with zeros added if necessary. Any digits to the right of this nth position are truncated.

elem is a valid operator. Operators are described in the sections that follow.

Stack Size

The stack can hold 80 entries.

Overview of Operators

The operators that you can use in F code elements are arithmetic, relational, and logical operators, the concatenation operator, and special operators. Each type is described in a separate section.

Order of Operations

To visualize the order of operations for operators that work on the top two stack entries, think of the stack entries and operators as though they were in the following algebraic infix order:

For F;

Entry 1 operator Entry 2

For FS; and FE;

     Entry 2 operator Entry 1

All operators pop the entries that they use and, except for the pop operator, push their result into Entry 1.

The Pushdown Stack

The F code processors operate on a pushdown stack. The operators push values on the stack, perform arithmetic and other operations on stack entries, and pop values off the stack.

The following diagram illustrates how stack entries would be pushed on the stack and popped off.

A value that is pushed onto the stack is put into stack Entry 1; to make this possible, any current value in Entry 1 is pushed down into Entry 2, and so on. Similarly, when a value is retrieved from the stack, it is popped from Entry 1; the value in Entry 2 is moved into Entry 1, and so on.

When the function processor encounters a push operator, it pushes the appropriate value onto the stack in Entry 1. For other functions (for example, arithmetic, relational or logical) the operands are popped off the stack, the calculation is performed and the result is pushed back.

Example

The mathematical notation "(1+2)*4=12" is processed by the F code processor as illustrated below.

Step 1: C4 The value 4 is pushed onto the stack.

Step 2: C2 The value 2 is pushed onto the stack.

Step 3: C1 The value 1 is pushed onto the stack.

Step 4: + The top two entries are popped off the stack and added together. The result (3) is pushed onto the stack.

Step 5: * The top two entries are popped off the stack and multiplied together. The result (12) is pushed onto the stack.

Push Operators

A push operator always pushes a single entry onto the stack. Existing values, if any, are moved down one position. Valid operators are as follows:

attribute-number{R{R}}{(format-code)}
Value of the attribute with the following options:

R specifies that the first value of the attribute is to be used repeatedly when evaluating a multivalued attribute.

RR specifies that the first subvalue is to be used repeatedly.

(format-code) any format code enclosed in parentheses is applied to the value from the specified attribute before it is pushed on the stack.

Note

Refer to the A code MultiValues section for details of how to use R and RR.

CnConstant, where. n is a string constant of any length up to the next semicolon or system delimiter.

D Current date.  Specifies that the system date is to be pushed onto the stack (internal format).

'literal' or "literal" Literal.

NA Number of attributes. Specifies that the number of attributes in the item is to be pushed onto the stack.

NB Current break level. Specifies that the current break level number is to be pushed onto the stack:

0 = detail line
1 = lowest break level
255 = GRAND-TOTAL line

ND Number of items since the last BREAK on a BREAK data line. This is equal to the item counter on a GRAND-TOTAL line. Used to compute averages.

NF The file specifier in the format:

{DICT }{/account/}file-name{,section-name}

NFA The account name part (if any) of the entered file specifier, in the format:

/account/

NFD The string "DICT " if a dictionary access; otherwise a null string.

NFF The entered file specifier, in the format:

{/account/}file-name

that is, with the DICT and section-name strings, if any, removed.

NFN The file name part of the entered file specifier - that is, with the DICT, account name and section name strings, if any, removed.

NFS The section name part of the entered file specifier, in the format:

,section-name

NI Item counter.  Specifies that the value of the current item counter is to be pushed onto the stack.

NL Item Length. Specifies that the length of the item (in bytes) is to be pushed onto the stack. The length includes the item-id with its terminating attribute mark and the item body with all its attribute marks. It does not include the segment mark or optional pad that follow the item, the item count field, nor the other front matter.

NS Subvalue counter. Specifies that the value of the current subvalue counter is to be pushed onto the stack.

NTTime of last update. Specifies that the time of update of the last record read (specified as the number of milliseconds since midnight) is to be pushed onto the stack.

NU Date of last update. Specifies that the date of update of the last record read (specified as the number days since 31 December 1967) is to be pushed onto the stack.

NV Value counter. Specifies that the value of the current value counter is to be pushed onto the stack.

P Duplicate of Entry 1 is pushed on top of the stack.

T System time. Specifies that the current time is to be pushed onto the stack (internal format).

V or LPVPrevious Value. Specifies that the value from the previous format code is to be pushed onto the stack.

Arithmetic Operators

The arithmetic operations for the F code processors include multiplication, division, remaindering, addition, subtraction, integer function, and summation of previous operations. The operands are popped from the stack and the result pushed onto the stack as Entry 1

The following operators perform arithmetic on the top two entries:

*{n} Multiply the top two stack entries. If n is specified, the result is divided by 10 raised to the power of n.

/ Divide stack entries:

F Divide Entry 1 by Entry 2

FS, FE Divide Entry 2 by Entry 1

Note

Division by zero returns the dividend.

R Return the remainder to the top of the stack:

F Remainder of Entry 1/Entry 2

FS, FE Remainder of Entry 2/Entry 1

Note

If the divisor evaluates to 0, the result is 0.

+ Add the top two entries in the stack.

- Subtract stack entries:

F Subtracts Entry 2 from Entry 1 (Entry 1 - Entry 2)

FS, FE Subtracts Entry 1 from Entry 2 (Entry 2 - Entry 1)

I Pop Entry 1 and return the integer part to the top of the stack.

S Add together all the values and subvalues that make up stack entry 1 and replace this entry with the result. It is strongly recommended that you use the $ operator to mark the start of the codes that generate the stack entry to be processed by the S operator (see The S Operator for more details).

Note

To reverse the F code arithmetic operations, set the ALTFCORR option in your operating environment. The F code divide (/), remainder (R) and subtract (-) operators will then function like the FS operators.

Miscellaneous Operators

The miscellaneous operators for the F code processors include formatting, exchanging stack entries, popping the top entry, concatenation, and string extraction.

The following are the miscellaneous operators:

_ Exchange the top two entries.

^ Pop Entry number 1 from the stack and discard. All other entries move up the stack by one.

(format-code) Perform the specified format code on Entry 1 and replace Entry 1 with the result.

: Concatenate stack entries:

F Concatenates Entry 1 onto Entry 2 (Entry 1 Entry 2)

FS, FE Concatenates Entry 2 onto Entry 1 (Entry 2 Entry 1)

The two entries are popped from the stack and the result is pushed onto the stack as entry 1.

Note

To reverse the F code concatenation operation, set the ALTFCORR option in your operating environment. The F code concatenation operator (:) functions like the FS operator.

[] Extract a subset from stack Entry 3. The starting column is specified in stack Entry 2 and the number of characters is specified in Entry 1. The three operands are popped from the stack and the result is pushed onto the stack as entry 1.

@ If stack entry 3 evaluates to true, the result is stack entry 2; otherwise, the result is stack entry 1. The three operands are popped from the stack and the result is pushed onto the stack as entry 1.

$ Used to mark the start of the codes that generate a stack entry that will be processed by the S operator.

Relational Operators

Relational operators compare the top two stack entries and produce a true (1) or false (0) result. The operands are popped from the stack and the result pushed onto the stack as Entry 1

The following are the relational operators:

= Equal to

< Less than:

F Entry 1 < Entry 2

FS, FE Entry 2 < Entry 1

> Greater than:

F Entry 1 > Entry 2

FS, FE Entry 2 > Entry 1

[ Less than or equal to:

F Entry 1 [ Entry 2

FS, FE Entry 2 [ Entry 1

] Greater than or equal to:

F Entry 1 ] Entry 2

FS, FE Entry 2 ] Entry 1

# Not equal

Note

The process option ALTFCORR does not reverse the F code relational operators.

Logical Operators

The logical operations combine the top two stack entries. The operands are popped from the stack and the result pushed onto the stack as Entry 1

& AND stack Entries 1 and 2. If both entries are nonzero, the result is 1; otherwise, the result is 0.

! OR stack Entries 1 and 2. If either of the entries is nonzero, the result is 1; otherwise, the result is 0.

Scaler

You can use a scaler with the F format of the F code to convert values stored with an embedded decimal point to integers that can be used by an English processor.

Syntax

F{n};elem{;elem}...

Syntax Elements

n is a number (1-6) to convert the stored value to a scaled integer. That is, the value is padded with zeros or rounded to obtain the specified precision.

elem is any operator as described previously under the associated headings.

Example

This example illustrates the difference between values stored with an embedded decimal point (typically generated through an ALL function) and the values stored for use by an English processor:

                        Stored by
Actual Value ENGLISH ALL
100.23 10023 100.23
566.10000 56610000 566.1

Assume that attribute 13 of an item contains 6.022 and attribute 15 contains 100.34 and the following is entered into the data definition item attribute 8:

F3;15;13;+

The resulting value is 106362.  Note that the numbers with varying decimal places are handled correctly by either padding or rounding.

Multivalues

The F and FS functions handle multivalues in the same way as the A conversion code. Refer to the Multivaluessection in the description of the A code for details and examples.

The S Operator

The S operator adds together all the values and subvalues that make up stack entry 1 and replaces this entry with the result. For example, assuming that attribute 15 contains the values as above and attribute 8 contains the following conversion code:

F;$;15;S;

after the F code is processed, the resulting value in stack entry 1 is 140 (that is the sum of all the values in attribute 15).

Note

The use of the $ operator to mark the start of the codes to be processed by the S operator is strongly recommended.

A more complex example would be:

F;"2";$;10;15;+;S;*;

This does the following:

  1. The literal value "2" is pushed into the stack.
  2. The $ operator marks the start of the codes to be processed by the S operator.
  3. The contents of attributes 10 and 15 are pushed onto the stack (it is assumed for the purposes of this example, that these attributes contain multivalued data as defined above).
  4. The separate values in the top two stack entries are added to form a multivalued result as in the example above. Stack entry 1 now contains 25]40]55]50.
  5. The S operator adds these values together. Stack entry 1 now contains 170 (25+40+55+50). Note that the $ operator ensures that values lower on the stack are excluded from this calculation.
  6. The top two stack entries are multiplied together. The stack now contains the single entry 340 (170 * 2).

Format Codes

You can use the format codes in a function in two ways. One is to transform the contents of an attribute before it is pushed on the stack.  The other way is to transform the top entry on the stack.

How to Use

To process an attribute before it is pushed on the stack, follow the AMC with the format codes enclosed in parentheses.

To process the top entry on the stack, specify the format codes within parentheses as an operation by itself.

To specify more than one format code in one operation, separate the codes with the value mark (CTRL+]).

If you specify a literal in a format code within a function, be sure that any left and right parentheses in the literal are exactly paired.  Otherwise, the processor will reject the function.

Examples

The following function computes the difference in the year fields of the system date and the date stored in attribute 3.  D2/  converts the date from the internal format to a format amenable to the G2/  conversion specification (mm/dd/yy).

F;D(D2/]G2/2);3(D2/]G2/2);-

Another, simpler, way to do the same operation is to use:

F;D(DY);3(DY);-

Examples of Stack Operations

The following examples illustrate the use of various operators.

In the first example, the values in attributes 1-3 of an item are selected and multiplied; constants and concatenation are used; a value is extracted from stack Entry 3. Finally, the values in Entries 1 and 2 are concatenated.

Attribute 8 of the item 'CODE' in the DICT of the PARTS file contains the following function.

F;2;3;*;C-;:;1;C3;C2;[ ];:

Element

Meaning

2

Value AMC 2

3

Value AMC 3

*

Multiply

C-

Load constant "-"

:

Concatenate:
  (F) entry 1 with 2
  (FS) entry 2 with 1

1

Value AMC 1

C3

Starting column

C2

Number of characters

[ ]

Extract string

:

Concatenate:
  (F) entry 1 with 2
  (FS) entry 2 with 1

In the PARTS file, item '1137' contains the following values:

001 S*27-9
002 32
003 21

The following diagram shows the stack contents as the F code is computed. The second diagram shows how the corresponding FS code is computed.

F Code Examples

As an example of format code processing, the F code could be set up as follows:

F;10;11;(T*SALES;X;;3);*

The following processes are performed:

As an example of break processing, the F code could be set up as follows:

F;ND;3;/

The following processes are performed:

The F or FS code operator NL pushes the item length on the stack. For example, a data definition item can be set up as follows:

LENGTH
001  A
002  0                    AMC
003 
004 
005 
006 
007 
008  FS;NL;(MD0,)         Length and Mask Decimal
009  R                    Alignment
010  6                    Column width