annotate Lang/IBBMath.cs @ 43:2baf3f949cfb

* Make sure that we use "." as our decimal separator when parsing doubles from XML (should fix warfoundry:#185 for Europe) * Automated cleanups no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Oct 2009 09:47:16 +0000
parents ebc01964a918
children 298b2ff956bb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (IBBMath.cs) is a part of the IBBoard project and is copyright 2009 IBBoard
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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.
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 //
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System;
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 namespace IBBoard.Lang
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 {
34
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
10 /// <summary>
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
11 /// RoundType defines how a number should be rounded. "Up" always rounds up (Ceiling), "Down" always rounds down (Floor), "Banker" rounds
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
12 /// up or down as appropriate with a balanced bias (rounding towards even). Each rounding method also has a "ToHalf" version that performs
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
13 /// the same type of rounding, but to the closes half number instead of the closest whole number.
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
14 /// </summary>
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
15 public enum RoundType
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
16 {
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
17 Up,
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
18 Down,
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
19 Banker,
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
20 UpToHalf,
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
21 DownToHalf,
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
22 BankerToHalf
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
23 }
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
24
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
25 /// <summary>
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
26 /// IBBMath provides a number of custom Maths functions based on the core Math classes.
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
27 /// </summary>
31
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 public class IBBMath
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 {
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 /// <summary>
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 /// Rounds a number to the closest half, with a bias towards whole numbers. This is equivalent to 'round-to-even' in that
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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.
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 /// </summary>
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 /// <param name="number">
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 /// The <see cref="System.Double"/> to round to the nearest 0.5
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 /// </param>
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 /// <returns>
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 /// <code>param</code> rounded to the nearest 0.5
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 /// </returns>
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 public static double RoundToHalf(double number)
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 {
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 return Math.Round(number * 2) / 2;
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 }
33
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
44
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
45 /// <summary>
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
46 /// Returns the largest whole or half number that is less than or equal to the specified number.
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
47 /// </summary>
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
48 /// <param name="number">
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
49 /// The <see cref="System.Double"/> to round to the nearest 0.5
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
50 /// </param>
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
51 /// <returns>
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
52 /// <code>param</code> rounded to the nearest 0.5 that is less than or equal to <code>param</code>
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
53 /// </returns>
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
54 public static double FloorToHalf(double number)
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
55 {
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
56 return Math.Floor(number * 2) / 2;
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
57 }
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
58
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
59 /// <summary>
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
60 /// Returns the smallest whole or half number that is greater than or equal to the specified number.
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
61 /// </summary>
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
62 /// <param name="number">
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
63 /// The <see cref="System.Double"/> to round to the nearest 0.5
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
64 /// </param>
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
65 /// <returns>
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
66 /// <code>param</code> rounded to the nearest 0.5 that is greater than or equal to <code>param</code>
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
67 /// </returns>
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
68 public static double CeilToHalf(double number)
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
69 {
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
70 return Math.Ceiling(number * 2) / 2;
8971a1c48dbf Re #18 - Add rounding to half methods
IBBoard <dev@ibboard.co.uk>
parents: 31
diff changeset
71 }
34
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
72
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
73 /// <summary>
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
74 /// Returns the number rounded as defined by the <code>roundType</code>
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
75 /// </summary>
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
76 /// <param name="number">
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
77 /// The <see cref="System.Double"/> to round
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
78 /// </param>
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
79 /// <param name="roundType">
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
80 /// The way in which <code>number</code> should be rounded
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
81 /// </param>
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
82 /// <returns>
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
83 /// The rounded <see cref="System.Double"/>
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
84 /// </returns>
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
85 public static double Round(double number, RoundType roundType)
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
86 {
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
87 double val;
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
88
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
89 switch (roundType)
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
90 {
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
91 case RoundType.Up:
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
92 val = Math.Ceiling(number);
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
93 break;
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
94 case RoundType.Down:
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
95 val = Math.Floor(number);
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
96 break;
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
97 case RoundType.Banker:
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
98 val = Math.Round(number);
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
99 break;
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
100 case RoundType.UpToHalf:
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
101 val = CeilToHalf(number);
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
102 break;
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
103 case RoundType.DownToHalf:
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
104 val = FloorToHalf(number);
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
105 break;
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
106 case RoundType.BankerToHalf:
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
107 val = RoundToHalf(number);
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
108 break;
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
109 default:
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
110 throw new InvalidOperationException("Unhandled round type: "+roundType);
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
111 }
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
112
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
113 return val;
d597feec5dd4 Closes #20 - Add rounding method with enum
IBBoard <dev@ibboard.co.uk>
parents: 33
diff changeset
114 }
39
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
115
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
116 /// <summary>
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
117 /// Returns the number rounded up or down to the closest whole number.
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
118 /// </summary>
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
119 /// <param name="number">
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
120 /// The <see cref="System.Double"/> to round
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
121 /// </param>
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
122 /// <param name="roundUp">
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
123 /// <code>true</code> to round up, else rounds down
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
124 /// </param>
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
125 /// <returns>
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
126 /// The rounded <see cref="System.Double"/>
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
127 /// </returns>
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
128 public static double Round(double number, bool roundUp)
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
129 {
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
130 return (roundUp ? Math.Ceiling(number) : Math.Floor(number));
ebc01964a918 * Add short method to round up or down based on a boolean - useful for compacting rounding up/down calls when they need to be done inline
IBBoard <dev@ibboard.co.uk>
parents: 34
diff changeset
131 }
31
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
132 }
7a3749a2d8e6 Re #19 - Add "Round to half" method
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
133 }