# ABS / SQRT / NEG — One-Input Math

Source: https://rungs.dev/docs/ladder-logic/instructions/abs-sqrt-neg

> Reference for the one-input Ladder Logic math instructions in rungs.dev — ABS (absolute value), SQRT (square root), and NEG (negate). Each reads one Source and writes one Dest.

The [math instructions](https://rungs.dev/docs/ladder-logic/instructions/math) you have met so far combine two sources. These three work
on a single value: they read **Source**, transform it, and write the result to **Dest**
every scan the rung is true. Like all math blocks, they live in the **Math** tab of the
ladder toolbar, and Source can be a tag or a typed-in number.

| Instruction | Writes to Dest                     |
| ----------- | ---------------------------------- |
| `ABS`       | Source with the minus sign dropped |
| `SQRT`      | the square root of Source          |
| `NEG`       | `0 − Source` (sign flipped)        |

## ABS — Absolute Value [#abs--absolute-value]

Drops the minus sign: `-5` becomes `5`, and values that are already positive pass through
unchanged. Useful when you care about _how far_ a value is from zero but not which side it
is on — for example, turning a temperature error of `-3.2` degrees into a deviation of
`3.2` for an alarm comparison.

```ladder
ABS(TempError,TempDeviation)
```

## SQRT — Square Root [#sqrt--square-root]

Writes the square root of Source into Dest. A classic use is flow measurement: the
differential pressure across an orifice plate grows with the _square_ of the flow, so the
flow signal is recovered with a square root.

```ladder
SQRT(PressureDrop,FlowRate)
```

If Source is negative, `SQRT` takes the square root of its absolute value instead of
failing — `SQRT(-9.0)` gives `3.0`. This mirrors Logix 5000® controllers. It also means a
sensor glitch below zero produces a plausible-looking number, so if a negative Source
would be a fault in your process, check for it separately.

## NEG — Negate [#neg--negate]

Flips the sign: `7` becomes `-7`, `-2.5` becomes `2.5`. Computed as `0 − Source`, the same
result you would get from `SUB(0, Source, Dest)`.

```ladder
NEG(Offset,InvertedOffset)
```

## Rounding When Dest Is a DINT [#rounding-when-dest-is-a-dint]

Exactly like the [two-source math instructions](https://rungs.dev/docs/ladder-logic/instructions/math): the math itself can produce a
fraction, and storing a fraction into a `DINT` destination rounds it — ties go to the
nearest _even_ number (banker rounding).

| Example                | Dest (`DINT`) | Why                           |
| ---------------------- | ------------- | ----------------------------- |
| `SQRT(3, DintDest)`    | `2`           | 1.732… rounds up              |
| `SQRT(6.25, DintDest)` | `2`           | 2.5 is a tie — rounds to even |
| `ABS(-2.5, DintDest)`  | `2`           | 2.5 tie again                 |
| `NEG(3.5, DintDest)`   | `-4`          | −3.5 tie — even is −4         |

## One Edge Case Worth Knowing [#one-edge-case-worth-knowing]

The most negative `DINT`, `-2147483648`, has no positive twin — the largest positive
`DINT` is `2147483647`. So `ABS(-2147483648)` and `NEG(-2147483648)` both give back
`-2147483648`, still negative, with no error. Real controllers do the same. If a value
can legitimately sit at that extreme, don't rely on `ABS` to make it positive.