This year begins with the detection of a bug in an addition.
No, it is not a joke.
I was calculating the Decimal value of a string, and the result was wrong.
Here is the code:
Code: Select all
Procedure.i zDec(S.s)
Protected.i Dec,i
For i=1 To Len(S)
Dec+Abs(Asc(Mid(S,i,1)))*Pow(256,Abs(i-Len(S)))
Next
ProcedureReturn Dec
EndProcedure
Procedure.q zDecDebug(S.s)
Protected.q X,DecOkay,DecProb,i
For i=1 To Len(S)
Debug "<Loop "+i+">"
Debug " "+Str(Abs(Asc(Mid(S,i,1))))
Debug " "+Str(Pow(256,Abs(i-Len(S))))
X=Abs(Asc(Mid(S,i,1)))*Pow(256,Abs(i-Len(S)))
Debug " ="+X
Debug " ="+Str(Abs(Asc(Mid(S,i,1)))*Pow(256,Abs(i-Len(S))))
DecOkay+X
DecProb+Abs(Asc(Mid(S,i,1)))*Pow(256,Abs(i-Len(S)))
Debug "DecOkay="+DecOkay
Debug "DecProb="+DecProb
Next
ProcedureReturn DecOkay
EndProcedure
Debug zDecDebug("ONEDRIV")
Debug zDec("ONEDRIV")The zDecDebug() is the same procedure with debugging.
As you can see, when I calculate to X before adding it to DecOkay, it looks fine.
But the variable X is not necessary because I can add it directly to the variable Dec, but the result is different at the 7th iteration.
I am working on Windows 11 64-bit, so using .i (integers) or .q (quad) makes no difference.
The final result 22322582566095190 has 17 digits, and is far below the integer range 9223372036854775808 to +9223372036854775807 (19 digits), so it is normally safe.
So, why do I get a difference of 2

