# HG changeset patch # User IBBoard # Date 1254853874 0 # Node ID 298b2ff956bbacecdfc7c5a2136b0ae97e6244e8 # Parent 7b6d1feb6e03abe3a3e5381626aa4ecc95b16a65 * Move IBBMath to a more sensible package - we're not in Java, so "lang" isn't the catch-all package * Obsolete the old version no-open-ticket diff -r 7b6d1feb6e03 -r 298b2ff956bb CustomMath/IBBMath.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CustomMath/IBBMath.cs Tue Oct 06 18:31:14 2009 +0000 @@ -0,0 +1,133 @@ +// This file (IBBMath.cs) is a part of the IBBoard project and is copyright 2009 IBBoard +// +// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. +// + +using System; + +namespace IBBoard.CustomMath +{ + /// + /// RoundType defines how a number should be rounded. "Up" always rounds up (Ceiling), "Down" always rounds down (Floor), "Banker" rounds + /// up or down as appropriate with a balanced bias (rounding towards even). Each rounding method also has a "ToHalf" version that performs + /// the same type of rounding, but to the closes half number instead of the closest whole number. + /// + public enum RoundType + { + Up, + Down, + Banker, + UpToHalf, + DownToHalf, + BankerToHalf + } + + /// + /// IBBMath provides a number of custom Maths functions based on the core Math classes. + /// + public class IBBMath + { + /// + /// Rounds a number to the closest half, with a bias towards whole numbers. This is equivalent to 'round-to-even' in that + /// 0.25 is rounded down to 0.0 and 0.75 is rounded up to 1.0 so that a bias isn't introduced by rounding. + /// + /// + /// The to round to the nearest 0.5 + /// + /// + /// param rounded to the nearest 0.5 + /// + public static double RoundToHalf(double number) + { + return Math.Round(number * 2) / 2; + } + + /// + /// Returns the largest whole or half number that is less than or equal to the specified number. + /// + /// + /// The to round to the nearest 0.5 + /// + /// + /// param rounded to the nearest 0.5 that is less than or equal to param + /// + public static double FloorToHalf(double number) + { + return Math.Floor(number * 2) / 2; + } + + /// + /// Returns the smallest whole or half number that is greater than or equal to the specified number. + /// + /// + /// The to round to the nearest 0.5 + /// + /// + /// param rounded to the nearest 0.5 that is greater than or equal to param + /// + public static double CeilToHalf(double number) + { + return Math.Ceiling(number * 2) / 2; + } + + /// + /// Returns the number rounded as defined by the roundType + /// + /// + /// The to round + /// + /// + /// The way in which number should be rounded + /// + /// + /// The rounded + /// + public static double Round(double number, RoundType roundType) + { + double val; + + switch (roundType) + { + case RoundType.Up: + val = Math.Ceiling(number); + break; + case RoundType.Down: + val = Math.Floor(number); + break; + case RoundType.Banker: + val = Math.Round(number); + break; + case RoundType.UpToHalf: + val = CeilToHalf(number); + break; + case RoundType.DownToHalf: + val = FloorToHalf(number); + break; + case RoundType.BankerToHalf: + val = RoundToHalf(number); + break; + default: + throw new InvalidOperationException("Unhandled round type: "+roundType); + } + + return val; + } + + /// + /// Returns the number rounded up or down to the closest whole number. + /// + /// + /// The to round + /// + /// + /// true to round up, else rounds down + /// + /// + /// The rounded + /// + public static double Round(double number, bool roundUp) + { + return (roundUp ? Math.Ceiling(number) : Math.Floor(number)); + } + } +} diff -r 7b6d1feb6e03 -r 298b2ff956bb IBBoard.csproj --- a/IBBoard.csproj Sun Oct 04 19:30:31 2009 +0000 +++ b/IBBoard.csproj Tue Oct 06 18:31:14 2009 +0000 @@ -1,4 +1,4 @@ - + Local @@ -127,6 +127,7 @@ + diff -r 7b6d1feb6e03 -r 298b2ff956bb Lang/IBBMath.cs --- a/Lang/IBBMath.cs Sun Oct 04 19:30:31 2009 +0000 +++ b/Lang/IBBMath.cs Tue Oct 06 18:31:14 2009 +0000 @@ -7,11 +7,7 @@ namespace IBBoard.Lang { - /// - /// RoundType defines how a number should be rounded. "Up" always rounds up (Ceiling), "Down" always rounds down (Floor), "Banker" rounds - /// up or down as appropriate with a balanced bias (rounding towards even). Each rounding method also has a "ToHalf" version that performs - /// the same type of rounding, but to the closes half number instead of the closest whole number. - /// + [Obsolete("Use IBBoard.CustomMeth.IBBMath instead")] public enum RoundType { Up, @@ -25,6 +21,7 @@ /// /// IBBMath provides a number of custom Maths functions based on the core Math classes. /// + [Obsolete("Use IBBoard.CustomMeth.IBBMath instead")] public class IBBMath { /// @@ -39,7 +36,7 @@ /// public static double RoundToHalf(double number) { - return Math.Round(number * 2) / 2; + return IBBoard.CustomMath.IBBMath.RoundToHalf(number); } /// @@ -53,7 +50,7 @@ /// public static double FloorToHalf(double number) { - return Math.Floor(number * 2) / 2; + return IBBoard.CustomMath.IBBMath.FloorToHalf(number); } /// @@ -67,7 +64,7 @@ /// public static double CeilToHalf(double number) { - return Math.Ceiling(number * 2) / 2; + return IBBoard.CustomMath.IBBMath.CeilToHalf(number); } /// @@ -84,33 +81,8 @@ /// public static double Round(double number, RoundType roundType) { - double val; - - switch (roundType) - { - case RoundType.Up: - val = Math.Ceiling(number); - break; - case RoundType.Down: - val = Math.Floor(number); - break; - case RoundType.Banker: - val = Math.Round(number); - break; - case RoundType.UpToHalf: - val = CeilToHalf(number); - break; - case RoundType.DownToHalf: - val = FloorToHalf(number); - break; - case RoundType.BankerToHalf: - val = RoundToHalf(number); - break; - default: - throw new InvalidOperationException("Unhandled round type: "+roundType); - } - - return val; + IBBoard.CustomMath.RoundType newRoundType = (IBBoard.CustomMath.RoundType) Enum.Parse(typeof(IBBoard.CustomMath.RoundType), roundType.ToString()); + return IBBoard.CustomMath.IBBMath.Round(number, newRoundType); } /// @@ -127,7 +99,7 @@ /// public static double Round(double number, bool roundUp) { - return (roundUp ? Math.Ceiling(number) : Math.Floor(number)); + return IBBoard.CustomMath.IBBMath.Round(number, roundUp); } } }