I have a number that needs to be kept in a range from 0 to 9
-1 would be 9
10 would 0
I tried to mod operator but for negative number its still returning a negative number.
I have a number that needs to be kept in a range from 0 to 9
-1 would be 9
10 would 0
I tried to mod operator but for negative number its still returning a negative number.
would -2 be 8 ?
and 11 be 1 ?
just want to make sure i’m understanding this right
[quote=486637:@Norman Palardy]would -2 be 8 ?
and 11 be 1 ?
just want to make sure i’m understanding this right[/quote]
Yes that is the case.
it’s a wrapping map.
If it’s less than zero add 10
If it’s greater than 10 subtract 10
Does that work?
just what I was writing
[quote=486639:@Tim Streater]If it’s less than zero add 10
If it’s greater than 10 subtract 10
Does that work?[/quote]
mod is never greater than the parameter, but when it is negative then it doesn’t behave the way i understood mod to opperate.
something like this seems to work
For i As Integer = -30 To 30
Dim remainder As Integer
Dim newvalue As Integer
If i < 0 Then
remainder = i Mod 10
If remainder < 0 Then
newvalue = 10 + remainder
End If
Else
remainder = i Mod 10
newvalue = remainder
End If
listbox1.AddRow Str(i) + "->" + Str(newValue)
Next
I so wish we had an immediate mode!!!
a lot of stuff can be tested in an IDE script window
[code]Function Run(args() as String) Handles Run as Integer
dim i, r as integer
dim ub as integer = 9
for i=-2 to 11
r = i mod (ub+1)
if (r < 0) then r = (ub+1) + r
Print(str(i) + " mod 10 = " + str®)
next
End Function
[/code]
Complicating things, there’s quite a variation in how different software packages implement the mod function for negative values. Excel handles it differently than Xojo.
I think the following will do what you want, with j=10.
Public Function ModFix(i As Integer, j As Integer) as Integer
dim m As Integer = i mod j
if m<0 then return m+j
return m
End Function
Norman, thanks for that hint.
Just like Brian O’Brien, I was wishing for an immediate window the other day to try out some simple stuff. It took me a while to find out how to launch a Xojo Script window since I’ve never used Xojo Script:
File / IDE Scripts / New IDE Script
But that did the trick. Coincidentally I also was trying to figure out the behavior of the MOD operator when I was wishing for an immediate window.
Xojo just keeps revealing new things!
-Wes
the IDE script window is handy for testing out some things
not all code works there though
anything that requires the runtime framework - folderitems etc wont work
but still you can prototype a lot of ideas there