240 lines
8.1 KiB
VB.net
240 lines
8.1 KiB
VB.net
#Region "Microsoft.VisualBasic::ee81197d8117588452030dc667220d62, Microsoft.VisualBasic.Core\Language\Value\Numeric\float.vb"
|
||
|
||
' Author:
|
||
'
|
||
' asuka (amethyst.asuka@gcmodeller.org)
|
||
' xie (genetics@smrucc.org)
|
||
' xieguigang (xie.guigang@live.com)
|
||
'
|
||
' Copyright (c) 2018 GPL3 Licensed
|
||
'
|
||
'
|
||
' GNU GENERAL PUBLIC LICENSE (GPL3)
|
||
'
|
||
'
|
||
' This program is free software: you can redistribute it and/or modify
|
||
' it under the terms of the GNU General Public License as published by
|
||
' the Free Software Foundation, either version 3 of the License, or
|
||
' (at your option) any later version.
|
||
'
|
||
' This program is distributed in the hope that it will be useful,
|
||
' but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
' GNU General Public License for more details.
|
||
'
|
||
' You should have received a copy of the GNU General Public License
|
||
' along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
||
|
||
|
||
' /********************************************************************************/
|
||
|
||
' Summaries:
|
||
|
||
' Class float
|
||
'
|
||
' Constructor: (+2 Overloads) Sub New
|
||
' Function: CompareTo, Equals, GetTypeCode, ToBoolean, ToByte
|
||
' ToChar, ToDateTime, ToDecimal, ToDouble, ToInt16
|
||
' ToInt32, ToInt64, ToSByte, ToSingle, (+2 Overloads) ToString
|
||
' ToType, ToUInt16, ToUInt32, ToUInt64
|
||
' Operators: -, (+2 Overloads) *, (+2 Overloads) /, ^, (+2 Overloads) +
|
||
' <, <=, >, >=
|
||
'
|
||
'
|
||
' /********************************************************************************/
|
||
|
||
#End Region
|
||
|
||
Imports System.Runtime.CompilerServices
|
||
|
||
Namespace Language
|
||
|
||
''' <summary>
|
||
''' <see cref="System.Double"/>
|
||
''' </summary>
|
||
Public Class float : Inherits Value(Of Double)
|
||
Implements IComparable
|
||
Implements IConvertible
|
||
Implements IEquatable(Of Double)
|
||
|
||
Sub New(x#)
|
||
Value = x#
|
||
End Sub
|
||
|
||
Sub New()
|
||
Me.New(0R)
|
||
End Sub
|
||
|
||
Public Overrides Function ToString() As String
|
||
Return Value
|
||
End Function
|
||
|
||
Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
|
||
Dim type As Type = obj.GetType
|
||
|
||
If type.Equals(GetType(Double)) Then
|
||
Return Value.CompareTo(DirectCast(obj, Double))
|
||
ElseIf type.Equals(GetType(float)) Then
|
||
Return Value.CompareTo(DirectCast(obj, float).Value)
|
||
Else
|
||
Throw New Exception($"Miss-match of type: {GetType(float).FullName} --> {type.FullName}")
|
||
End If
|
||
End Function
|
||
|
||
#Region "Numeric operators"
|
||
|
||
''' <summary>
|
||
''' n < value <= n2
|
||
''' 假若n 大于value,则返回最大值,上面的表达式肯定不成立
|
||
''' </summary>
|
||
''' <param name="n"></param>
|
||
''' <param name="x"></param>
|
||
''' <returns></returns>
|
||
Public Shared Operator <(n#, x As float) As float
|
||
If n >= x.Value Then
|
||
Return New float(Double.MaxValue)
|
||
Else
|
||
Return x
|
||
End If
|
||
End Operator
|
||
|
||
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
||
Public Shared Operator *(n#, x As float) As Double
|
||
Return n * x.Value
|
||
End Operator
|
||
|
||
Public Overloads Shared Operator +(x As float, y As float) As Double
|
||
Return x.Value + y.Value
|
||
End Operator
|
||
|
||
Public Overloads Shared Operator /(x As float, y As float) As Double
|
||
Return x.Value / y.Value
|
||
End Operator
|
||
|
||
Public Overloads Shared Operator +(x#, y As float) As Double
|
||
Return x + y.Value
|
||
End Operator
|
||
|
||
Public Overloads Shared Widening Operator CType(x As Double) As float
|
||
Return New float(x)
|
||
End Operator
|
||
|
||
Public Overloads Shared Operator <=(x As float, n As Double) As Boolean
|
||
Return x.Value <= n
|
||
End Operator
|
||
|
||
Public Overloads Shared Operator >=(x As float, n As Double) As Boolean
|
||
Return x.Value >= n
|
||
End Operator
|
||
|
||
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
||
Public Overloads Shared Operator /(x As float, n As Double) As Double
|
||
Return x.Value / n
|
||
End Operator
|
||
|
||
Public Shared Operator >(n As Double, x As float) As float
|
||
Return x
|
||
End Operator
|
||
|
||
Public Shared Operator ^(x As float, power As Double) As Double
|
||
Return x.Value ^ power
|
||
End Operator
|
||
|
||
Public Overloads Shared Narrowing Operator CType(f As float) As Double
|
||
Return f.Value
|
||
End Operator
|
||
|
||
Public Overloads Shared Operator -(a As float, b As float) As Double
|
||
Return a.Value - b.Value
|
||
End Operator
|
||
|
||
Public Overloads Shared Operator *(a As float, b As float) As Double
|
||
Return a.Value * b.Value
|
||
End Operator
|
||
#End Region
|
||
|
||
#Region "Implements IConvertible"
|
||
|
||
Public Function GetTypeCode() As TypeCode Implements IConvertible.GetTypeCode
|
||
Return TypeCode.Double
|
||
End Function
|
||
|
||
Public Function ToBoolean(provider As IFormatProvider) As Boolean Implements IConvertible.ToBoolean
|
||
If Value = 0R Then
|
||
Return False
|
||
Else
|
||
Return True
|
||
End If
|
||
End Function
|
||
|
||
Public Function ToChar(provider As IFormatProvider) As Char Implements IConvertible.ToChar
|
||
Return ChrW(CInt(Value))
|
||
End Function
|
||
|
||
Public Function ToSByte(provider As IFormatProvider) As SByte Implements IConvertible.ToSByte
|
||
Return CSByte(Value)
|
||
End Function
|
||
|
||
Public Function ToByte(provider As IFormatProvider) As Byte Implements IConvertible.ToByte
|
||
Return CByte(Value)
|
||
End Function
|
||
|
||
Public Function ToInt16(provider As IFormatProvider) As Short Implements IConvertible.ToInt16
|
||
Return CShort(Value)
|
||
End Function
|
||
|
||
Public Function ToUInt16(provider As IFormatProvider) As UShort Implements IConvertible.ToUInt16
|
||
Return CUShort(Value)
|
||
End Function
|
||
|
||
Public Function ToInt32(provider As IFormatProvider) As Integer Implements IConvertible.ToInt32
|
||
Return CInt(Value)
|
||
End Function
|
||
|
||
Public Function ToUInt32(provider As IFormatProvider) As UInteger Implements IConvertible.ToUInt32
|
||
Return CUInt(Value)
|
||
End Function
|
||
|
||
Public Function ToInt64(provider As IFormatProvider) As Long Implements IConvertible.ToInt64
|
||
Return CLng(Value)
|
||
End Function
|
||
|
||
Public Function ToUInt64(provider As IFormatProvider) As ULong Implements IConvertible.ToUInt64
|
||
Return CULng(Value)
|
||
End Function
|
||
|
||
Public Function ToSingle(provider As IFormatProvider) As Single Implements IConvertible.ToSingle
|
||
Return CSng(Value)
|
||
End Function
|
||
|
||
Public Function ToDouble(provider As IFormatProvider) As Double Implements IConvertible.ToDouble
|
||
Return Value
|
||
End Function
|
||
|
||
Public Function ToDecimal(provider As IFormatProvider) As Decimal Implements IConvertible.ToDecimal
|
||
Return CDec(Value)
|
||
End Function
|
||
|
||
Public Function ToDateTime(provider As IFormatProvider) As Date Implements IConvertible.ToDateTime
|
||
Return Date.FromBinary(CLng(Value))
|
||
End Function
|
||
|
||
Public Overloads Function ToString(provider As IFormatProvider) As String Implements IConvertible.ToString
|
||
Return Value.ToString(provider)
|
||
End Function
|
||
|
||
Public Function ToType(conversionType As Type, provider As IFormatProvider) As Object Implements IConvertible.ToType
|
||
Return CTypeDynamic(Value, conversionType)
|
||
End Function
|
||
#End Region
|
||
|
||
#Region "Implements IEquatable(Of Double)"
|
||
Public Overloads Function Equals(other As Double) As Boolean Implements IEquatable(Of Double).Equals
|
||
Return Value = other
|
||
End Function
|
||
#End Region
|
||
|
||
End Class
|
||
End Namespace
|