# Thread: Using the modulo operator (%) in parametric parts

1. Login to Give a bone

## Using the modulo operator (%) in parametric parts

Hi all,

I'm making a parametric part where the connection height is based on a calculation that may not result in integer values (e.g. 6.75"). I figured out a formula that uses the mod operator (%) (available in the Model Parameters Calculator) and rounds a number to its nearest value: N+((N%1)-(2*((N%1)%0.5))) . "Hooray!" I thought, since this formula works perfectly inside the Model Parameters dialog box (see SS below). However, when I try to look at the Size Parameters, I get a message that says "One or more calculated values failed", and consequently, my part can't be validated and used. Through some troubleshooting, I'm 99.9% sure that it's because of the % operator I'm using in the Model Parameters.

My question is: Is there a certain way I need to format the % operator so that it doesn't cause an error in the Size Parameters? Or, know of any ways to round the number without using %?

Any ideas are appreciated.

Thanks,

2. Login to Give a bone

## Re: Using the modulo operator (%) in parametric parts

Try using Int(6.75)

(integer function)

This will not round up though, so if you need to have that ability you could use an if statement:

If Int(NUM) = Int(NUM+0.5) Then
RESULT = Int(NUM)
Else
RESULT = Int(NUM+0.5)
End If

-Buzz

3. Login to Give a bone

## Re: Using the modulo operator (%) in parametric parts

Looking at my post, and realizing that I haven't had my coffee yet...

No if statement required, could simply use:

Int(NUM+0.5)

-Buzz

4. Login to Give a bone

## Re: Using the modulo operator (%) in parametric parts

Thanks Buzz,

Maybe I'm missing something, but the dimension I'm trying to control is the Connection Height RH1, which is controlled through the Model Parameters; and if I understand you corrently, I'll have to do a Size Parameters calculation to use Int(). As of now, whenever I try to tell RH1 to refer to a Size Parameter (e.g. PDim1), it just tells me that it's an invalid equation. Hopefully you know of a way to get Model Parameters to play nice with Size Parameters?

5. Login to Give a bone

## Re: Using the modulo operator (%) in parametric parts

For formulae, you need to edit them through the model parameters. It looks like Int() does not work, but int() does (lowercase). Try that.

-Buzz

6. Login to Give a bone

## Re: Using the modulo operator (%) in parametric parts

Hooray Buzz! It works, and is much more elegant than that formula I was using.

Thanks a million,

7. Login to Give a bone

## Re: Using the modulo operator (%) in parametric parts

No sweat - strange that it needs to be lowercase...

-Buzz

8. Login to Give a bone

## Re: Using the modulo operator (%) in parametric parts

On a different (perhaps more interesting?) note:

After playing around with a custom Size Parameter, I'm now 100% sure that the % operator in Size Parameter calculations is broken. Even though it gives '%' as an available option in the calculator, the result it gives is completely wrong; for example, doing 7 % 4 should yield 3, however, 7 % 4 in the size parameters yields 7 (see SS).

It appears the correct operator is Mod instead of %, since Size Parameters are apparently evaluated using VB. Doing 7 Mod 4 yields the correct result of 3 (SS). However, it seems that it doesn't like it when I try to Mod by a decimal/non-integer number (i.e. 0.5 or (1/2)); it just gives an "Invalid equation" error. My formula doesn't give an error if I copy+paste it into a Size Parameter calculation and replace all of the %'s with Mod's, and then change the 0.5 to an integer. Microsoft says VB can handle decimal mods, so perhaps AMEP is using VBA instead ( http://msdn.microsoft.com/en-us/libr...sz(VS.80).aspx ).

Finally, it seems that what was giving me trouble in the first place is the fact that it doesn't like having the % operator (whatever it does) in the same equation as a named-variable, since trying 7 % 4 doesn't produce an 'Invalid equation' error, but pH % 4 does.

If I had to place a bet as to what's going on here, I'd guess that the Model Parameters are being evaluated using C or C++ or something similar, but the Size Parameters are evaluated using VB (or more likely, VBA). Unfortunately, it seems that all equations are ultimately evaluated in SP's VB, and therefore any equations entered in MP must also be valid in VB, despite being written for C. This would explain why % works in MP, but does nothing in SP, and vice-versa for Mod. It would also (possibly) explain why Int(x) works in SP, but you have to use int(x) in MP; luckily, it appears that VB doesn't care that the i isn't capitalized.

I may be wrong about all that, but it's my best guess. Anyhow, anyone know of a good way to tell Autodesk they've screwed up? = ]

9. Login to Give a bone

## Re: Using the modulo operator (%) in parametric parts

anyone know of a good way to tell Autodesk they've screwed up?
I usually start with "Dear Autodesk" and then end with "Love, Buzz"

It sounds like you have it working, but in the future if you run into more problems because of this, you may have some luck editing the .xml file directly for those equations.

-Buzz

10. Login to Give a bone