Results 1 to 12 of 12

Thread: [RESOLVED] Loop number over

  1. #1

    Thread Starter
    Member
    Join Date
    May 2022
    Location
    App.Path
    Posts
    54

    Resolved [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.

  2. #2
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,736

    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.

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,603

    Re: Loop number over

    Quote Originally Posted by VanGoghGaming View Post
    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?
    Whatever(256) = 1.
    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

  4. #4
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,736

    Talking Re: Loop number over

    Quote Originally Posted by Zvoni View Post
    Shouldn't that be i mod 255?
    255 doesn't satisfy his condition "Whatever(255) = 255". LoL, I think the specifications should be clearer.

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,603

    Re: Loop number over

    Quote Originally Posted by VanGoghGaming View Post
    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

  6. #6

  7. #7
    Fanatic Member
    Join Date
    Nov 2011
    Posts
    548

    Re: Loop number over

    Quote Originally Posted by Zvoni View Post
    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

  8. #8
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,603

    Re: Loop number over

    Quote Originally Posted by k_zeon View Post
    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

  9. #9
    Addicted Member
    Join Date
    Jul 2021
    Posts
    227

    Re: Loop number over

    Quote Originally Posted by OrangeBlaze View Post
    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 0-255, not 1-255.
    But if so, your formula for X>255 is (X - 256) Mod 255 + 1
    And for X<0 is X And 255

  10. #10
    Hyperactive Member
    Join Date
    Jan 2018
    Posts
    286

    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.

  11. #11

    Thread Starter
    Member
    Join Date
    May 2022
    Location
    App.Path
    Posts
    54

    Re: Loop number over

    Quote Originally Posted by Dry Bone View Post
    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.

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,318

    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
  •  



Click Here to Expand Forum to Full Width