> For the complete documentation index, see [llms.txt](https://help.qtech.co.nz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-06-arithmetic.md).

# Module 06: Arithmetic functions

Add, subtract, multiply and divide analogue values, and use the analogue accumulators ANLACC1 and ANLACC2.

The DLP language has a set of simple arithmetic functions to allow the programmer to carry out basic mathematical operations while working with variables in the DLP.

This module discusses the functions available for carrying out common arithmetic operations and introduces two system registers, the Analogue Accumulators.

This module contains help on:

* the analogue accumulators, `ANLACC1` and `ANLACC2`
* addition: `ADDR` and `ADDS`
* subtraction: `SUBR` and `SUBS`
* multiplication: `MULR` and `MULS`
* division: `DIVR` and `DIVS`
* adding or taking away 1: `INCANL` and `DECANL`

Before you start, complete [Module 05](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-05-pulse-counting.md).

## Step 1: Understand the analogue accumulators

While the logic accumulator is useful in carrying out operations in the DLP, often the programmer will require that the result of a function in a DLP won't be a logic True or False. Some functions exist that trigger the RTU to do something specific, such as sending a COS back to the base station, or the result will be a number, as in the case of most arithmetic functions.

The RTU maintains two analogue system registers called the Analogue Accumulators, `ANLACC1` and `ANLACC2`.

* `ANLACC1` is used to store the primary result of arithmetic functions.
* `ANLACC2` is used to store (when appropriate) things like a remainder or an overflow.

To use a result, copy it with `CPANL`, for example `CPANL ANLACC1, NAIN1, 1`.

## Step 2: Add with ADDR and ADDS

As with the analogue comparison functions outlined in [Module 04](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-04-analogue-setpoints.md), different commands must be used depending on whether you are adding a register to another register (`ADDR`) or to a hard-coded constant setpoint (`ADDS`). In either case, the result is placed in `ANLACC1`. If the result is greater than 65535, then 1 is placed in `ANLACC2` to signify 65536 and the rest is placed in `ANLACC1`.

| First number | Second number | Actual result | ANLACC1 | ANLACC2 |
| ------------ | ------------- | ------------- | ------- | ------- |
| 50,000       | 10,000        | 60,000        | 60,000  | 0       |
| 60,000       | 5535          | 65,535        | 65,535  | 0       |
| 60,000       | 5536          | 65,536        | 0       | 1       |
| 60,000       | 10,000        | 70,000        | 4464    | 1       |

See [Module 05](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-05-pulse-counting.md#step-2-count-pulses-with-the-pulse-counters).

## Step 3: Subtract with SUBR and SUBS

As with the analogue comparison functions outlined in [Module 04](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-04-analogue-setpoints.md), different commands must be used depending on whether you are subtracting a register from another register (`SUBR`) or subtracting a hard-coded constant setpoint (`SUBS`). In either case, the result is placed in `ANLACC1`. If the result is less than 0, then 0 is placed in both `ANLACC1` and `ANLACC2`.

| First number | Second number | Actual result | ANLACC1 |
| ------------ | ------------- | ------------- | ------- |
| 50,000       | 10,000        | 40,000        | 40,000  |
| 100          | 100           | 0             | 0       |
| 100          | 200           | −100          | 0       |

## Step 4: Multiply with MULR and MULS

As with the analogue comparison functions outlined in [Module 04](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-04-analogue-setpoints.md), different commands must be used depending on whether you are multiplying a register by another register (`MULR`) or by a hard-coded constant setpoint (`MULS`). In either case, the result is placed in `ANLACC1`. If the result is greater than 65535, then the result is calculated as a 32-bit unsigned number where `ANLACC2` is the most significant word. In other words the result can be found by (`ANLACC2` \* 65536 + `ANLACC1`).

| First number | Second number | Actual result | ANLACC1 | ANLACC2 |
| ------------ | ------------- | ------------- | ------- | ------- |
| 30,000       | 2             | 60,000        | 60,000  | 0       |
| 60,000       | 2             | 120,000       | 54464   | 1       |
| 60,000       | 20            | 1,200,000     | 20352   | 18      |

## Step 5: Divide with DIVR and DIVS

As with the analogue comparison functions outlined in [Module 04](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-04-analogue-setpoints.md), different commands must be used depending on whether you are dividing a register by another register (`DIVR`) or by a hard-coded constant setpoint (`DIVS`). In either case, the result is placed in `ANLACC1`. If the first number is not evenly divisible by the second number, the remainder is placed in `ANLACC2`.

| First number | Second number | Actual result | ANLACC1 | ANLACC2 |
| ------------ | ------------- | ------------- | ------- | ------- |
| 10           | 2             | 5             | 5       | 0       |
| 15           | 2             | 7.5           | 7       | 1       |
| 33           | 7             | 4.7142        | 4       | 5       |

## Step 6: Add or take away 1 with INCANL and DECANL

Frequently we simply want to increment or decrement an analogue register in response to a certain condition. To do so using `ADDS` or `SUBS` would be a little cumbersome as we would first have to add a "1" to the register and then as a separate operation, then use `CPANL` to copy the result from `ANLACC1` back into the original register.

The good news is that we can use `INCANL` or `DECANL` to increment a nominated analogue register and not have to worry about copying analog accumulators.

* If a register is already at 65535 and the DLP tries to increment it using `INCANL`, the value will stay at 65535.
* If the register is at 0 and the DLP tries to decrement it using `DECANL`, the value will remain at 0.

See [Module 05](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-05-pulse-counting.md) example 1.

## Try it

To show a 32-bit result in DATRAN VI, see [Show a 32-bit result in DATRAN VI](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/instruction-reference/analogue.md#show-a-32-bit-result-in-datran-vi).

## Key points

* `ANLACC1` is used to store the primary result of arithmetic functions, while `ANLACC2` is used to store (when appropriate) things like a remainder or an overflow.
* We can use `INCANL` or `DECANL` to increment a nominated analogue register and not have to worry about copying analog accumulators.

**Next:** [Module 07: Logic operations and jumps](https://help.qtech.co.nz/datran/datran-vi-scada/datran-dlp-ide/dlp-self-help-course/module-07-logic-and-jumps.md)
