Hi,
I used this to test them and added your variation (using an else):
SuperStrict
'Modulo Test
Import "eucmod.c"
Extern
Function eucmod:Int( dividend:Int, divisor:Int )
EndExtern
Const LISTLEN:Int = 5
Const REPETITION:Int = 1000000
Global start:Int, finish:Int
speedtest( "x mod n ", xmodn, LISTLEN, REPETITION )
speedtest( "emodulo ", emodulo, LISTLEN, REPETITION )
speedtest( "emodulo2", emodulo2, LISTLEN, REPETITION )
speedtest( "emodulo3", emodulo2, LISTLEN, REPETITION )
speedtest( "eucmod ", eucmod, LISTLEN, REPETITION )
Function speedtest( descr:String, fn:Int( x:Int, y:Int ), length:Int, qty:Int )
start = MilliSecs()
For Local n:Int = -qty To qty
Local result:Int = fn( n, length )
Next
finish = MilliSecs()
Print( descr+": "+(finish-start) )
End Function
Function xmodn:Int( x:Int, n:Int )
Return (x Mod n )
End Function
Function emodulo:Int( x:Int, n:Int )
Return (x Mod n + n) Mod n
End Function
Function emodulo2:Int( x:Int, n:Int )
If x>=0 Return x Mod n
Return (x Mod n + n) Mod n
End Function
Function emodulo3:Int( x:Int, n:Int ) ' @Henri
If x < 0 Then
Return (x Mod n + n) Mod n
Else
Return x Mod n
EndIf
End Function
The results were high in debug mode, but in release mode the C code was actually slower, so this appears to be the best solution:
Function emod:Int( dividend:Int, divisor:Int )
If dividend>=0 Return dividend Mod divisor
Return ( dividend Mod divisor + divisor ) Mod divisor
End Function
Si...