P4039

Code

P4039

Message

Constant expression divides or takes the modulo of a value by zero

This error occurs when a constant expression divides or takes the modulo of a value by zero. This applies both to plain constant expressions in statement bodies and to constant expressions used as a VAR initializer (enabled by --allow-constant-initializer-expressions).

The expression is a genuine constant expression – both operands are known at compile time – but the operation itself has no defined result, so it is reported distinctly from P4038 (“does not reduce to a constant value”), which covers expressions that reference something that is not constant at all.

Example

The following code will generate error P4039:

PROGRAM main
VAR
    x : LREAL := 1.0 / 0.0;
END_VAR
END_PROGRAM

To fix this error, ensure the divisor is not zero:

PROGRAM main
VAR
    x : LREAL := 1.0 / 2.0;
END_VAR
END_PROGRAM

This also applies to integer division and modulo, and in statement bodies, not just VAR initializers:

PROGRAM main
VAR
    y : DINT;
END_VAR
y := 5 / 0;
END_PROGRAM

See Also

  • P4038 – initializer expression does not reduce to a constant value

  • P4040 – constant integer expression overflows its evaluation range

  • Data Types – integer and real type ranges

Think IronPLC is wrong about this?

If you believe this diagnostic is incorrect, open an issue on GitHub with a small sample that demonstrates the problem.