`ivi` Arithmetic, etc.

`ivi`

offers a range of arithmetical operations which allow it to
evaluate numerical expressions, to add, subtract, multiply and divide
numbers, and so on. It can also perform numerical checks on record
fields, and make assignments to some fields dependent on the contents of
others (see Editing Records).

### Numerical Expressions

A numerical expression takes the form:

1.73*(3+8-4.6)/5.931

involving numbers, arithmetic operators: + - * / and parentheses. A number by itself, of course, is just a special case of such an expression.

Numbers may be typed in any of the usual forms. A single quote (') indicates a decimal exponent; e.g. 2'29 is 2 * (10 power 29).

Numerical expressions may also contain relational operators: < > = , and Boolean operators: & (AND) ! (OR). These operators return the values True, represented by 1, and False, represented by 0; for example, (4+3)>5 yields True, i.e. 1, while 6.7<2 returns False, i.e. 0.

Some care is advised with the = operator, since all numbers are represented in "floating point" form with finite precision, so that some expressions which appear to have equal value may differ by tiny fractions. `ivi`

attempts to cater for this, but may not be successful in some circumstances.

Any non-permissable character terminates the expression, and this includes any internal space. (Leading spaces, however, are allowed, so that with the operations which follow, it is possible to add a series of numbers separated by spaces.)

### Numerical Expression Templates

Numerical expression templates closely resemble numerical expressions, but permit "field selectors" in place of numbers, for example:

#3*(3+#7-4.6)/5.931

The field selectors (#3, #7) refer to fields of the current record (in either record view) or words relative to the current cursor position (in text view). In an arithmetical operation, they are replaced by the appropriate record fields or words to yield a numerical expression, which can then be evaluated.

More specifically, in text view, #1 is interpreted as the "word" starting at the current cursor position, #2 as the following word, and so on. The words are restricted to the current line.

Boolean expression templates and Boolean expressions are special cases of their numerical counterparts in which the result is True or False. (See Editing Records: Extended Check.)

### Basic Arithmetical Operations

The basic operations return their result in the "floating point (FP) register", which is displayed at appropriate moments on the left of the help/error message line of the display.

**Operation:** FP load

**Default key:** `<ESC> #`

**Operation number:** 42

**Operation:** Add

**Default key:** `<ESC> +`

**Operation number:** 43

**Operation:** Subtract

**Default key:** `<ESC> -`

**Operation number:** 44

**Operation:** Multiply

**Default key:** `<ESC> *`

**Operation number:** 45

**Operation:** Divide

**Default key:** `<ESC> /`

**Operation number:** 46

Each of these operations evaluates a numerical expression starting at the cursor. (This may, of course, be just a number.) The result is then loaded into (added to, subtracted from, multiplied into, divided into) the FP register, which is then displayed on the screen. The cursor stays where it is. This means that a column of numbers can be added (multiplied, etc.) by using the `<down>` key between arithmetical operations; a row of numbers can added, etc. using the `Next` number operation.

**Error Messages:**

FP Syntax Error - the expression is syntactically invalid.

FP Overflow Error - the computed value overflows the register.

The first error commonly arises from the accidental insertion of a space in an expression, and this, in its turn, may well be caused by a space in a record field which is inserted into a template.

The second, a hangover from olden times, may not be seen at all on your computer if it contains a standard floating point chip. The reason is that overflow computations such as 1/0 or 2'100*2'100 no longer raise an error state; instead they return a special "number" representing "undefined" or "infinity", and this is simply displayed as the result of the computation. So we may see:

EVALUATE 1/0 = inf

**Operation:** FP clear

**Default key:** `<ESC> %`

**Operation number:** 50

**Operation:** FP increment

**Default key:** `<ESC> &`

**Operation number:** 49

These operations clear the FP register to zero, and increment it by 1, respectively.

One use of the second, in combination with the FP register to text operation which follows, is to number the lines of a text.

**Operation:** FP register to text

**Purpose:** Copy the contents of the FP register into the current corefile starting at the cursor

**Default Key:** `<ESC> =`

**Operation Number:** 52

The operation inserts or overtypes according to the current submode.

**Operation:** Display FP register

**Purpose:** Display the FP register on the screen.

**Default Key:** None. The user may assign a key with the KEy command.

**Operation Number:** 62

In the standard alphanumeric version of `ivi`

, the FP register shares space on the display with various other items (help/error message, etc.) If one of these overwrites the FP register display, this operation can be used to redisplay it.

**Command:** EValuate

**Purpose:** Evaluate a numerical expression.

**Format:** `EV <string>`

**Parameters:** `<string>` is a numerical expression.

The expression is evaluated and the result displayed on the command line following the parameter. Entering `<RETURN>` or `<DEL>` removes it.

The operation does not affect the FP register.

**Example:**

`EV 1.5+2*3.3+(4.2*5) = 29.1`

**Errors:**

The same as for the basic arithmetical operations.

**Defining and Using Numerical Expression Templates**

**Command:** `EXpression`

**Purpose:** Define a numerical expression template

**Format:** `EX <string>`

**Parameter:** The parameter is the template.

The template is stored for use in the Compute expression operation.

**Operation:** Compute expression

**Purpose:** Evaluate the stored numerical expression template based on the current line or record

**Default Key:** `<ESC> @`

**Operation Number:** 47

Material from the present line or record is substituted into the previously stored numerical expression template, and the resulting expression is evaluated. The result is placed in the FP register and displayed.

**Example:**

Suppose we have carried out `EX (#1+#2)/#3`. Then the operation causes fields 1 and 2 of the current record to be added, and the result to be divided by field 3. (Or, in text view, the number at the cursor to be added to the next number on the line, and the result divided by the third number).

So if the cursor is at the start of a text line:

10 15 20

`<ESC>@` sets the FP register to (10+15)/20 = 1.25.

This can be used, for example, for computations based on numbers entered in a text file, in the manner of a spreadsheet. The operation is applied to each line, with the result copied back at the end of the line.

**Warning.** As the description implies, it is expected that the template will contain arithmetic operators, parentheses and perhaps some fixed numbers, while the record (or text) will contain numbers to replace the field selectors. However, `ivi`

makes no attempt to check this. The text from the cursor line is simply merged into the template to form an expression. If the user wishes to create some effect by having operators and/or parentheses in the text line or record fields, so be it!

The merged expression follows the rules for numerical the expressions given earlier. In particular, it terminates at the first non-valid character.