Math (output instruction)

NEG: Negate

Writes A with its sign flipped.

Also called: negate, sign flip, twos complement.

NEG(number, a, real)

Arguments

NameWhat it isAcceptsDefault
destinationWhat the instruction acts ona registerrequired
aSourcea number or a tag0
realFloating pointtrue or falsefalse

Arguments are positional and this table is generated from the simulator's own instruction registry, so it always matches what the engine accepts.

What it does

NEG turns a positive value negative and a negative one positive. It is arithmetic, and nothing to do with bits or edges despite what the name suggests to most people the first time they meet it.

The common use is direction. A drive that takes a signed speed reference runs forward on a positive number and reverse on the same number negated, so one speed setting and one NEG covers both directions without a second set of scaling.

It also turns up in correction maths, where an offset measured in one direction has to be applied in the other.

Be careful with the name. In Siemens ladder there is also a falling edge detector abbreviated N, and NEG in this arithmetic sense is a different thing entirely. Here, the falling edge instruction is OSF.

A rung you can run

One speed setting, both directions

RUNG 1Jog_FwdJog_RevMOV Speed_Ref:= Jog_SpeedRUNG 2Jog_RevJog_FwdNEG Speed_RefNEG Jog_SpeedRUNG 3Jog_FwdJog_RevCLR Speed_Ref:= 0
XIC(Jog_Fwd)XIO(Jog_Rev)MOV(Speed_Ref,Jog_Speed)
XIC(Jog_Rev)XIO(Jog_Fwd)NEG(Speed_Ref,Jog_Speed)
XIO(Jog_Fwd)XIO(Jog_Rev)CLR(Speed_Ref)

Set Jog_Speed to 300. Jog forward and the reference is 300; jog reverse and it is -300, from the same setting. Ask for both at once and the interlocks leave it at zero, which is the third rung doing the job an operator will eventually need it to do.

Run this rung in the simulator

What it is called on other controllers

PlatformEquivalent
Rockwell (Studio 5000)NEG
Siemens (TIA Portal)NEG, which creates the twos complement. Not the N coil, which is a falling edge
Mitsubishi (GX Works)NEG
CODESYS and IEC 61131-3NEG, or a leading - in structured text

Common mistakes

  • Expecting an edge. The name looks like the negative edge instruction, and it is not. OSF is the one that fires for one scan when a bit goes from on to off.
  • Using NEG when you meant ABS. Negating a value that is already negative makes it positive, which looks like ABS until the day the input is positive and the answer comes out negative.

Practise it

Interactive exercises that use NEG. Each one checks your ladder against a real scan.

Related instructions

All ladder logic instructions