1 #Region "Microsoft.VisualBasic::ee81197d8117588452030dc667220d62, Microsoft.VisualBasic.Core\Language\Value\Numeric\float.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 float
35     
36     '         Constructor: (+2 OverloadsSub New
37     '         Function: CompareTo, Equals, GetTypeCode, ToBoolean, ToByte
38     '                   ToChar, ToDateTime, ToDecimal, ToDouble, ToInt16
39     '                   ToInt32, ToInt64, ToSByte, ToSingle, (+2 OverloadsToString
40     '                   ToType, ToUInt16, ToUInt32, ToUInt64
41     '         Operators: -, (+2 Overloads) *, (+2 Overloads) /, ^, (+2 Overloads) +
42     '                    <, <=, >, >=
43     
44     
45     ' /********************************************************************************/
46
47 #End Region
48
49 Imports System.Runtime.CompilerServices
50
51 Namespace Language
52
53     ''' <summary>
54     ''' <see cref="System.Double"/>
55     ''' </summary>
56     Public Class float : Inherits Value(Of Double)
57         Implements IComparable
58         Implements IConvertible
59         Implements IEquatable(Of Double)
60
61         Sub New(x#)
62             Value = x#
63         End Sub
64
65         Sub New()
66             Me.New(0R)
67         End Sub
68
69         Public Overrides Function ToString() As String
70             Return Value
71         End Function
72
73         Public Function CompareTo(obj As ObjectAs Integer Implements IComparable.CompareTo
74             Dim type As Type = obj.GetType
75
76             If type.Equals(GetType(Double)) Then
77                 Return Value.CompareTo(DirectCast(obj, Double))
78             ElseIf type.Equals(GetType(float)) Then
79                 Return Value.CompareTo(DirectCast(obj, float).Value)
80             Else
81                 Throw New Exception($"Miss-match of type:  {GetType(float).FullName} --> {type.FullName}")
82             End If
83         End Function
84
85 #Region "Numeric operators"
86
87         ''' <summary>
88         ''' n &lt; value &lt;= n2
89         ''' 假若n 大于value,则返回最大值,上面的表达式肯定不成立
90         ''' </summary>
91         ''' <param name="n"></param>
92         ''' <param name="x"></param>
93         ''' <returns></returns>
94         Public Shared Operator <(n#, x As float) As float
95             If n >= x.Value Then
96                 Return New float(Double.MaxValue)
97             Else
98                 Return x
99             End If
100         End Operator
101
102         <MethodImpl(MethodImplOptions.AggressiveInlining)>
103         Public Shared Operator *(n#, x As float) As Double
104             Return n * x.Value
105         End Operator
106
107         Public Overloads Shared Operator +(x As float, y As float) As Double
108             Return x.Value + y.Value
109         End Operator
110
111         Public Overloads Shared Operator /(x As float, y As float) As Double
112             Return x.Value / y.Value
113         End Operator
114
115         Public Overloads Shared Operator +(x#, y As float) As Double
116             Return x + y.Value
117         End Operator
118
119         Public Overloads Shared Widening Operator CType(x As DoubleAs float
120             Return New float(x)
121         End Operator
122
123         Public Overloads Shared Operator <=(x As float, n As DoubleAs Boolean
124             Return x.Value <= n
125         End Operator
126
127         Public Overloads Shared Operator >=(x As float, n As DoubleAs Boolean
128             Return x.Value >= n
129         End Operator
130
131         <MethodImpl(MethodImplOptions.AggressiveInlining)>
132         Public Overloads Shared Operator /(x As float, n As DoubleAs Double
133             Return x.Value / n
134         End Operator
135
136         Public Shared Operator >(n As Double, x As float) As float
137             Return x
138         End Operator
139
140         Public Shared Operator ^(x As float, power As DoubleAs Double
141             Return x.Value ^ power
142         End Operator
143
144         Public Overloads Shared Narrowing Operator CType(f As float) As Double
145             Return f.Value
146         End Operator
147
148         Public Overloads Shared Operator -(a As float, b As float) As Double
149             Return a.Value - b.Value
150         End Operator
151
152         Public Overloads Shared Operator *(a As float, b As float) As Double
153             Return a.Value * b.Value
154         End Operator
155 #End Region
156
157 #Region "Implements IConvertible"
158
159         Public Function GetTypeCode() As TypeCode Implements IConvertible.GetTypeCode
160             Return TypeCode.Double
161         End Function
162
163         Public Function ToBoolean(provider As IFormatProvider) As Boolean Implements IConvertible.ToBoolean
164             If Value = 0R Then
165                 Return False
166             Else
167                 Return True
168             End If
169         End Function
170
171         Public Function ToChar(provider As IFormatProvider) As Char Implements IConvertible.ToChar
172             Return ChrW(CInt(Value))
173         End Function
174
175         Public Function ToSByte(provider As IFormatProvider) As SByte Implements IConvertible.ToSByte
176             Return CSByte(Value)
177         End Function
178
179         Public Function ToByte(provider As IFormatProvider) As Byte Implements IConvertible.ToByte
180             Return CByte(Value)
181         End Function
182
183         Public Function ToInt16(provider As IFormatProvider) As Short Implements IConvertible.ToInt16
184             Return CShort(Value)
185         End Function
186
187         Public Function ToUInt16(provider As IFormatProvider) As UShort Implements IConvertible.ToUInt16
188             Return CUShort(Value)
189         End Function
190
191         Public Function ToInt32(provider As IFormatProvider) As Integer Implements IConvertible.ToInt32
192             Return CInt(Value)
193         End Function
194
195         Public Function ToUInt32(provider As IFormatProvider) As UInteger Implements IConvertible.ToUInt32
196             Return CUInt(Value)
197         End Function
198
199         Public Function ToInt64(provider As IFormatProvider) As Long Implements IConvertible.ToInt64
200             Return CLng(Value)
201         End Function
202
203         Public Function ToUInt64(provider As IFormatProvider) As ULong Implements IConvertible.ToUInt64
204             Return CULng(Value)
205         End Function
206
207         Public Function ToSingle(provider As IFormatProvider) As Single Implements IConvertible.ToSingle
208             Return CSng(Value)
209         End Function
210
211         Public Function ToDouble(provider As IFormatProvider) As Double Implements IConvertible.ToDouble
212             Return Value
213         End Function
214
215         Public Function ToDecimal(provider As IFormatProvider) As Decimal Implements IConvertible.ToDecimal
216             Return CDec(Value)
217         End Function
218
219         Public Function ToDateTime(provider As IFormatProvider) As Date Implements IConvertible.ToDateTime
220             Return Date.FromBinary(CLng(Value))
221         End Function
222
223         Public Overloads Function ToString(provider As IFormatProvider) As String Implements IConvertible.ToString
224             Return Value.ToString(provider)
225         End Function
226
227         Public Function ToType(conversionType As Type, provider As IFormatProvider) As Object Implements IConvertible.ToType
228             Return CTypeDynamic(Value, conversionType)
229         End Function
230 #End Region
231
232 #Region "Implements IEquatable(Of Double)"
233         Public Overloads Function Equals(other As DoubleAs Boolean Implements IEquatable(Of Double).Equals
234             Return Value = other
235         End Function
236 #End Region
237
238     End Class
239 End Namespace