changeset 46:298b2ff956bb

* 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
author IBBoard <dev@ibboard.co.uk>
date Tue, 06 Oct 2009 18:31:14 +0000
parents 7b6d1feb6e03
children 966ba575d4e6
files CustomMath/IBBMath.cs IBBoard.csproj Lang/IBBMath.cs
diffstat 3 files changed, 143 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- /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
+{
+	/// <summary>
+	/// 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.
+	/// </summary>
+	public enum RoundType
+	{
+		Up,
+		Down,
+		Banker,
+		UpToHalf,
+		DownToHalf,
+		BankerToHalf
+	}
+
+	/// <summary>
+	/// IBBMath provides a number of custom Maths functions based on the core Math classes.
+	/// </summary>
+	public class IBBMath
+	{
+		/// <summary>
+		/// 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.
+		/// </summary>
+		/// <param name="number">
+		/// The <see cref="System.Double"/> to round to the nearest 0.5
+		/// </param>
+		/// <returns>
+		/// <code>param</code> rounded to the nearest 0.5
+		/// </returns>
+		public static double RoundToHalf(double number)
+		{
+			return Math.Round(number * 2) / 2;
+		}
+		
+		/// <summary>
+		/// Returns the largest whole or half number that is less than or equal to the specified number.
+		/// </summary>
+		/// <param name="number">
+		/// The <see cref="System.Double"/> to round to the nearest 0.5
+		/// </param>
+		/// <returns>
+		/// <code>param</code> rounded to the nearest 0.5 that is less than or equal to <code>param</code>
+		/// </returns>
+		public static double FloorToHalf(double number)
+		{
+			return Math.Floor(number * 2) / 2;
+		}
+		
+		/// <summary>
+		/// Returns the smallest whole or half number that is greater than or equal to the specified number.
+		/// </summary>
+		/// <param name="number">
+		/// The <see cref="System.Double"/> to round to the nearest 0.5
+		/// </param>
+		/// <returns>
+		/// <code>param</code> rounded to the nearest 0.5 that is greater than or equal to <code>param</code>
+		/// </returns>
+		public static double CeilToHalf(double number)
+		{
+			return Math.Ceiling(number * 2) / 2;
+		}
+
+		/// <summary>
+		/// Returns the number rounded as defined by the <code>roundType</code>
+		/// </summary>
+		/// <param name="number">
+		/// The <see cref="System.Double"/> to round
+		/// </param>
+		/// <param name="roundType">
+		/// The way in which <code>number</code> should be rounded
+		/// </param>
+		/// <returns>
+		/// The rounded <see cref="System.Double"/>
+		/// </returns>
+		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;
+		}
+		
+		/// <summary>
+		/// Returns the number rounded up or down to the closest whole number.
+		/// </summary>
+		/// <param name="number">
+		/// The <see cref="System.Double"/> to round
+		/// </param>
+		/// <param name="roundUp">
+		/// <code>true</code> to round up, else rounds down
+		/// </param>
+		/// <returns>
+		/// The rounded <see cref="System.Double"/>
+		/// </returns>
+		public static double Round(double number, bool roundUp)
+		{
+			return (roundUp ? Math.Ceiling(number) : Math.Floor(number));
+		}
+	}
+}
--- 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 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <ProjectType>Local</ProjectType>
@@ -127,6 +127,7 @@
     <Compile Include="Xml\XmlTools.cs" />
     <Compile Include="IO\BinaryReaderBigEndian.cs" />
     <Compile Include="Lang\IBBMath.cs" />
+    <Compile Include="CustomMath\IBBMath.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="libs\log4net.dll" />
--- 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
 {
-	/// <summary>
-	/// 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.
-	/// </summary>
+	[Obsolete("Use IBBoard.CustomMeth.IBBMath instead")]
 	public enum RoundType
 	{
 		Up,
@@ -25,6 +21,7 @@
 	/// <summary>
 	/// IBBMath provides a number of custom Maths functions based on the core Math classes.
 	/// </summary>
+	[Obsolete("Use IBBoard.CustomMeth.IBBMath instead")]
 	public class IBBMath
 	{
 		/// <summary>
@@ -39,7 +36,7 @@
 		/// </returns>
 		public static double RoundToHalf(double number)
 		{
-			return Math.Round(number * 2) / 2;
+			return IBBoard.CustomMath.IBBMath.RoundToHalf(number);
 		}
 		
 		/// <summary>
@@ -53,7 +50,7 @@
 		/// </returns>
 		public static double FloorToHalf(double number)
 		{
-			return Math.Floor(number * 2) / 2;
+			return IBBoard.CustomMath.IBBMath.FloorToHalf(number);
 		}
 		
 		/// <summary>
@@ -67,7 +64,7 @@
 		/// </returns>
 		public static double CeilToHalf(double number)
 		{
-			return Math.Ceiling(number * 2) / 2;
+			return IBBoard.CustomMath.IBBMath.CeilToHalf(number);
 		}
 
 		/// <summary>
@@ -84,33 +81,8 @@
 		/// </returns>
 		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);
 		}
 		
 		/// <summary>
@@ -127,7 +99,7 @@
 		/// </returns>
 		public static double Round(double number, bool roundUp)
 		{
-			return (roundUp ? Math.Ceiling(number) : Math.Floor(number));
+			return IBBoard.CustomMath.IBBMath.Round(number, roundUp);
 		}
 	}
 }