# Using Custom Macros to Create Arithmetic Functions

These user-defined functions can solve both simple and more complicated arithmetic problems.

An arithmetic function is simply a formula or solution method that solves a common arithmetic problem. Examples include square root, absolute value, sine, cosine and tangent. Functions are often built-in features of electronic calculators and programming languages. You provide some simple input, and the function returns the answer. Type a value and press the square root button on your calculator, for example, and the square root of the value you entered will be displayed.

Custom macro B provides a variety of very helpful and important built-in arithmetic functions. But there are some rather obvious omissions. For instance, there is no square function. Not having a function for a given problem presents no real problem, of course, as long as you know the related formula to solve the problem. To square a value, you simply multiply the value by itself.

There is a way to create your own functions using custom macro B. While the square function may be somewhat simplistic, it will work nicely to show how user-defined functions are created. We’ll use a G65 command to pass certain “entry data” to a user-defined-function custom macro. For the square function example, this will include the value to be squared. We also need a place to store the answer, so we will also include a common variable number into which the answer will be placed. After the custom macro is executed, the square of the value will be available in this common variable. Here is the G65 command, which will be in your calling program:

**G65 P9500 S5.0 V101.0**

The squaring custom macro will be program O9500. S represents the value to be squared and V represents the common variable number in which the answer will be placed. Here is the squaring custom macro:

**O9500
#[#22] = #19 * #19
M99**

#19 and #22 are the local variable representations for S and V, respectively. #19 (the value of S) will be multiplied times itself, and the result will be placed in the common variable number specified by #22 (the value of V). In our case, the answer will be placed in common variable #101.

This is a pretty simple example, so let’s look for something a bit more complicated. While many control manufacturers claim to be FANUC-compatible, there are often some minor differences when it comes to custom macro arithmetic functions. One popular “FANUC-compatible” control, for example, does not provide the fix-up (FUP) function, which rounds a real-number value to the next higher integer. As long as you can determine how to round a value to the next higher integer, you can create the FUP function.

Consider these commands in the custom macro body:

**#103 = 1.0 (Overall depth)
#17 = 0.15 (Depth of each pass)
#104 = #103 / #17 (Value to be rounded up)
G65 P9501 F#104 V105.0 (Call program to round up #104)
#106 = #103 / #105 (Recalculate depth per pass)**

The value to be rounded up (#104) is the number of passes for a pocket milling operation. It is calculated by dividing the overall pocket depth by the depth per pass. With the current settings, the value of #104 will be 6.666666… when the custom macro is executed. In this scenario, we would want a total of seven evenly spaced passes. When this is divided into the total depth (1.0 in our case), the recalculated depth per pass will be 0.1248, meaning the pocket will be milled with seven passes of 0.1248 each. Here is the custom macro:

**O9501 (Round up program)
#1 = FIX [#9] (Round #9 down)
IF [[#9 - #1] EQ 0] GO TO 98 (Test to see that #9 wasn’t already a whole number integer)
#[#22] = #1 + 1 (Add one to the rounded down value)
GO TO 99
N98 #[#22] = #9 (#9 was already a whole number value)
N99 M99**

Since the control being used does have the FIX function that rounds down a real number value, we’re using it as part of our solution. We’ll round down the input value (#9) and then simply add one to it. Note that we must also check to be sure that the value of #9 wasn’t already a whole number before the custom macro was executed.

You may be able to come up with other ways to round up a value. Indeed, there may be several ways to solve this problem. Additionally, the control in question may not allow the FIX function, meaning an entirely different solution must be found. But if you can find the solution for any function you wish to develop, you can develop the related user-defined arithmetic function.