@Jerry
Firstly, thank you for the effort you've obviously been putting in here on my problem. I did reply over the weekend that "they were all integers", but that seems to have failed to post correctly from my iPad.
I did produce a `minimalist` version of the code and function in question over the weekend, but of course testing that here today, it works without error (rather like yours) !
Instead I've been using the infamous print statement to try and determine exactly what is going on, and I submit these for your consideration.....
Common Stuff
Code: Select all
' Define the parameters required for each axis
Structure TAxis
Value As Integer
Hi As Integer
Lo As Integer
Mid As Integer
Range As Integer
End Structure
Public Dim Y As TAxis
Routine in question
Code: Select all
Public Function NearestCorner(Yq As Integer, Zq As Integer, LastCorner As Byte) As Byte
'The calling parameters are academic as all values fixed as constants in the routine to debug !
Dim YHyst As Integer
Yhyst = 17
Yq = 1606
Y.Mid = 1561
print ("[In: Yq=",dectostr(Yq),", Y.Mid=",dectostr(Y.Mid),", YHyst=",dectostr(YHyst),", (Y.Mid + Y.Hyst)=",dectostr(Y.Mid + YHyst))
print (", Yq - (Y.Mid + YHyst)=",dectostr(Yq - (Y.Mid + YHyst)))
print (", (Y.Mid + YHyst) - Yq=",dectostr((Y.Mid + YHyst) - Yq),"]")
printcrlf
If (Yq > (Y.Mid + YHyst)) Then
print (" Yq+")
Else
If Yq < (Y.Mid - YHyst) Then
print (" Yq-")
Else
print (" Yq=")
EndIf
EndIf
printcrlf
print ("[Out: Yq=",dectostr(Yq),", Y.Mid=",dectostr(Y.Mid),", YHyst=",dectostr(YHyst),", (Y.Mid + Y.Hyst)=",dectostr(Y.Mid + YHyst))
print (", Yq - (Y.Mid + YHyst)=",dectostr(Yq - (Y.Mid + YHyst)))
print (", (Y.Mid + YHyst) - Yq=",dectostr((Y.Mid + YHyst) - Yq),"]")
printcrlf
End Function
Output:
[In: Yq=1606, Y.Mid=1561, YHyst=17, (Y.Mid + Y.Hyst)=1578, Yq - (Y.Mid + YHyst)=0, (Y.Mid + YHyst) - Yq=0]
Yq=
[Out: Yq=1606, Y.Mid=1561, YHyst=17, (Y.Mid + Y.Hyst)=1578, Yq - (Y.Mid + YHyst)=0, (Y.Mid + YHyst) - Yq=0]
Where the final two pieces of maths on both `In` and `Out` lines are obviously wrong (should be +28, then -28 not 0,0).
Changing the 2nd and 3rd lines of each `In` and `Out` print to include Integer() yields a different, but equally WRONG result (it should be +28 then -28 not PLUS 79 and
PLUS 79 again)!
Code: Select all
print (" Yq - (Y.Mid + YHyst)=",dectostr(Integer(Yq - (Y.Mid + YHyst))))
print (" (Y.Mid + YHyst) - Yq=",dectostr(Integer((Y.Mid + YHyst) - Yq)),"]")
Output:
[In: Yq=1606, Y.Mid=1561, YHyst=17, (Y.Mid + Y.Hyst)=1578, Yq - (Y.Mid + YHyst)=79, (Y.Mid + YHyst) - Yq=79]
Yq=
[Out: Yq=1606, Y.Mid=1561, YHyst=17, (Y.Mid + Y.Hyst)=1578, Yq - (Y.Mid + YHyst)=79, (Y.Mid + YHyst) - Yq=79]
Finally, defining a new local integer in the routine...
And changing all references to from Y.Mid to the now new local YMid, give the following correct output (and decision):
[In: Yq=1606 YMid=1561 YHyst=17 (YMid + Y.Hyst)=1578 Yq - (YMid + YHyst)=28 (YMid + YHyst) - Yq=-28]
Yq+
[Out: Yq=1606 YMid=1561 YHyst=17 (YMid + Y.Hyst)=1578 Yq - (YMid + YHyst)=28 (YMid + YHyst) - Yq=-28]
Also defining a temporary Integer local variable, assigning this to (Y.Mid + YHyst) and using the new temporary variably in the IF statement, also yields the correct outcome.
So, my rather ignorant conclusions would be that the maths is going wrong, and this is something to do with the conversion to 32 bit maths to support Integer + Integer. Why changing the maths and IF's to a local Integer YMid, instead of the structure variable Y.Mid somewhat eludes me.
Whilst everyone's first thought is "well what idiot would do it that way", the answer is
this idiot. The compiler is meant to let idiots like me code without too much thought for the variable types, and just `sort it out` for me. In this case I feel somewhat let down on that undertaking.
Can you shed any light on what is happening ? Not that I can't bodge round it of course, it's just I hate doing that because I know the same unresolved problem will bite me in the bum a little further down the road, and be even harder to find !
Many thanks for your time,
Best regards,
Nigel