comparison CustomMath/IBBMath.cs @ 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
children 972cc51adeeb
comparison
equal deleted inserted replaced
45:7b6d1feb6e03 46:298b2ff956bb
1 // This file (IBBMath.cs) is a part of the IBBoard project and is copyright 2009 IBBoard
2 //
3 // 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.
4 //
5
6 using System;
7
8 namespace IBBoard.CustomMath
9 {
10 /// <summary>
11 /// RoundType defines how a number should be rounded. "Up" always rounds up (Ceiling), "Down" always rounds down (Floor), "Banker" rounds
12 /// up or down as appropriate with a balanced bias (rounding towards even). Each rounding method also has a "ToHalf" version that performs
13 /// the same type of rounding, but to the closes half number instead of the closest whole number.
14 /// </summary>
15 public enum RoundType
16 {
17 Up,
18 Down,
19 Banker,
20 UpToHalf,
21 DownToHalf,
22 BankerToHalf
23 }
24
25 /// <summary>
26 /// IBBMath provides a number of custom Maths functions based on the core Math classes.
27 /// </summary>
28 public class IBBMath
29 {
30 /// <summary>
31 /// Rounds a number to the closest half, with a bias towards whole numbers. This is equivalent to 'round-to-even' in that
32 /// 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.
33 /// </summary>
34 /// <param name="number">
35 /// The <see cref="System.Double"/> to round to the nearest 0.5
36 /// </param>
37 /// <returns>
38 /// <code>param</code> rounded to the nearest 0.5
39 /// </returns>
40 public static double RoundToHalf(double number)
41 {
42 return Math.Round(number * 2) / 2;
43 }
44
45 /// <summary>
46 /// Returns the largest whole or half number that is less than or equal to the specified number.
47 /// </summary>
48 /// <param name="number">
49 /// The <see cref="System.Double"/> to round to the nearest 0.5
50 /// </param>
51 /// <returns>
52 /// <code>param</code> rounded to the nearest 0.5 that is less than or equal to <code>param</code>
53 /// </returns>
54 public static double FloorToHalf(double number)
55 {
56 return Math.Floor(number * 2) / 2;
57 }
58
59 /// <summary>
60 /// Returns the smallest whole or half number that is greater than or equal to the specified number.
61 /// </summary>
62 /// <param name="number">
63 /// The <see cref="System.Double"/> to round to the nearest 0.5
64 /// </param>
65 /// <returns>
66 /// <code>param</code> rounded to the nearest 0.5 that is greater than or equal to <code>param</code>
67 /// </returns>
68 public static double CeilToHalf(double number)
69 {
70 return Math.Ceiling(number * 2) / 2;
71 }
72
73 /// <summary>
74 /// Returns the number rounded as defined by the <code>roundType</code>
75 /// </summary>
76 /// <param name="number">
77 /// The <see cref="System.Double"/> to round
78 /// </param>
79 /// <param name="roundType">
80 /// The way in which <code>number</code> should be rounded
81 /// </param>
82 /// <returns>
83 /// The rounded <see cref="System.Double"/>
84 /// </returns>
85 public static double Round(double number, RoundType roundType)
86 {
87 double val;
88
89 switch (roundType)
90 {
91 case RoundType.Up:
92 val = Math.Ceiling(number);
93 break;
94 case RoundType.Down:
95 val = Math.Floor(number);
96 break;
97 case RoundType.Banker:
98 val = Math.Round(number);
99 break;
100 case RoundType.UpToHalf:
101 val = CeilToHalf(number);
102 break;
103 case RoundType.DownToHalf:
104 val = FloorToHalf(number);
105 break;
106 case RoundType.BankerToHalf:
107 val = RoundToHalf(number);
108 break;
109 default:
110 throw new InvalidOperationException("Unhandled round type: "+roundType);
111 }
112
113 return val;
114 }
115
116 /// <summary>
117 /// Returns the number rounded up or down to the closest whole number.
118 /// </summary>
119 /// <param name="number">
120 /// The <see cref="System.Double"/> to round
121 /// </param>
122 /// <param name="roundUp">
123 /// <code>true</code> to round up, else rounds down
124 /// </param>
125 /// <returns>
126 /// The rounded <see cref="System.Double"/>
127 /// </returns>
128 public static double Round(double number, bool roundUp)
129 {
130 return (roundUp ? Math.Ceiling(number) : Math.Floor(number));
131 }
132 }
133 }