Parameter Value returns wrong Answer using Floor()

Parameter Value returns wrong Answer using Floor()

richard_griffin
Enthusiast Enthusiast
1,262 Views
16 Replies
Message 1 of 17

Parameter Value returns wrong Answer using Floor()

richard_griffin
Enthusiast
Enthusiast

If you create a Numeric parameter (mm) and type in 1348.5 and run the code below, the message box returns 3.

 

test = Floor((Test - 322 - 90.5 - 136) / 200)

MessageBox.Show(test, "Msg")

 If you remove Test from the code and type in 1348.5

 

test = Floor((1348.5 - 322 - 90.5 - 136) / 200)

MessageBox.Show(test, "Msg")

 

It returns 4 which is correct.

 

Can someone explain why this is the case and if possible, a way to get the parameter version to return the correct answer?

 

 

0 Likes
Accepted solutions (3)
1,263 Views
16 Replies
Replies (16)
Message 2 of 17

clutsa
Collaborator
Collaborator

There are some known issues in converting from Inventor to iLogic...

https://forums.autodesk.com/t5/inventor-customization/attack-of-the-doubleforequals/m-p/5808334

 

My math says 3 would be right though.  (790)/200=3.95 the floor of which is 3.

Edit: I managed to misstype 322 as 332 in several different places... now I see what you're saying. 

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 3 of 17

richard_griffin
Enthusiast
Enthusiast

Edit: I saw your edit when it was too late. 

 

Its just when it takes the value from the parameter that it goes wrong which is really confusing.

 

Using a parameter with 1148.5 returns the correct answer (600 / 200).

 

Its like one rogue parameter that isnt converting correctly.

0 Likes
Message 4 of 17

clutsa
Collaborator
Collaborator

Can you use...

test = Floor((Round(Test,8) - 322 - 90.5 - 136) / 200)

 it seems to help on mine but I'm using inches so I have extra math to convert mine to metric and I may just be correcting that rounding error. 

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

Message 5 of 17

richard_griffin
Enthusiast
Enthusiast

Yeah that does work, I tried that a little while ago.

 

It doesn't change the fact that its using the incorrect number for the parameter though which is concerning.

0 Likes
Message 6 of 17

clutsa
Collaborator
Collaborator

Also this seems to work...

test = Floor(((Parameter.Param("Test").Value*10) - 322 - 90.5 - 136) / 200)

but I know that's not helpful because it's basically the same kind of workaround...

I think @MjDeck got it right in post 11 of the link I posted earlier. 

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

Message 7 of 17

clutsa
Collaborator
Collaborator

Sorry, I meant to @xiaodong_liang in the last post to get his thoughts too.

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

Message 8 of 17

MjDeck
Autodesk
Autodesk

@richard_griffin , I agree with @clutsa's suggesstion to use Round.
The parameter value in the rule is slightly incorrect because of rounding error when converting units. But it is very, very close. You can see that by running this code:

MessageBox.Show("Test - 1348.5 = " & Test - 1348.5)

You can also see the problem in the Parameters dialog. Create a new parameter named Test_ul with units of ul. Enter this Equation for it:

isolate(Test;mm;ul) - 1348.5 ul

The isolate function converts the value to ul.
You'll see that the value shown for that parameter is -0.00000. It's very close to zero, but it's negative.


Mike Deck
Software Developer
Autodesk, Inc.

Message 9 of 17

richard_griffin
Enthusiast
Enthusiast

Thanks for your response, our in house Developer has had a look at it and it and he says its a bit more than a conversion issue, he created a rule to explicitly handle the mm conversion to ensure we have the correct value from the start. 

 

Is this something worth opening a case over? I can send you his code if you'd like to take a look.

0 Likes
Message 10 of 17

bradeneuropeArthur
Mentor
Mentor

I have seen that floor better (only) work with unitless parameters!

This means that you need to convert the parameters first to "ul" than floor or Ceil this "ul" parameter.

Than after Ceil of Floor, convert it back to mm or equal as you prefer.

 

Regards,

Autodesk Software: Inventor Professional 2018 | Vault Professional 2018 | Autocad Mechanical 2018
Programming Skills: Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Dimension Component! | Partlist Export! | Derive I-properties! | Vault Prompts Via API! | Vault Handbook/Manual!
Drawing Toggle Sheets! | Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 11 of 17

richard_griffin
Enthusiast
Enthusiast

It appears the issue isnt with Floor() itself, but rather with the way Inventor is using the Parameter. Its 'close to' the actual value, but not the value which in turn causes floor to return the wrong answer.

0 Likes
Message 12 of 17

clutsa
Collaborator
Collaborator

@richard_griffin I'm never going to say "No, I don't want to see some code." If you post it and I find it worthless I didn't waste much time looking at it but if it's something I've never thought of I can learn something new. That's why I help on the forum is to learn myself and help teach others along the way. I've learned more about how Inventor/iLogic work trying to figure out how to solve someone else's problems then I ever would have just trying to solve my own. 

So long story short, yes, please share the code... even if none of the people in the current conversation need it someone else down the road may learn from it. 

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 13 of 17

MjDeck
Autodesk
Autodesk

@richard_griffin , please share the code. If it's more than a conversion issue then we should open a case for it.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 14 of 17

MjDeck
Autodesk
Autodesk
Accepted solution

Here's a demonstration of roundoff error in converting units. Parameter values are stored internally in units of cm.
Open a web browser, and hit F12 to bring up the Developer Tools.
Click on the Console tab.
Enter these commands:

let x = 1348.5
let x_cm = x / 10
let x_mm = x_cm / 0.1
x_mm - 1348.5

The output should be:
-2.2737367544323206e-13


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 15 of 17

richard_griffin
Enthusiast
Enthusiast

I have opened a ticket, if you have access to it and would like to take a look. 15031339

 

 

 

 

0 Likes
Message 16 of 17

MjDeck
Autodesk
Autodesk
Accepted solution

I looked at the code you provided in the ticket. To see that the problem is caused by roundoff in the units conversion:
After this line:

Dim mmValue As Double = oUnitsOfMeasure.ConvertUnits(inventorValue.ModelValue, Inventor.UnitsTypeEnum.kCentimeterLengthUnits, Inventor.UnitsTypeEnum.kMillimeterLengthUnits) 

Add this line:

Trace.WriteLine("mmValue - 1348.5 = {0}", mmValue - 1348.5)

 


Mike Deck
Software Developer
Autodesk, Inc.

Message 17 of 17

MjDeck
Autodesk
Autodesk
Accepted solution

@richard_griffin , we have improved the accuracy of the units conversion in the Inventor 2020.2 Update. (Internal issue number INVGEN-12769.)
In 2020.2, no workaround is needed. Your original code will give the expected answer.


Mike Deck
Software Developer
Autodesk, Inc.