# wraps the mod result to avoid negative results.

```

//http://isotopescreencapture.codeplex.com/
namespace Isotope.Math
{
public static class MathUtil
{
/// <summary>
/// This is a variant of mod that wraps the mod result to avoid negative results. this is what Python&#039;s mod operator does
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private static double mod_wrap_angle(double x, double y)
{
if (y == 0)
{
throw new System.DivideByZeroException();
}

double r = x%y;
if (r > 0 &amp;&amp; y < 0)
{
r = r + y;
}
else if (r < 0 &amp;&amp; y > 0)
{
r = r + y;
}
return r;
}

/// <summary>
/// wraps a number around so that it always fits between 0.0 and 1.0. negative numbers will wrap around to the correct positive number
/// </summary>
/// <remarks>
/// if the input number is already in the range, no change will occur
/// </remarks>
/// <param name="v">input value </param>
/// <returns>the wrapped number</returns>
public static double WrapAngle_0_1(double v)
{
const double min = 0.0;
const double max = 1.0;
if (IsInRange(v, min, max))
{
// the number is already in the range so do nothing
return v;
}
return mod_wrap_angle(v, max);
}
/// <summary>
/// Checks if a value is in a range (inclusive)
/// </summary>
/// <param name="val"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static bool IsInRange(double val, double min, double max)
{
return ((min <= val) &amp;&amp; (val <= max));
}

/// <summary>
/// Checks if a value is in the range 0.0 to 1.0 inclusive
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public static bool IsInRange_0_1(double val)
{
return IsInRange(val, 0.0, 1.0);
}
}
}

```