/**
* Returns a number between kLowerBound and kUpperBound
* e.g.: Wrap(-1, 0, 4); // Returns 4
* e.g.: Wrap(5, 0, 4); // Returns 0
*/
int Wrap(int const kX, int const kLowerBound, int const kUpperBound)
{
// Suggest an implementation?
}
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Do the Java Integer and Double objects have unnece
- Why does const allow implicit conversion of refere
- thread_local variables initialization
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- What are the problems associated to Best First Sea
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- ceil conterpart for Math.floorDiv in Java?
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
The sign of
a % b
is only defined ifa
andb
are both non-negative.Why not using Extension methods.
Usage:
currentInt = (++currentInt).Wrap(0, 2);
An answer that has some symmetry and also makes it obvious that when kX is in range, it is returned unmodified.
I've faced this problem as well. This is my solution.
I don't know if it's good, but I'd thought I'd share since I got directed here when doing a Google search on this problem and found the above solutions lacking to my needs. =)
Please do not overlook this post. :)
Is this any good?
This works for negative inputs, and all arguments can be negative so long as L is less than H.
Background... (Note that
H
here is the reused variable, set to originalH-L+1
).I had been using
(N-L)%H+L
when incrementing, but unlike in Lua, which I used before starting to learn C a few months back, this would NOT work if I used inputs below the lower bound, never mind negative inputs. (Lua is built in C, but I don't know what it's doing, and it likely wouldn't be fast...)I decided to add
+(N<L)*H
to make(N-L+(N<L)*H)%H+L
, as C seems to be defined such that true=1 and false=0. It works well enough for me, and seems to answer the original question neatly. If anyone knows how to do it without the MOD operator % to make it dazzlingly fast, please do it. I don't need speed right now, but some time I will, no doubt.EDIT:
That function fails if
N
is lower thanL
by more thanH-L+1
but this doesn't:I think it would break at the negative extreme of the integer range in any system, but should work for most practical situations. It adds an extra multiplication and a division, but is still fairly compact.
(This edit is just for completion, because I came up with a much better way, in a newer post in this thread.)
Crow.
The following should work independently of the implementation of the mod operator:
An advantage over other solutions is, that it uses only a single % (i.e. division), which makes it pretty efficient.
Note (Off Topic):
It's a good example, why sometimes it is wise to define intervals with the upper bound being being the first element not in the range (such as for STL iterators...). In this case, both "+1" would vanish.