-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNumericExtensions.cs
More file actions
124 lines (114 loc) · 4.37 KB
/
Copy pathNumericExtensions.cs
File metadata and controls
124 lines (114 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ControlLibrary.Common
{
/// <summary>
/// Numeric utility methods used by controls. These methods are similar in
/// scope to the WPF DoubleUtil class.
/// </summary>
internal static class NumericExtensions
{
/// <summary>
/// Check if a number is zero.
/// </summary>
/// <param name="value">The number to check.</param>
/// <returns>True if the number is zero, false otherwise.</returns>
public static bool IsZero(this double value)
{
// We actually consider anything within an order of magnitude of
// epsilon to be zero
return Math.Abs(value) < 2.2204460492503131E-15;
}
/// <summary>
/// Check if a number isn't really a number.
/// </summary>
/// <param name="value">The number to check.</param>
/// <returns>
/// True if the number is not a number, false if it is a number.
/// </returns>
public static bool IsNaN(this double value)
{
// Get the double as an unsigned long
NanUnion union = new NanUnion { FloatingValue = value };
// An IEEE 754 double precision floating point number is NaN if its
// exponent equals 2047 and it has a non-zero mantissa.
ulong exponent = union.IntegerValue & 0xfff0000000000000L;
if ((exponent != 0x7ff0000000000000L) && (exponent != 0xfff0000000000000L))
{
return false;
}
ulong mantissa = union.IntegerValue & 0x000fffffffffffffL;
return mantissa != 0L;
}
/// <summary>
/// Determine if one number is greater than another.
/// </summary>
/// <param name="left">First number.</param>
/// <param name="right">Second number.</param>
/// <returns>
/// True if the first number is greater than the second, false
/// otherwise.
/// </returns>
public static bool IsGreaterThan(double left, double right)
{
return (left > right) && !AreClose(left, right);
}
/// <summary>
/// Determine if one number is less than or close to another.
/// </summary>
/// <param name="left">First number.</param>
/// <param name="right">Second number.</param>
/// <returns>
/// True if the first number is less than or close to the second, false
/// otherwise.
/// </returns>
public static bool IsLessThanOrClose(double left, double right)
{
return (left < right) || AreClose(left, right);
}
/// <summary>
/// Determine if two numbers are close in value.
/// </summary>
/// <param name="left">First number.</param>
/// <param name="right">Second number.</param>
/// <returns>
/// True if the first number is close in value to the second, false
/// otherwise.
/// </returns>
public static bool AreClose(double left, double right)
{
if (left == right)
{
return true;
}
double a = (Math.Abs(left) + Math.Abs(right) + 10.0) * 2.2204460492503131E-16;
double b = left - right;
return (-a < b) && (a > b);
}
/// <summary>
/// NanUnion is a C++ style type union used for efficiently converting
/// a double into an unsigned long, whose bits can be easily
/// manipulated.
/// </summary>
[StructLayout(LayoutKind.Explicit)]
private struct NanUnion
{
/// <summary>
/// Floating point representation of the union.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "It is accessed through the other member of the union")]
[FieldOffset(0)]
internal double FloatingValue;
/// <summary>
/// Integer representation of the union.
/// </summary>
[FieldOffset(0)]
internal ulong IntegerValue;
}
}
}