I have a method in C# as follows (which wraps a number across a range, say 0 to 360... if you pass 0-359 you get the same value, if you pass 360 you get 0, 361 gets 1, etc.):
/// <summary>
/// Wraps the value across the specified boundary range.
///
/// If the value is in the range <paramref name="min"/> (inclusive) to <paramref name="max"/> (exclusive),
/// <paramref name="value"/> will be returned. If <paramref name="value"/> is equal to <paramref name="max"/>,
/// <paramref name="min"/> will be returned. The method essentially creates a loop between <paramref name="min"/>
/// and <paramref name="max"/>.
/// </summary>
/// <param name="value">The value to wrap.</param>
/// <param name="min">The minimum value of the boundary range, inclusive.</param>
/// <param name="max">The maximum value of the boundary range, exclusive.</param>
/// <returns>The value wrapped across the specified range.</returns>
public static T Wrap<T>(T value, T min, T max) where T : IComparable<T>
{
// If it's positive or negative infinity, we just return the minimum, which is the "origin"
bool infinityDouble = typeof(T) == typeof(double) && (double.IsPositiveInfinity(Convert.ToDouble(value)) || double.IsNegativeInfinity(Convert.ToDouble(value)));
bool infinityFloat = typeof(T) == typeof(float) && (float.IsPositiveInfinity(Convert.ToSingle(value)) || float.IsNegativeInfinity(Convert.ToSingle(value)));
if (infinityDouble || infinityFloat)
{
return min;
}
// If the value is between the origin (inclusive) and the maximum value (exclusive), just return the value
if (value.CompareTo(min) >= 0 && value.CompareTo(max) < 0)
{
return value;
}
// The range of the wrapping function
var range = (dynamic)max - (dynamic)min;
return ((((value % range) + range) - min) % range) + min;
}
I also needed this method in C++, which I defined as follows:
/*!
Wraps the value across the specified boundary range.
If the value is in the range \a min (inclusive) to \a max (exclusive), \a value will be returned.
If \a value is equal to \a max, \a min will be returned. The method essentially creates a loop between
\a min and \a max.
\param value The value to wrap.
\param min The minimum value of the boundary range, inclusive.
\param max The maximum value of the boundary range, exclusive.
\return The value wrapped across the specified range.
*/
template <typename T> const T& MathHelper::wrap(const T &value, const T &min, const T &max)
{
// If it's positive or negative infinity, we just return the minimum, which is the "origin"
bool infinityDouble = value == std::numeric_limits<double>::infinity() || value == -std::numeric_limits<double>::infinity();
bool infinityFloat = value == std::numeric_limits<float>::infinity() || value == -std::numeric_limits<float>::infinity();
if (infinityDouble || infinityFloat)
{
return min;
}
// If the value is between the origin (inclusive) and the maximum value (exclusive), just return the value
if (value >= min && value < max)
{
return value;
}
// The range of the wrapping function
T range = max - min;
return ((((value % range) + range) - min) % range) + min;
}
Now my question is: am I checking for infinity correctly in the C++ version? I can't see any way to say "if double, do these checks, if float, do these checks". If it's not the type I want, will it just return false? Also, why is the % operator not defined for float and double? I guess I'll have to implement the modulo operator myself. The method is pretty much intended for numeric types - byte, short, int, long, float, double.