After long debug sessions I've found several problems with the % operator and its conterpart Mod(), I also had trouble using Int(), << >> operators and Shl() and Shr() with big numbers.
For example:
Code: Select all
Mod(12345679123, 234567) returns 183346
12345679123 % 234567 returns -22763
There are several cases where big numbers behaves strange, I feel that they are represented in several ways internally and converted
on the fly by Hollywood depending on the cases, I remember to have found something like the following during my tests (numbers are invented, because I don't remember the original ones):
Code: Select all
Local a = b / c
DebugPrint(a) ; -> 123343.98
Local aa
aa = a
DebugPrint(aa) ; -> 123344
When I saw that I jumped on my chair!
Here is a little script with some tests so you have an example of what I remember to have encountered in these two days of panic.
Code: Select all
; Very basic and slow mod routine to test the builtin one
Function mymod(a, b)
Local aa = a
While aa > b
aa = aa - b
Wend
Return(aa)
EndFunction
Local values = { 1, 123, 123456, 123456789, 12345679123, 123456789134 }
Local dividers = { 2648, 25534, 234567, 234567890, 23456790123, 234567901245 }
Local t = ListItems(values)-1
For Local i = 0 To t
Local v1 = values[i]
For Local ii = 0 To t
Local v2 = dividers[ii]
DebugPrint("VALUES: " .. v1 .. ", " .. v2)
Local r1 = Mod(v1, v2)
Local r2 = v1 % v2
Local r3 = mymod(v1, v2)
DebugPrint("| MOD() = " .. r1)
DebugPrint("| % = " .. r2)
DebugPrint("| MYMOD = " .. r3)
If r1 <> r2 Or r1 <> r3 Or r2 <> r3 Then DebugPrint("| *** ERROR ***\n")
Next
Next
DebugPrompt("PROCEED WITH INT() - Hit Enter")
For Local i = 0 To t
Local v = values[i]
Local r = Int(v)
DebugPrint("| VALUE: ", v)
DebugPrint("| INT(): ", r)
DebugPrint("| CAST : ", Cast(Int(v), False, #INTEGER))
If v <> r Then DebugPrint("| *** ERROR ***\n")
Next
DebugPrompt("PROCEED WITH SHIFT >> And DIVISION - Hit Enter")
Local a = 1385294725120
Local c = 255548
DebugPrint("Testing only these two values: ", a, ", ", b)
For Local bit = 1 To 24 Step 2
Local b = 2^bit
DebugPrint(bit .. " BITs = " .. 2^bit)
DebugPrint(" Division : " .. a .. " / " .. b .. " = " .. a/b)
DebugPrint(" Shift Right: " .. a .. " >> " .. bit .. " = " .. a>>bit)
DebugPrint("---")
DebugPrint(" Division : " .. c .. " / " .. b .. " = " .. c/b)
DebugPrint(" Shift Right: " .. c .. " >> " .. bit .. " = " .. c>>bit)
DebugPrint("--- --- ---")
Next
DebugPrompt("Hit Enter to Close")