Expr can be used like a calculator. This is useful for quick checks, MDI input, setup calculations, probing values, tool dimensions, offsets, and script logic.
The most important rule is that a calculation produces a value. In many places the last expression is the value you see as the result.
In PlanetCNC MDI input, if the first character is `=`, the input is evaluated as an Expr expression instead of G-code.
For example:
=2+3*5;
This evaluates the expression and outputs:
17
This is useful for quick calculations without creating a script file.
The `=` marker can also be used with multi-line Expr input.
=a=2; b=3; a+b;
This evaluates to:
5
The first line starts Expr mode. The following lines are normal Expr statements.
Use the normal arithmetic operators:
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | add | 2 + 3 | 5 |
| - | subtract | 10 - 4 | 6 |
| * | multiply | 6 * 7 | 42 |
| / | divide | 20 / 5 | 4 |
| % | modulo, remainder | 10 % 3 | 1 |
| ** | power | 2 ** 3 | 8 |
Examples:
2 + 3; 10 - 4; 6 * 7; 20 / 5; 10 % 3; 2 ** 3;
Expr follows normal calculation order. Multiplication and division are done before addition and subtraction.
2 + 3 * 5; // 17 (2 + 3) * 5; // 25
Use parentheses when the intended order should be obvious.
safe_z = clearance + (tool_length * 0.5);
Parentheses are not only for the computer. They also help you read the script later.
For anything more than a very short calculation, store intermediate values in variables.
tool_diameter = 6; tool_radius = tool_diameter / 2; stepover = tool_radius * 0.4; stepover;
The final result is the value of `stepover`.
This style is usually easier to check than one long expression.
Negative values are common in probing and Z movement.
probe_depth = -2; safe_z = 5; travel = safe_z - probe_depth; travel;
This evaluates to `7` because subtracting a negative value adds it.
When a negative value is part of a larger calculation, parentheses can make it clearer:
travel = safe_z - (-2);
Division uses `/`.
20 / 4; // 5 7 / 2; // 3.5
Modulo uses `%` and returns the remainder.
10 % 3; // 1 12 % 3; // 0
Division by zero and modulo by zero are errors.
10 / 0; // error 10 % 0; // error
The + operator can also join text when a string is involved.
tool = 3; diameter = 6; 'Tool ' + tool + ', diameter ' + diameter;
This produces:
Tool 3, diameter 6
Use parentheses when you want a calculation before text is joined.
'Radius: ' + (diameter / 2);
Most statements end with `;`.
a = 2; b = 3; a + b;
If an error points at the next line, check whether the previous line is missing a semicolon.
This result is `17`, not `25`:
2 + 3 * 5;
Use parentheses when you want addition first:
(2 + 3) * 5;
Text is not automatically converted for arithmetic.
'10' * 2; // error '10'.parse_num() * 2; // 20
In MDI, try:
=40 / 2;
Then try:
=width=40; height=25; area=width*height; area;
The final result is `1000`.