wraps the mod result to avoid negative results. : Math « Development Class « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Development Class » MathScreenshots 
wraps the mod result to avoid negative results.
 

//http://isotopescreencapture.codeplex.com/
//The MIT License (MIT)
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'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 > && y < 0)
            {
                r = r + y;
            }
            else if (r < && 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&& (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.01.0);
        }        
   }
}

   
  
Related examples in the same category
1.Demonstrate Math.Sin(), Math.Cos(), and Math.Tan(). Demonstrate Math.Sin(), Math.Cos(), and Math.Tan().
2.Find the radius of a circle given its area. Find the radius of a circle given its area.
3.Math operators
4.Use Math.Round
5.Math.Sign
6.Math.Abs
7.Math.Min
8.Converts a degrees angle into a radians angle.
9.Converts a radians angle into a degrees angle.
10.Converts degrees to radians.
11.Gets the mean value from a list
12.Gets the median from the list
13.Gets the standard deviation
14.Statistical functions: Mean
15.Statistical functions: Standard Deviation
16.Statistical functions: Standard Deviation
17.The Method converts the temperature in Fahrenheit to Celcius
18.The Method converts the temperature in Celcius to Fahrenheit
19.Returns the maximum value of three numbers
20.Returns the minimum value of three numbers
21.rounds val to the nearest fractional value
22.Combines two input numbers in some proportion.
23.Normalise Angle
24.Method that calculates the Greatest Common Divisor (GCD) of two positive integer numbers.
25.Calculates the floor of the log, base 2, of 'x'.
26.Calculates the Least Common Multiple (LCM) of two strictly positive integer numbers.
27.Get Prime, Is prime
28.Same as System.Math.Acos but if angle is out of range, the result is clamped.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.