Why no wrap() function like clamp() in std::num?

std::num has a clamp() function, but no wrap() function. We have a Wrapping struct that as far as I know only works with min and max constants of primitive number types but not with a custom range. We can use clamp() with custom min and max and it's quite useful. A wrap() can be useful to accurately represent party parrot movements, without worrying about out of bounds and panicking parrots all over the place.

click to expand some party parrot gifs [warning: flashing colors]

200
Nyanparrot

There are wrapping_add methods for all the integer types.

In my experience, rem_euclid() is usually all you need for wrapping behaviors. The lower end of the range must be 0, but that's a simple addition to fix if needed, and the value starting at 0 is often useful somewhere in the computation, e.g. to pick an animation frame.

5 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.