Wrap values with upper and lower bound? [modified]
-
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
-
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
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
-
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
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.
-
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.
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|
-
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|
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?
-
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?
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|
-
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|
-
So I could check to see whether originalValue is negative or not and if it is I do what?
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|
-
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|
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 :)