Mercurial > silverbladetech
annotate SilverlightGlimpse/SilverFlow.Controls/Extensions/MathExtensions.cs @ 64:ba89e36631bc
Latest version
author | Steven Hollidge <stevenhollidge@hotmail.com> |
---|---|
date | Sun, 22 Apr 2012 14:20:52 +0100 |
parents | 536498832a79 |
children |
rev | line source |
---|---|
63
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
1 using System; |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
2 |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
3 namespace SilverFlow.Controls.Extensions |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
4 { |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
5 /// <summary> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
6 /// Math extensions |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
7 /// </summary> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
8 public static class MathExtensions |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
9 { |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
10 /// <summary> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
11 /// Determines whether the number is NaN. |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
12 /// </summary> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
13 /// <param name="number">The number.</param> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
14 /// <returns> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
15 /// <c>true</c> if is NaN; otherwise, <c>false</c>. |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
16 /// </returns> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
17 public static bool IsNotSet(this double number) |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
18 { |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
19 return double.IsNaN(number); |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
20 } |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
21 |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
22 /// <summary> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
23 /// Determines whether the specified number is near the test number. |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
24 /// </summary> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
25 /// <param name="number">The number.</param> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
26 /// <param name="testNumber">The test number.</param> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
27 /// <param name="accuracy">Accuracy.</param> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
28 /// <returns> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
29 /// <c>true</c> if the specified number is near the testing one; otherwise, <c>false</c>. |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
30 /// </returns> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
31 public static bool IsNear(this double number, double testNumber, double accuracy) |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
32 { |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
33 return number >= (testNumber - accuracy) && number <= (testNumber + accuracy); |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
34 } |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
35 |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
36 /// <summary> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
37 /// Compares two numbers without their signs and gets absolutely minimal value. |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
38 /// </summary> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
39 /// <param name="num1">First number.</param> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
40 /// <param name="num2">Second number.</param> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
41 /// <returns>Absolutely minimal value.</returns> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
42 public static double AbsMin(double num1, double num2) |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
43 { |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
44 if (num1.IsNotSet() && num2.IsNotSet()) |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
45 return double.NaN; |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
46 |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
47 if (num1.IsNotSet()) return num2; |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
48 if (num2.IsNotSet()) return num1; |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
49 |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
50 double abs1 = Math.Abs(num1); |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
51 double abs2 = Math.Abs(num2); |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
52 |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
53 if (abs1 < abs2) return num1; |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
54 if (abs2 < abs1) return num2; |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
55 |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
56 // Abs. values are equal. Return first number |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
57 return num1; |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
58 } |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
59 |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
60 /// <summary> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
61 /// Ensures the given number is in the specified range. |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
62 /// </summary> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
63 /// <param name="number">The number.</param> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
64 /// <param name="low">The lower limit.</param> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
65 /// <param name="high">The upper limit.</param> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
66 /// <returns>A number in the specified range.</returns> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
67 public static double EnsureInRange(this double number, double low, double high) |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
68 { |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
69 if (number.IsNotSet()) return double.NaN; |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
70 |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
71 double result = Math.Max(number, low); |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
72 return Math.Min(result, high); |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
73 } |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
74 |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
75 /// <summary> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
76 /// Returns a double value if it is not a NAN, or zero. |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
77 /// </summary> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
78 /// <param name="number">The number.</param> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
79 /// <returns>Double value if it is not a NAN, or zero.</returns> |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
80 public static double ValueOrZero(this double number) |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
81 { |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
82 return double.IsNaN(number) ? 0 : number; |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
83 } |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
84 } |
536498832a79
Latest version before changing bindings to Listbox
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
85 } |