Macro Arithmetic

Group Expression Purpose Example Result
Define#i=#jDefine or substitute one variable for another#100=#103#100= 2
Addition#i=#j+#kSum#100=#101+#102#100= 1
#i=#j-#kSubtract#100=#101-#102#100= -1
#i=#jOR#kLogical Sum#100=#101OR#102#100= 1
#i=#jXOR#kExclusive OR
Multiplication#i=#j*#kProduct#100=#103*#105#100= 10
#i=#j/#kQuotient#100=#105/#103#100= 2.5
#i=#jAND#kLogical Product#100=#102AND#103#100= 1
Functions#i=SIN[#j]Sine#100=SIN[#105]
#i=ASIN[#j]Arcsine#100=ASIN[#105]
#i=COS[#j]Cosine#100=COS[#105]
#i=ACOS[#j]Arccosine#100=ACOS[#105]
#i=TAN[#j]Tangent#100=TAN[#105]
#i=ATAN[#j]Arctangent#100=ATAN[#105]
#i=SQRT[#j]Square Root#100=SQRT[#106]#100= 3
#i=ABS[#j]Absolute Value#100=ABS[#106]#100= 9
#i=BIN[#j]Convert BCD to Bin#100=BIN[#105]
#i=BCD[#j]Convert Bin to BCD#100=BCD[#105]
#i=ROUND[#j]Round off value#100=ROUND[#107]#100= 1
#i=FIX[#j]Discard fractions less than 1#100=FIX[#107]
#i=FUP[#j]Add 1 to fractions less than 1#100=FUP[#107]
#i=LN[#j]Logarithm#100=LN[#105]
#i=EXP[#j]Index#100=EXP[#105]
#i=ADP[#j]Add decimal point#100=ADP[#102]#100= 1.

Defined Variables for Examples

VariableValue
#1010
#1021
#1032
#1040
#1055
#106-9
#1071.2

Evaluation Order

Fanuc Macro B evaluates expressions using standard mathematical precedence, from highest to lowest priority:

PriorityOperationExample
1 (highest)Functions (SIN, COS, SQRT, ABS, ROUND, etc.)SIN[45]
2Multiplication / Division (* /)#1*#2
3Addition / Subtraction (+ -)#1+#2
4 (lowest)Logical (AND, OR, XOR)#1 AND #2

Use brackets [ ] to force evaluation order. Fanuc uses square brackets for all mathematical grouping — parentheses ( ) are reserved for comments only.

#100 = #1 + #2 * #3       (MULTIPLICATION FIRST: #2*#3, THEN ADD #1)
#100 = [#1 + #2] * #3     (BRACKETS FORCE ADDITION FIRST)
#100 = SIN[#1 + 45]       (ADDITION INSIDE FUNCTION BRACKETS)

Vacant Variable Behavior

When a null (vacant) variable is used in arithmetic, the result depends on the operation:

ExpressionIf #1 is nullIf #1 = 0
#100 = #1 + 5#100 = null (null propagates)#100 = 5
#100 = #1 * 3#100 = null#100 = 0
#100 = #1#100 = null#100 = 0
X#1X address is omitted entirelyMoves to X0.0

This is a feature, not a bug. It allows optional arguments: if the caller doesn't provide an argument, the variable stays null and can be detected with IF [#1 EQ #0]. See Variable Types for the full null vs zero reference.

Trigonometry Notes

All Fanuc trig functions use degrees, not radians. This is different from most programming languages.

FunctionInputOutputNotes
SIN[angle]DegreesRatio (-1 to 1)SIN[90] = 1.0
COS[angle]DegreesRatio (-1 to 1)COS[0] = 1.0
TAN[angle]DegreesRatioUndefined at 90, 270
ASIN[ratio]-1 to 1DegreesReturns -90 to 90
ACOS[ratio]-1 to 1DegreesReturns 0 to 180
ATAN[y]/[x]Two valuesDegreesTwo-argument form returns full 360° range

ATAN two-argument form: ATAN[y]/[x] returns the angle in degrees with correct quadrant. This is equivalent to ATAN2(y,x) in other languages. Example: #100=ATAN[1.0]/[1.0] returns 45 (degrees).

ROUND, FIX, and FUP

These rounding functions behave differently depending on whether they're used in arithmetic or as axis addresses:

FunctionIn ArithmeticAs Axis AddressExample (#100=1.7)
ROUNDRound to nearest integerRound to least input incrementROUND[#100] = 2
FIXTruncate toward zeroSameFIX[#100] = 1
FUPRound away from zero (ceiling)SameFUP[#100] = 2

For negative values: FIX[-1.7] = -1 (toward zero), FUP[-1.7] = -2 (away from zero).

Address rounding: When a variable is used directly as an axis word (e.g., X#100), the control automatically rounds to the machine's least input increment (typically 0.001mm or 0.0001"). The ROUND function is only needed when you want to round within an arithmetic expression.

Common use for FUP: Calculating number of passes. #100=FUP[ABS[#3]/#7] ensures you always round up to get at least enough passes to cover the full depth.

Practical Examples

Bolt Hole Circle Calculation

(BOLT HOLE CIRCLE: R=RADIUS, N=HOLES, A=START ANGLE)
#100=0 (HOLE COUNTER)
WHILE [#100 LT #3] DO1
#101=#1 * COS[#7 + #100 * [360/#3]]   (X POSITION)
#102=#1 * SIN[#7 + #100 * [360/#3]]   (Y POSITION)
G81 X#101 Y#102 Z#2 R0.1 F#9
#100=#100+1
END1
G80

Speed and Feed Formulas

(CALCULATE RPM FROM SURFACE SPEED AND DIAMETER)
(#1=SFM, #2=DIAMETER IN INCHES)
#100=[#1*12]/[#2*3.14159]  (RPM = SFM*12 / PI*D)
#100=ROUND[#100]            (ROUND TO INTEGER)

(CALCULATE FEED RATE)
(#3=CHIP LOAD PER TOOTH, #4=NUMBER OF FLUTES)
#101=#3*#4*#100  (FEED = CHIPLOAD * FLUTES * RPM)

S#100 F#101

Metric / Inch Conversion

(CONVERT INCH TO MM)
#100 = #1 * 25.4

(CONVERT MM TO INCH)
#100 = #1 / 25.4

See also: Macro Control Flow for WHILE/DO loops used with calculations, Variable Types for null variable arithmetic behavior, and the Macro Playground to test expressions interactively.

References

  • Peter Smid, Fanuc CNC Custom Macros, Industrial Press, 2004.
  • Fanuc, Operator’s Manual / Parameter Manual, FANUC Corporation.

Have a question or want to contribute?

Contact us with corrections, additions, or topics you'd like covered.

Get in Touch