
Jun 19th, 2024, 10:32 PM
#1
Thread Starter
Member
[RESOLVED] Loop number over
Hello all! I am not sure if I can properly describe this. I want a function that takes a long and if it is over 255, loops over back to 1. e.g. Whatever(255) = 255; Whatever(256) = 1.
If it is a negative number, it does opposite. Whatever(0) = 0; Whatever(1) = 255. I've gotten close to this but not quite. I am losing my mind over this and I'm sure it something really simple that I missed. I will be calling this function enough that nothing slow will work.
Thank you all in advance, help is much appreciated!!
(would this question be a math question or VB question??)
No, Shaggy Hiker, I am not stealing your signature.

Jun 19th, 2024, 10:49 PM
#2
Re: Loop number over
I'm not sure what you want for negative numbers but for positive numbers it should be Whatever(i) = i mod 256. Modular arithmetic works within a confined interval.

Jun 20th, 2024, 12:47 AM
#3
Re: Loop number over
Originally Posted by VanGoghGaming
I'm not sure what you want for negative numbers but for positive numbers it should be Whatever(i) = i mod 256. Modular arithmetic works within a confined interval.
Shouldn't that be i mod 255?
If i=256 > 256 mod 255 > Result = 1
And agreed: No idea how negative numbers are supposed to work
Last edited by Zvoni; Tomorrow at 31:69 PM.

One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie

People call me crazy because i'm jumping out of perfectly fine airplanes.

Code is like a joke: If you have to explain it, it's bad

Jun 20th, 2024, 01:03 AM
#4
Re: Loop number over
Originally Posted by Zvoni
Shouldn't that be i mod 255?
255 doesn't satisfy his condition "Whatever(255) = 255". LoL, I think the specifications should be clearer.

Jun 20th, 2024, 01:20 AM
#5
Re: Loop number over
Originally Posted by VanGoghGaming
255 doesn't satisfy his condition "Whatever(255) = 255". LoL, I think the specifications should be clearer.
Right, i walked into that one
How about this?
Code:
Sub main()
Dim i As Long
For i = 0 To 258
Debug.Print "i=" & i & "  Result: " & i \ 256 + i Mod 256
Next
End Sub
Result:
i=250  Result: 250
i=251  Result: 251
i=252  Result: 252
i=253  Result: 253
i=254  Result: 254
i=255  Result: 255
i=256  Result: 1
i=257  Result: 2
i=258  Result: 3
Last edited by Zvoni; Tomorrow at 31:69 PM.

One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie

People call me crazy because i'm jumping out of perfectly fine airplanes.

Code is like a joke: If you have to explain it, it's bad

Jun 20th, 2024, 01:48 AM
#6
Re: Loop number over

Jun 20th, 2024, 05:32 AM
#7
Fanatic Member
Re: Loop number over
Originally Posted by Zvoni
Right, i walked into that one
How about this?
Code:
Sub main()
Dim i As Long
For i = 0 To 258
Debug.Print "i=" & i & "  Result: " & i \ 256 + i Mod 256
Next
End Sub
Result:
i=250  Result: 250
i=251  Result: 251
i=252  Result: 252
i=253  Result: 253
i=254  Result: 254
i=255  Result: 255
i=256  Result: 1
i=257  Result: 2
i=258  Result: 3
if you change 258 to 1000 , it give incorrect data. i put 1000 and on concurrent loops, then next time will start at 2 then next will start at 3
Could use below. a little bit slower
Dim i As Long, ReLoop As Long
For i = 0 To 1000
If ReLoop = 256 Then ReLoop = 1
Debug.Print "i=" & i & "  Result: " & ReLoop
ReLoop = ReLoop + 1
Next

Jun 20th, 2024, 05:50 AM
#8
Re: Loop number over
Originally Posted by k_zeon
if you change 258 to 1000 , it give incorrect data. i put 1000 and on concurrent loops, then next time will start at 2 then next will start at 3
Could use below. a little bit slower
Dim i As Long, ReLoop As Long
For i = 0 To 1000
If ReLoop = 256 Then ReLoop = 1
Debug.Print "i=" & i & "  Result: " & ReLoop
ReLoop = ReLoop + 1
Next
Agreed. Didn't consider that.
Though, it's not making much sense:
for i=511 it jumps back to 1 which is not making sense, since 511 is not 2x256
But that's OP's Problem, not ours
Last edited by Zvoni; Jun 20th, 2024 at 05:59 AM.
Last edited by Zvoni; Tomorrow at 31:69 PM.

One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie

People call me crazy because i'm jumping out of perfectly fine airplanes.

Code is like a joke: If you have to explain it, it's bad

Jun 20th, 2024, 06:29 AM
#9
Addicted Member
Re: Loop number over
Originally Posted by OrangeBlaze
Hello all! I am not sure if I can properly describe this. I want a function that takes a long and if it is over 255, loops over back to 1. e.g. Whatever(255) = 255; Whatever(256) = 1.
If it is a negative number, it does opposite. Whatever(0) = 0; Whatever(1) = 255. I've gotten close to this but not quite. I am losing my mind over this and I'm sure it something really simple that I missed. I will be calling this function enough that nothing slow will work.
Thank you all in advance, help is much appreciated!!
(would this question be a math question or VB question??)
Following your logic:
Whatever(255)=255
Whatever(256)=1
Whatever(257)=2
...
Whatever(510)=255
Whatever(511)=1
Whatever(512)=2
Is this really what you want?
The more logical way is to loop 0255, not 1255.
But if so, your formula for X>255 is (X  256) Mod 255 + 1
And for X<0 is X And 255

Jun 20th, 2024, 03:58 PM
#10
Hyperactive Member
Re: Loop number over
I use this function for those situations:
Code:
Function Modulus(Numerator As Long, Modulo As Long)
Modulus = ((Numerator Mod Modulo) + Modulo) Mod Modulo
End Function
Modulus(1,256)=1
Modulus(1,256)=255
Since normal Mod doesn't handle negatives correctly for my most common uses.

Jun 20th, 2024, 07:10 PM
#11
Thread Starter
Member
Re: Loop number over
Originally Posted by Dry Bone
But if so, your formula for X>255 is (X  256) Mod 255 + 1
And for X<0 is X And 255
Yes, this is what works perfectly for me!! Thank you and everyone else who helped!!
No, Shaggy Hiker, I am not stealing your signature.

Jun 24th, 2024, 12:51 PM
#12
Re: [RESOLVED] Loop number over
Or just:
Code:
y = (x And 255) Mod 256
Although, looking at your OP, you say that 256 > 1. It seems to me that 256 should go to 0, which is what the above will do.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
