iLogic says equal number are not equal??

iLogic says equal number are not equal??

DRoam
Mentor Mentor
2,927 Views
24 Replies
Message 1 of 25

iLogic says equal number are not equal??

DRoam
Mentor
Mentor

I often have iLogic rules check if a parameter equals a value, in order to take some appropriate action. This is the kind of if/then test that needs to be very reliable, and you would think it would be -- just checking if one number equals another. However, sometimes I'll run an equality test and the iLogic will say that the two numbers are not equal, when in fact they are. I've run into this strange glitch multiple times before, and thought I'd post to try and find out what the heck is going on.

 

I put together a quick script to demonstrate the issue. To reproduce, first create a new Part and create a parameter in the Part called "d0". Then paste the following rule into the Part and run it:

 

 

oSetVal = 1.23456789

Parameter("d0") = oSetVal

IsEqual = If(Parameter("d0") = oSetVal,True,False)
		
MessageBox.Show( _
	"Parameter: " & Parameter("d0") & vbCrLf & _
	"Set value: " & oSetVal & vbCrLf & _
	"Equal: " & IsEqual )

 

The rule literally sets the parameter equal to the value, and a message box confirms that they are indeed equal, and yet an equality test says the values are not equal.

 

You can experiment with different numbers and see that most of the time, the code returns the correct "True" result. However, for some oddball values, it says they're not equal, even though they are. (Most numbers following the pattern 3.456789, 4.56789, etc. on up to 7.89 also exhibit the glitch).

 

What's the reason for this? Is there some way to fix this so I can be confident my parameter equality tests in iLogic rules will actually return the correct result?

 

 

 

Running Inventor 2017.4.6 on a Lenovo Windows 10 64-bit desktop

0 Likes
2,928 Views
24 Replies
Replies (24)
Message 21 of 25

clutsa
Collaborator
Collaborator

So my post 20 wasn't in direct response to your post 19 but I think the concept may work. For your case 2 can you...

Dim TestNum as DoubleForEquals = 440
If GenericFun = TestNum Then
     Parameter("x") = 1430
End If
If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 22 of 25

DRoam
Mentor
Mentor

The real-life usage will be to use GenericFun directly in an equality test with one or more numbers, like this:

 

'Before running, create a Numeric parameter called "d0" and set to 440 inches.

Sub Main()
	If GenericFun = 7.89 Then
		MessageBox.Show("d0 is 7.89")
		'Do something important
	Else If GenericFun = 440 Then
		MessageBox.Show("d0 is 440")
		'Do something else
	Else
		MessageBox.Show("d0 is NOT 7.89 or 440")
		'Do something else
	End If
End Sub

Function GenericFun As Object
	'If is text or Boolean param, return text or Boolean. Otherwise...
	Dim oReturnVal As DoubleForEquals = CDbl(Parameter.Param("d0").Value / 2.54)
	Return oReturnVal
End Function

If d0 is set to 7.89 or 440, the equality tests will return a false negative.

 

The goal is for this function to work "as expected".

 

If you change "Function GenericFun As Object" to "Function GenericFun As DoubleForEquals", the equality tests return True correctly, as expected.

 

But then I can't use GenericFun to check text parameters as well. I want to keep GenericFun able to return text or boolean objects, but function as used in the code above.

 

Basically, without touching anything inside of Sub Main(), and without changing the "Function GenericFun As Object", is there some way to change GenericFun so that it will return a DoubleForEquals value that results in the equality tests working correctly.

0 Likes
Message 23 of 25

clutsa
Collaborator
Collaborator

This is the best I can come up with...

Sub Main()
	Select Case GenericFun("d0")
	Case = CDbl4E(7.89)
		MessageBox.Show("d0 is 7.89")
	Case = CDbl4E(440)
		MessageBox.Show("d0 is 440")
	Case Else
		MessageBox.Show("d0 is NOT 7.89 or 440")
	End Select
'	If GenericFun("d0") = CDbl4E(7.89) Then
'		MessageBox.Show("d0 is 7.89")
'		'Do something important
'	Else If GenericFun("d0") = CDbl4E(440) Then
'		MessageBox.Show("d0 is 440")
'		'Do something else
'	Else
'		MessageBox.Show("d0 is NOT 7.89 or 440")
'		'Do something else
'	End If
End Sub

Function GenericFun(param As String) As Object
	'If is text or Boolean param, return text or Boolean. Otherwise...
	'Dim oReturnVal As DoubleForEquals = CDbl(Parameter.Param(param).Value/2.54)
	Dim oReturnVal As DoubleForEquals = CDbl(Parameter.ValueForEquals(param))
	Return oReturnVal
End Function
Function CDbl4E(MyDouble As Double) As DoubleForEquals 'cast DoubleForEquals
	CDbl4E = MyDouble
End Function
If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

Message 24 of 25

clutsa
Collaborator
Collaborator

My next closest attempt...

Sub Main()
	If GenericFun("d0").Equals(7.89) Then
		MessageBox.Show("d0 is 7.89")
		'Do something important
	Else If GenericFun("d0").Equals(440) Then
		MessageBox.Show("d0 is 440")
		'Do something else
	Else
		MessageBox.Show("d0 is NOT 7.89 or 440")
		'Do something else
	End If
End Sub

Function GenericFun(param As String) As Object
	'If is text or Boolean param, return text or Boolean. Otherwise...
	Dim oReturnVal As DoubleForEquals = CDbl(Parameter.Param(param).Value/2.54)
	'Dim oReturnVal As Double = CDbl(Parameter.ValueForEquals(param))
	Return oReturnVal
End Function
If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 25 of 25

HermJan.Otterman
Advisor
Advisor

Hello DRoam, I had a similar discussion some time ago...

https://forums.autodesk.com/t5/inventor-customization/disturbing-bug-while-using-the-floor-function-...

 

I tried to do a simple calculation, and the answer was one less because I used the "floor" option, so I had to get 10 but got 9, also because of this....

 

also, you get different result if you do calculations straight in the parameters dialog or in iLogic...

so now I round my values, before I do anything else...

 

 

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


0 Likes