[iLogic] Why Don't User Parameters Work With Arithmetic?

[iLogic] Why Don't User Parameters Work With Arithmetic?

SometimesInventorMakesMeAngry
Advocate Advocate
1,364 Views
7 Replies
Message 1 of 8

[iLogic] Why Don't User Parameters Work With Arithmetic?

SometimesInventorMakesMeAngry
Advocate
Advocate

 

length is a UserParameter with "in" units and expression "55 in + 5/16"".

 

 

length Mod 55.3125

 

 

 returns 55.3125 instead of 0. Why?

I did some testing and doing

 

 

Dim number As Double = 55.3125
number Mod 55.3125

 

 

returns 0, as exptected.

 

Trying

 

 

(length).Value Mod 55.3125

 

 

and

 

 

Dim middleMan As Double = length
middleMan Mod 55.3125

 

 

 Do not work either.

 

But

 

 

Math.Round(length, 9) Mod 55.3125

 

 

works ok, so I think it has something to do with the number of decimal places of doubles. But, according to Microsoft VB.NET documentation, Double can handle many more than only 9 decimal places. So what's the issue? This is code-breaking but I haven't seen it documented anywhere. Also, is rounding Parameter values to 9 decimal places guaranteed to work every time or is there a better method? Maybe accessing the Parameter.Value property through the API and converting units is best.

0 Likes
1,365 Views
7 Replies
Replies (7)
Message 2 of 8

WCrihfield
Mentor
Mentor

Hi @SometimesInventorMakesMeAngry.  I believe you are correct about it having to do with tiny differences out at many decimal places.  I think the breaking point is 15 decimal places.  Below is a simple iLogic rule I used some time back for checking this Double comparison level, to find the breaking point.  When I know a parameter's value will be scrutinized closely, I generally include several zero's after its decimal place, then a 1 at the end.  Then after the parameter is created or established, I will change the 1 to a zero.  Or simply put several zeros after the decimal place, without the little 1 at the end, but that sometimes eliminates the zeros automatically.

Dim oDbl1 As Double = 1.000000000000001
Dim oDbl2 As Double = 1.000000000000000
'one more zero then they would test as equal

If oDbl1.Equals(oDbl2) Then
	MsgBox("They are Equal.", , "")
Else
	MsgBox("They are Not Equal.", , "")
End If

If oDbl1.CompareTo(oDbl2) = 0 Then
	MsgBox("They 'Compare' equally.", , "")
ElseIf oDbl1.CompareTo(oDbl2) < 0 Then
	MsgBox("Comparison = oDbl1 < oDbl2", , "")
ElseIf oDbl1.CompareTo(oDbl2) > 0 Then
	MsgBox("Comparison = oDbl1 > oDbl2", , "")
End If

If Round(oDbl1, 4) = Round(oDbl2, 4) Then
	MsgBox("They are Equal (within Tolerance).", , "")
Else
	MsgBox("They are Not Equal (out of Tolerance).", , "")
End If

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 8

Frederick_Law
Mentor
Mentor

Maybe because Length has unit of inch.

55.3125 is unitless.

Also Length is 55 in + 5/16 in.

Need to do some debugging.

0 Likes
Message 4 of 8

Frederick_Law
Mentor
Mentor

It works this way:

 

iLogic-01.jpg

0 Likes
Message 5 of 8

Unfortunately, I've been burned by a similar problem in the past. In that case, I was using the

 

 

 

Parameter(parameterName as String)

 

 

 

syntax that you're using. I determined that this syntax sometimes gave the correct result when comparing values, but other times, it didn't. I didn't realize this until after the code was in production because it worked well with a variety of cases when testing.

 

It seems like the Parameter syntax also doesn't always work as expected. I just tried the following with the following results:

 

 

 

length Mod 55.3125

 

 

 

  1. length's expression is "55 in + 5 in / 16 ul" -> Fail
  2. length's expression is "55 in + 5/16"" (double quote for inches) -> Fail
  3. length's expression is "55.3125 in" -> Pass

I tried this on a separate IDE in VB.NET in case there is something weird going on with fractions. But it works as expected, so I believe this is iLogic/Parameter related.

SometimesInventorMakesMeAngry_0-1684511700602.png

 

What's more frustrating is if I print the values as follows

MsgBox((length).ToString() + " Mod 55.3125 = " + (length Mod 55.3125).ToString())

I get the following result

---------------------------
utof0e5s
---------------------------
55.3125 Mod 55.3125 = 55.3125
---------------------------
OK   
---------------------------

So the values appear to be the same, but they aren't somehow and it gives the wrong answer.

 

Maybe it's best to stick to API calls for this. Though some explanation or at least acknowledgement of the problem from Autodesk would be nice.

0 Likes
Message 6 of 8

Frederick_Law
Mentor
Mentor

Inventor store values in cm.

So all inch get converted hence the precision error.

Everything with equations are calculated when use, so they are more accurate.

And add error form binary to decimal conversion.

 

I cannot enter " as inch in parameter.

 

Interesting:

iLogic-02.jpg

0 Likes
Message 7 of 8

Frederick_Law
Mentor
Mentor

Add enough zero:

iLogic-03.jpg

0 Likes
Message 8 of 8

SometimesInventorMakesMeAngry
Advocate
Advocate

@johnsonshiue Do you have any input on this? Is this a known issue?

0 Likes