Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. Wrap values with upper and lower bound? [modified]

Wrap values with upper and lower bound? [modified]

Scheduled Pinned Locked Moved Algorithms
csharphelptutorialquestion
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Thrash505
    wrote on last edited by
    #1

    I've created a wrapvalue function in C#, but I can't figure out how to make it work with a lower bound, like a negative. Here's the function at the moment:

    private int WrapValue( int val, int upperBound ) {
    return val - ( upperBound * ( val / upperBound ) );
    }

    WrapValue( 1023, 360 ) would return 303. By the way ( val / upperBound ) is integer division. It works great for positive values, but not negatives and you can't specify a lower bound. Does anyone know how I to make it work with negatives and a lower bound so I could do something like this: WrapValue( int val, int lowerBound, int upperBound ) WrapValue( -300, -500, 123 ) Thanks in advance. By the way I'm using this to scroll a semi-transparent image over another image in the shape of a circle to create the illusion of a rotating sphere. The semi-transparent image needs to wrap around in a certain way and this function will help me accomplish that if I can get working correctly.

    modified on Sunday, July 20, 2008 5:19 PM

    L 1 Reply Last reply
    0
    • T Thrash505

      I've created a wrapvalue function in C#, but I can't figure out how to make it work with a lower bound, like a negative. Here's the function at the moment:

      private int WrapValue( int val, int upperBound ) {
      return val - ( upperBound * ( val / upperBound ) );
      }

      WrapValue( 1023, 360 ) would return 303. By the way ( val / upperBound ) is integer division. It works great for positive values, but not negatives and you can't specify a lower bound. Does anyone know how I to make it work with negatives and a lower bound so I could do something like this: WrapValue( int val, int lowerBound, int upperBound ) WrapValue( -300, -500, 123 ) Thanks in advance. By the way I'm using this to scroll a semi-transparent image over another image in the shape of a circle to create the illusion of a rotating sphere. The semi-transparent image needs to wrap around in a certain way and this function will help me accomplish that if I can get working correctly.

      modified on Sunday, July 20, 2008 5:19 PM

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, what you are trying to achieve is to alias an original value into a range [low,high); the way to achieve that is by adding/subtracting high-low as often as is required to get the value inside the given range. We know that (for integers and with b>0) a-a/b*b returns a number in [0, b) that is the range including 0 and up to but excluding b. And the modulo operator does exactly the same: a%b gives the remainder of a/b So it will look like newValue=origValue-(origValue-low)%(high-low) [ADDED] above formula is wrong, both test cases don't detect it. It should be: newValue=low+(origValue-low)%(high-low) [/ADDED] Fast proof (using diff for high-low): 1. for origValue=low, newValue=origValue-0*diff which equals low 2. for origValue=high, newValue=origValue-1*diff which equals high-(high-low) hence low as it should You'll have to check the behavior for negative numbers, I expect it to be all right. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Voting for dummies? No thanks. X|


      modified on Sunday, July 20, 2008 6:01 PM

      T 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, what you are trying to achieve is to alias an original value into a range [low,high); the way to achieve that is by adding/subtracting high-low as often as is required to get the value inside the given range. We know that (for integers and with b>0) a-a/b*b returns a number in [0, b) that is the range including 0 and up to but excluding b. And the modulo operator does exactly the same: a%b gives the remainder of a/b So it will look like newValue=origValue-(origValue-low)%(high-low) [ADDED] above formula is wrong, both test cases don't detect it. It should be: newValue=low+(origValue-low)%(high-low) [/ADDED] Fast proof (using diff for high-low): 1. for origValue=low, newValue=origValue-0*diff which equals low 2. for origValue=high, newValue=origValue-1*diff which equals high-(high-low) hence low as it should You'll have to check the behavior for negative numbers, I expect it to be all right. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Voting for dummies? No thanks. X|


        modified on Sunday, July 20, 2008 6:01 PM

        T Offline
        T Offline
        Thrash505
        wrote on last edited by
        #3

        hmmm... When I try this function:

        private int WrapValue( int val, int lowerBound, int upperBound ) {
        return val - ( val - lowerBound ) % ( upperBound - lowerBound );
        }

        with: WrapValue( 1023, 0, 360 ) I get 720 as a result. when I try this function:

        private int WrapValue( int val, int lowerBound, int upperBound ) {
        return val - ( val - lowerBound ) / ( upperBound - lowerBound );
        }

        with: WrapValue( 1023, 0, 360 ) I get 1021 as a result. But, shouldn't I be getting 303 still? I haven't tried with negatives or a different lower bound yet.

        L 1 Reply Last reply
        0
        • T Thrash505

          hmmm... When I try this function:

          private int WrapValue( int val, int lowerBound, int upperBound ) {
          return val - ( val - lowerBound ) % ( upperBound - lowerBound );
          }

          with: WrapValue( 1023, 0, 360 ) I get 720 as a result. when I try this function:

          private int WrapValue( int val, int lowerBound, int upperBound ) {
          return val - ( val - lowerBound ) / ( upperBound - lowerBound );
          }

          with: WrapValue( 1023, 0, 360 ) I get 1021 as a result. But, shouldn't I be getting 303 still? I haven't tried with negatives or a different lower bound yet.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, sorry my mistake, the formula should be:

          newValue=low+(origValue-low)%(high-low)

          which really means wrap around every high-low, but perform an offset of low In your second snippet, you had / again instead of % :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Voting for dummies? No thanks. X|


          T 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, sorry my mistake, the formula should be:

            newValue=low+(origValue-low)%(high-low)

            which really means wrap around every high-low, but perform an offset of low In your second snippet, you had / again instead of % :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Voting for dummies? No thanks. X|


            T Offline
            T Offline
            Thrash505
            wrote on last edited by
            #5

            Well it's closer... Using this function:

            private int WrapValue( int val, int lowerBound, int upperBound ) {
            return lowerBound + ( val - lowerBound ) % ( upperBound - lowerBound );
            }

            WrapValue( 1023, 0, 360 ) gives me 303. But WrapValue( -5, 0, 360 ) gives me -5, but it shouldn't it give me 355?

            L 1 Reply Last reply
            0
            • T Thrash505

              Well it's closer... Using this function:

              private int WrapValue( int val, int lowerBound, int upperBound ) {
              return lowerBound + ( val - lowerBound ) % ( upperBound - lowerBound );
              }

              WrapValue( 1023, 0, 360 ) gives me 303. But WrapValue( -5, 0, 360 ) gives me -5, but it shouldn't it give me 355?

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              OK, This page[^] explains a bit about modulo for negative numbers. Seems like in C# a%b will return in (-b,0] for negative a, always assuming positive b. To fix this, you will need a conditional test. Either check for origValueor keep the formula, and add another diff if it resulted below low. As a hack, you could forget the test, and start of with originalValue+99999*(high-low) but doing so reduces your useful range and/or risks an overflow, which would ruin everything. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Voting for dummies? No thanks. X|


              T 1 Reply Last reply
              0
              • L Luc Pattyn

                OK, This page[^] explains a bit about modulo for negative numbers. Seems like in C# a%b will return in (-b,0] for negative a, always assuming positive b. To fix this, you will need a conditional test. Either check for origValueor keep the formula, and add another diff if it resulted below low. As a hack, you could forget the test, and start of with originalValue+99999*(high-low) but doing so reduces your useful range and/or risks an overflow, which would ruin everything. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Voting for dummies? No thanks. X|


                T Offline
                T Offline
                Thrash505
                wrote on last edited by
                #7

                So I could check to see whether originalValue is negative or not and if it is I do what?

                L 1 Reply Last reply
                0
                • T Thrash505

                  So I could check to see whether originalValue is negative or not and if it is I do what?

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  if the result is less than low, adding another high-low seems the easiest way of dealing with it.

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Voting for dummies? No thanks. X|


                  T 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    if the result is less than low, adding another high-low seems the easiest way of dealing with it.

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    Voting for dummies? No thanks. X|


                    T Offline
                    T Offline
                    Thrash505
                    wrote on last edited by
                    #9

                    Nice, it works :) The function:

                    private int WrapValue( int val, int lowerBound, int upperBound ) {
                    int result = lowerBound + ( val - lowerBound ) % ( upperBound - lowerBound );
                    if( result < lowerBound ) {
                    result += ( upperBound - lowerBound );
                    }
                    return result;
                    }

                    I tried it with all different bounds and values and it works great. Thanks a ton :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups