MOD / MODR / MODLR
The modulo is calculated as follows:
Q = IN – Trunc(IN/BASE) * BASE
where Trunc(x) calculates the truncated (rounded toward zero) value of x.
-
-
If BASE = 0, then Q will return 0.
If IN = 0, then Q will return 0.
Inputs | Function | Description | ||
---|---|---|---|---|
MOD | MODR | MODLR | ||
IN | DINT | REAL | LREAL | Input value |
BASE | DINT | REAL | LREAL | Base of the modulo |
Output | Function | Description | Range | ||
---|---|---|---|---|---|
MOD | MODR | MODLR | |||
Q | DINT | REAL | LREAL | Modulo result | (-BASE, BASE) |
Examples
IN | BASE | Q |
---|---|---|
11 |
7 |
4 |
7 |
7 |
0 |
5 |
7 |
5 |
0 |
7 |
0 |
-5 |
7 |
-5 |
-7 |
7 |
0 |
-11 |
7 |
-4 |
11 |
-7 |
4 |
7 |
-7 |
0 |
5 |
-7 |
5 |
0 |
-7 |
0 |
-5 |
-7 |
-5 |
-7 |
-7 |
0 |
-11 |
-7 |
-4 |
5 |
0 |
0 |
IN | BASE | Q |
---|---|---|
9.00 |
5.75 |
3.25 |
5.75 |
5.75 |
0 |
2.50 |
5.75 |
2.50 |
0 |
5.75 |
0 |
-2.50 |
5.75 |
-2.5 |
-5.75 |
5.75 |
0 |
-9.00 |
5.75 |
-3.25 |
9.00 |
-5.75 |
3.25 |
5.75 |
-5.75 |
0 |
2.50 |
-5.75 |
2.50 |
0 |
-5.75 |
0 |
-2.50 |
-5.75 |
-2.5 |
-5.75 |
-5.75 |
0 |
-9.00 |
-5.75 |
-3.25 |
4.25 |
0 |
0 |
Remarks
The MOD family of functions can return negative numbers. By adding BASE to the result, the modulo value may be forced to a positive number if the range is required to be [0, BASE). An example of how to accomplish this in ST code follows.
q := MOD(x, base);
IF q < 0 THEN
q := q + base;
END_IF;
In FFLD language, the input rung (EN) enables the operation, and the output rung keeps the state of the input rung. In IL "Instruction list" This is a low-level language and resembles assembly language, the first input must be loaded before the function call. The second input is the operand of the function.
ST Language
Q := MOD (IN, BASE);
FBD Language
FFLD Language
(* The comparison is executed only if EN is TRUE *)
(* ENO has the same value as EN *)
IL Language
Op1: LD "Ladder diagram"
Ladder logic is a method of drawing electrical logic schematics. It is now a very popular graphical language for programming Programmable Logic Controllers (PLCs). It was originally invented to describe logic made from relays. The name is based on the observation that programs in this language resemble ladders, with two vertical "rails" and a series of horizontal "rungs" between them IN
MOD BASE
ST Q (* Q is the rest of integer division: IN / BASE *)
See also