1 | #Region "Microsoft.VisualBasic::52a567694badd8284c4ca3f55ca151fa, Microsoft.VisualBasic.Core\Extensions\Math\NumberEqualityComparer.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Class NumberEqualityComparer |
35 | ' |
36 | ' Properties: DeltaTolerance |
37 | ' |
38 | ' Constructor: (+2 Overloads) Sub New |
39 | ' Function: (+2 Overloads) Equals, GetHashCode, ToString |
40 | ' |
41 | ' |
42 | ' /********************************************************************************/ |
43 | |
44 | #End Region |
45 | |
46 | Imports System.Runtime.CompilerServices |
47 | |
48 | Namespace Math |
49 | |
50 | ''' <summary> |
51 | ''' 当两个数的误差值的绝对值小于阈值的时候认为两个数字相等 |
52 | ''' </summary> |
53 | Public Class NumberEqualityComparer : Implements IEqualityComparer(Of Double) |
54 | |
55 | Public Property DeltaTolerance As Double |
56 | |
57 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
58 | Sub New(tolerance As Double) |
59 | DeltaTolerance = tolerance |
60 | End Sub |
61 | |
62 | Sub New() |
63 | Call Me.New(0.00001) |
64 | End Sub |
65 | |
66 | Public Overrides Function ToString() As String |
67 | Return $"|a-b| <= {DeltaTolerance}" |
68 | End Function |
69 | |
70 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
71 | Public Overloads Function Equals() As GenericLambda(Of Double).IEquals |
72 | Return AddressOf Equals |
73 | End Function |
74 | |
75 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
76 | Public Overloads Function Equals(x As Double, y As Double) As Boolean Implements IEqualityComparer(Of Double).Equals |
77 | Return Math.Abs(x - y) <= _DeltaTolerance |
78 | End Function |
79 | |
80 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
81 | Public Overloads Function GetHashCode(obj As Double) As Integer Implements IEqualityComparer(Of Double).GetHashCode |
82 | Return obj.GetHashCode |
83 | End Function |
84 | End Class |
85 | |
86 | End Namespace |