ilogic/ChatGPT calculation error?

ilogic/ChatGPT calculation error?

jake_pietrzak
Contributor Contributor
1,461 Views
13 Replies
Message 1 of 14

ilogic/ChatGPT calculation error?

jake_pietrzak
Contributor
Contributor

Hi All,

 

I encountered this error, and have absolutely no idea how to deal with it.

If any of You might know the answer, please help  because I literally go crazy 😤

jake_pietrzak_0-1708689144170.png

Many thanks for help!

 

Code from Screenshot:

 

		S_Length = 7392.8
		width_SA_L1_V_first = 1200
		SA_L1_LeftOffset = 96.4
		SA_L1_RightOffset= 96.4
		SA_L1_fbw = 1200
		Dim a As Double = (S_Length - width_SA_L1_V_first - SA_L1_LeftOffset - SA_L1_RightOffset) / SA_L1_fbw
		
		MessageBox.Show(a, "a")
		MessageBox.Show(Int(a), "Int(a)")


		If Int(a) = a Then
			MessageBox.Show(a & " = " & Int(a) & " >> True", "Integer Check")
		Else
			MessageBox.Show(a & " = " & Int(a) & " >> False", "Integer Check")
		End If

 

Accepted solutions (1)
1,462 Views
13 Replies
Replies (13)
Message 2 of 14

Michael.Navara
Advisor
Advisor

I'm sorry, but read this article before you post your question.

0 Likes
Message 3 of 14

jake_pietrzak
Contributor
Contributor
My apologies, message updated.
Screenshot was just to show chatGPT error.
In Inventor though, its still telling me that 5 = 5 is false.
Any chance that you know what is wrong with it?
0 Likes
Message 4 of 14

FINET_Laurent
Advisor
Advisor

Hello @jake_pietrzak,

 

Relying on chatGPT without any coding background will cause more harm that good.

The code is working correctly, as the result of the calculus is not 5, but rather 5.0803333.

If you replace with 5 instad of the calculus, the code return true.

 

What is it that is not working? Let's maybe start with what you want to achive, like copy pasting your ChatGPT request, so we can sort it out.

 

Kind regards,

FINET L.

 

 

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 5 of 14

Michael.Navara
Advisor
Advisor

When I run your code I got this result. In my opinion it works as designed.

 

2024-02-23_13-20-52.png

0 Likes
Message 6 of 14

jake_pietrzak
Contributor
Contributor

ChatGPT was just to test if code is correct, but anyway, what I try to achieve is to get correct result from the code.

If you will run it in inventor it gives wrong result ( If Int(a) = a is true or not, assuming that the result of simple equation from code = 5).

0 Likes
Message 7 of 14

jake_pietrzak
Contributor
Contributor

Thats the thing, result of the equation is not a = 5.083, its 5. 

Also, I have this:

jake_pietrzak_0-1708691606292.png

Edit: There was an error in original code, SA_L1_RightOffset was not defined. But it still works not right and it gives me 5 = 5 as false.

 

0 Likes
Message 8 of 14

FINET_Laurent
Advisor
Advisor
Accepted solution

@jake_pietrzak,

 

Well if it has nothing to do with ChatGPT then would you mind pasting here the code that is going wrong instead.

The only thing I can think off that would cause such issues is : Double-precision floating-point format - Wikipedia

 

Kind regards,

FINET L.

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

Message 9 of 14

jake_pietrzak
Contributor
Contributor
Code I'm talking about is in original post.
But you were right, it was all about how 'a' was defined. I changed Double to Single and it works as intended.
Originally it wasn't defined at all just 'a = ...', and it was giving me wrong result as well.
Later changed it to double as ceiling (Ceil) function I use in other part of the code uses parameters defined as 'Double'.
Anyway, it works now.
Thanks a lot.
Message 10 of 14

Michael.Navara
Advisor
Advisor

Just a note. I discover the same as @FINET_Laurent . You can test this code sample and you can see that the calculated result is not exactly 6000 as expected, but little bit different number (6000,0000000000009 in my case).

 

 

Dim A As Double = 7392.8
Dim B As Double = 1200
Dim C As Double = 96.4
Dim D As Double = 96.4
Dim E As Double = 1200
Dim result as Double = A
result -= B
result -= C
result -= D

Logger.Debug(result.ToString("R"))

 

 

Also when I put the code to VisualStudio, it recommends me the fix for floating point number comparison.

2024-02-23_14-18-44.png

I expect we are not the first who has this problem 😀.

Message 11 of 14

Frederick_Law
Mentor
Mentor

The nature of binary floating point number.

It's an infinite estimation.

https://en.wikipedia.org/wiki/Floating-point_arithmetic

0 Likes
Message 12 of 14

lionelmoen4
Observer
Observer

Can you explain the primary differences between iLogic, Macros, AddIns, and Apprentice within the framework of Inventor programming?

0 Likes
Message 14 of 14

WCrihfield
Mentor
Mentor

Just adding a few links in here to some related 'iLogic' tools that may help when comparing two Double data type values, if done within an iLogic rule (may not be available in VBA).

LmiMath.EqualWithinTolerance Method (Double, Double) 

LmiMath.EqualWithinTolerance Method (Double, Double, Double) 

When used in an iLogic rule, the 'EqualWithinTolerance' method is directly available to use, because its source (Autodesk.iLogic.Runtime.LmiMath) is already included for us in every rule.  I use that second one a lot in my rules, because it provides an easy way to specify two input values I want to compare to each other (for equality), and a 'tolerance' value by which the two are allowed to not be equal.

 

There are also multiple other tools for similar purposes defined within the iLogic add-in.  Such as the DoubleUtil Class, and its Methods, and the DoubleForEquals Structure, and its members.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)