# CPT — Compute

Source: https://rungs.dev/docs/ladder-logic/instructions/cpt

> CPT (Compute) works out a whole expression in one box and stores the answer in a tag. Covers operator order, the REAL promotion rule, and why it is not a one-shot.

Works out an expression and stores the answer in a tag. Runs on every scan the rung is true, not
once per rising edge.

Everything `CPT` does you could also do by chaining `MUL`, `DIV` and `ADD` boxes through
scratch tags. `CPT` lets you write it on one line instead:

```
CPT(Result, (Speed * 60) / PulsesPerRev)
```

## Operands [#operands]

| Name         | Type                       | What it is                                   |
| ------------ | -------------------------- | -------------------------------------------- |
| `dest`       | `SINT` `INT` `DINT` `REAL` | Tag the answer is stored in                  |
| `expression` | `SINT` `INT` `DINT` `REAL` | Tags and typed-in values joined by operators |

The destination has to be a tag — a number or another expression cannot be written to. A `BOOL`
destination is not allowed either: use [`CMP`](https://rungs.dev/docs/ladder-logic/instructions/cmp) when the answer is a yes or no.

## How It Works [#how-it-works]

| Rung       | What happens                                             |
| ---------- | -------------------------------------------------------- |
| Rung true  | Works out the expression and stores the answer in `dest` |
| Rung false | Nothing. `dest` keeps the value it had                   |

**`CPT` is not a one-shot.** Hold the rung true for ten scans and it works the expression out ten
times and stores ten times. If you want it to run once per button press, put a
[`ONS`](https://rungs.dev/docs/ladder-logic/instructions/one-shot-ons) in front of it.

## What you can write in the expression [#what-you-can-write-in-the-expression]

`+` `-` `*` `/` `MOD`, brackets, a negative sign, `**` to raise to a power, the bitwise
operators `AND` `OR` `XOR` `NOT`, and the functions `ABS` `SQRT` `TRUNC` `LN` `LOG` `DEG` `RAD`
`SIN` `COS` `TAN` `ASIN` `ACOS` `ATAN` `ATAN2`.

You can read an element of an array (`Samples[2]`, and the index can be an expression of its own
like `Samples[i + 1]`), and a member of a structure (`Tmr.ACC`).

You **cannot** write a comparison — `Count > 5` — because a comparison answers yes or no and
`CPT` has to store a number. Put those in a [`CMP`](https://rungs.dev/docs/ladder-logic/instructions/cmp) box.

The full operator order, the whole-expression decimal rule and the operators that do not behave
the way maths does are on the [Expressions in Ladder](https://rungs.dev/docs/ladder-logic/expressions) page. The short version:

- `-2**2` is `-4`, because the power runs before the minus sign
- `2**3**2` is `64`, because two powers run left to right
- one decimal anywhere makes the whole expression decimal, so `7/2*1.5` stored in a `DINT` is `5`

## Example — Motor RPM [#example--motor-rpm]

A sensor gives pulses per second and the motor gives a fixed number of pulses per revolution.
Turn that into RPM.

```ladder
XIC(In_Running)CPT(Sts_MotorRpm, (In_PulsesPerSec * 60) / Cfg_PulsesPerRev)

In_Running = 1
In_PulsesPerSec = 250
Cfg_PulsesPerRev = 100
```

`In_PulsesPerSec` is 250 and `Cfg_PulsesPerRev` is 100, so the answer is 15000 / 100 = 150 RPM.

Watch the order. Writing `In_PulsesPerSec / Cfg_PulsesPerRev * 60` would work out 250/100 first,
and because both are whole numbers that is 2, not 2.5 — giving 120 RPM instead of 150. Multiply
before you divide when you are working in whole numbers.

## Common Mistakes [#common-mistakes]

- Expecting it to run once per press. It runs every scan the rung is true. Add a
  [`ONS`](https://rungs.dev/docs/ladder-logic/instructions/one-shot-ons) if you want one.
- Dividing whole numbers and losing the fraction: `7 / 2` is `3`. Store into a `REAL`, or bring
  a decimal into the expression, if you need `3.5`.
- Writing a comparison in it. `CPT` stores a number; [`CMP`](https://rungs.dev/docs/ladder-logic/instructions/cmp) decides a rung.
- Reaching for `CPT` for a single add. `ADD(A, B, Result)` reads better than `CPT(Result, A + B)`
  when there is only one operation.

## Related [#related]

- [CMP](https://rungs.dev/docs/ladder-logic/instructions/cmp) — the same expressions, used to decide a rung
- [Expressions in Ladder](https://rungs.dev/docs/ladder-logic/expressions) — operator order, decimals, and the traps
- [Math instructions](https://rungs.dev/docs/ladder-logic/instructions/math) — one operation per box