Math
The firmware supports extensive mathematical operations in G-code using expressions enclosed in square brackets []
. These expressions can be used in G-code parameters and with the M118.1 P# command to evaluate and print results. Note that all functions using letters are lowercase and are case sensitive.
Basic Arithmetic Operations
Operators
+
: Addition-
: Subtraction*
: Multiplication/
: Division^
: Exponentiation (power)mod
: Modulo (remainder)
Examples
M118.1 P[1+1] ; Returns result = 2.000
M118.1 P[5-3] ; Returns result = 2.000
M118.1 P[4*3] ; Returns result = 12.000
M118.1 P[10/2] ; Returns result = 5.000
M118.1 P[2^3] ; Returns result = 8.000
M118.1 P[7mod3] ; Returns result = 1.000
G01 X[#103+2*#107]
Mathematical Functions
Trigonometric Functions (angles in degrees)
sin[x]
: Sine of x degreescos[x]
: Cosine of x degreestan[x]
: Tangent of x degreesasin[x]
: Arcsine of x (returns degrees)acos[x]
: Arccosine of x (returns degrees)atan[x]
: Arctangent of x (returns degrees)
Examples
M118.1 P[sin[45]] ; Returns result = 0.707
M118.1 P[cos[0]] ; Returns result = 1
M118.1 P[tan[90]] ; Returns result = nan
M118.1 P[asin[0.5]] ; Returns result = 30
M118.1 P[acos[0]] ; Returns result = 90
Other Mathematical Functions
sqrt[x]
: Square root of xabs[x]
: Absolute value of xround[x]
: Round x to nearest integerfix[x]
: Floor function (truncate to integer)fup[x]
: Ceiling function (round up to integer)ln[x]
: Natural logarithm of xexp[x]
: Exponential function (e^x)
Examples
M118.1 P[sqrt[16]] ; Returns result = 4.000
M118.1 P[abs[-5]] ; Returns result = 5.000
M118.1 P[round[3.7]] ; Returns result = 4.000
M118.1 P[fix[3.7]] ; Returns result = 3.000
M118.1 P[fup[3.2]] ; Returns result = 4.000
M118.1 P[ln[2.718]] ; Returns result = 1.000
M118.1 P[exp[2]] ; Returns result = 7.389
Comparison Operators
Boolean comparisons can be used and will evaluate to 1 (true) or 0 (false):
Boolean Comparisons
eq
: Equal (with tolerance)ne
: Not equalgt
: Greater thange
: Greater than or equallt
: Less thanle
: Less than or equal
Examples
M118.1 P[5eq5] ; Returns result = 1.000 (true)
M118.1 P[3gt2] ; Returns result = 1.000 (true)
M118.1 P[2lt5] ; Returns result = 1.000 (true)
M118.1 P[4le4] ; Returns result = 1.000 (true)
Logical Operators
Logical Operators can be used and will evaluate to 1 (true) or 0 (false):
Boolean Logic
and
: Logical AND (both values must be non-zero)or
: Logical OR (at least one value must be non-zero)xor
: Logical XOR (exactly one value must be non-zero)nor
: Logical NOR (both values must be zero)
Examples
M118.1 P[1and1] ; Returns result = 1.000 (true)
M118.1 P[0or1] ; Returns result = 1.000 (true)
M118.1 P[1xor0] ; Returns result = 1.000 (true)
M118.1 P[0nor0] ; Returns result = 1.000 (true)
Complex Expressions
Complex expressions can be used and will be evaluated per normal PEMDAS order of operations.
Order of Operations (PEMDAS)
Parentheses/Brackets
Exponentiation
Multiplication/Division/Modulo
Addition/Subtraction
Comparison operators
Logical operators
Examples
M118.1 P[1+2*[5-2*2]+2^2+sin[45]] ; Returns result = 7.707
Error Handling
The follow are the expected behaviors when errors are encountered:
Division by zero: Halts with error
Modulo by zero: Halts with error
Undefined functions: Returns nan
Invalid variables: Halts with error
Mismatched brackets: Halts with error
Further Reading
GCodeTutor.com has a good article describing how variables and math can be used in GCode.
Last updated