本文整理汇总了VB.NET中System.Single结构体的典型用法代码示例。如果您正苦于以下问题:VB.NET Single结构体的具体用法?VB.NET Single怎么用?VB.NET Single使用的例子?那么, 这里精选的结构体代码示例或许可以为您提供帮助。
在下文中一共展示了Single结构体的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Example
Module Example
Public Sub Main()
Dim value As Single = .2
Dim result1 As Single = value * 10
Dim result2 As Single
For ctr As Integer = 1 To 10
result2 += value
Next
Console.WriteLine(".2 * 10: {0:R}", result1)
Console.WriteLine(".2 Added 10 times: {0:R}", result2)
End Sub
End Module
输出:
.2 * 10: 2 .2 Added 10 times: 2.00000024
示例2: Example
Module Example
Public Sub Main()
Dim value As Single = 123.456
Dim additional As Single = Single.Epsilon * 1e15
Console.WriteLine($"{value} + {additional} = {value + additional}")
End Sub
End Module
输出:
123.456 + 1.401298E-30 = 123.456
示例3: Example
Module Example
Public Sub Main()
Dim values() As Single = { 10.01, 2.88, 2.88, 2.88, 9.0 }
Dim result As Single = 27.65
Dim total As Single
For Each value In values
total += value
Next
If total.Equals(result) Then
Console.WriteLine("The sum of the values equals the total.")
Else
Console.WriteLine("The sum of the values ({0}) does not equal the total ({1}).",
total, result)
End If
End Sub
End Module
输出:
The sum of the values (27.65) does not equal the total (27.65). If the index items in the Console.WriteLine statement are changed to {0:R}, the example displays the following output: The sum of the values (27.639999999999997) does not equal the total (27.64).
示例4: Example
' 导入命名空间
Imports System.IO
Module Example
Public Sub Main()
Dim sw As New StreamWriter(".\Singles.dat")
Dim values() As Single = { 3.2/1.11, 1.0/3, CSng(Math.PI) }
For ctr As Integer = 0 To values.Length - 1
sw.Write(values(ctr).ToString())
If ctr <> values.Length - 1 Then sw.Write("|")
Next
sw.Close()
Dim restoredValues(values.Length - 1) As Single
Dim sr As New StreamReader(".\Singles.dat")
Dim temp As String = sr.ReadToEnd()
Dim tempStrings() As String = temp.Split("|"c)
For ctr As Integer = 0 To tempStrings.Length - 1
restoredValues(ctr) = Single.Parse(tempStrings(ctr))
Next
For ctr As Integer = 0 To values.Length - 1
Console.WriteLine("{0} {2} {1}", values(ctr),
restoredValues(ctr),
If(values(ctr).Equals(restoredValues(ctr)), "=", "<>"))
Next
End Sub
End Module
输出:
2.882883 <> 2.882883 0.3333333 <> 0.3333333 3.141593 <> 3.141593
示例5: Example
' 导入命名空间
Imports System.IO
Module Example
Public Sub Main()
Dim sw As New StreamWriter(".\Singles.dat")
Dim values() As Single = { 3.2/1.11, 1.0/3, CSng(Math.PI) }
For ctr As Integer = 0 To values.Length - 1
sw.Write("{0:G9}{1}", values(ctr),
If(ctr < values.Length - 1, "|", ""))
Next
sw.Close()
Dim restoredValues(values.Length - 1) As Single
Dim sr As New StreamReader(".\Singles.dat")
Dim temp As String = sr.ReadToEnd()
Dim tempStrings() As String = temp.Split("|"c)
For ctr As Integer = 0 To tempStrings.Length - 1
restoredValues(ctr) = Single.Parse(tempStrings(ctr))
Next
For ctr As Integer = 0 To values.Length - 1
Console.WriteLine("{0} {2} {1}", values(ctr),
restoredValues(ctr),
If(values(ctr).Equals(restoredValues(ctr)), "=", "<>"))
Next
End Sub
End Module
输出:
2.882883 = 2.882883 0.3333333 = 0.3333333 3.141593 = 3.141593
示例6: Example
Module Example
Public Sub Main()
Dim value1 As Double = 1/3
Dim sValue2 As Single = 1/3
Dim value2 As Double = CDbl(sValue2)
Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2))
End Sub
End Module
输出:
0.33333333333333331 = 0.3333333432674408: False
示例7: Example
Module Example
Public Sub Main()
Dim value1 As Single = .3333333
Dim value2 As Single = 1/3
Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2))
End Sub
End Module
输出:
0.3333333 = 0.333333343: False
示例8: Example
Module Example
Public Sub Main()
Dim value1 As Single = 10.201438
value1 = CSng(Math.Sqrt(CSng(Math.Pow(value1, 2))))
Dim value2 As Single = CSng(Math.Pow(value1 * CSng(3.51), 2))
value2 = CSng(Math.Sqrt(value2) / CSng(3.51))
Console.WriteLine("{0} = {1}: {2}",
value1, value2, value1.Equals(value2))
Console.WriteLine()
Console.WriteLine("{0:G9} = {1:G9}", value1, value2)
End Sub
End Module
输出:
10.20144 = 10.20144: False 10.201438 = 10.2014389
示例9: Example
Module Example
Public Sub Main()
Dim value1 As Single = .3333333
Dim value2 As Single = 1/3
Dim precision As Integer = 7
value1 = CSng(Math.Round(value1, precision))
value2 = CSng(Math.Round(value2, precision))
Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2))
End Sub
End Module
输出:
0.3333333 = 0.3333333: True
示例10: Example
Module Example
Public Sub Main()
Dim one1 As Single = .1 * 10
Dim one2 As Single = 0
For ctr As Integer = 1 To 10
one2 += CSng(.1)
Next
Console.WriteLine("{0:R} = {1:R}: {2}", one1, one2, one1.Equals(one2))
Console.WriteLine("{0:R} is approximately equal to {1:R}: {2}",
one1, one2,
IsApproximatelyEqual(one1, one2, .000001))
End Sub
Function IsApproximatelyEqual(value1 As Single, value2 As Single,
epsilon As Single) As Boolean
' If they are equal anyway, just return True.
If value1.Equals(value2) Then Return True
' Handle NaN, Infinity.
If Single.IsInfinity(value1) Or Single.IsNaN(value1) Then
Return value1.Equals(value2)
Else If Single.IsInfinity(value2) Or Single.IsNaN(value2)
Return value1.Equals(value2)
End If
' Handle zero to avoid division by zero
Dim divisor As Single = Math.Max(value1, value2)
If divisor.Equals(0) Then
divisor = Math.Min(value1, value2)
End If
Return Math.Abs(value1 - value2)/divisor <= epsilon
End Function
End Module
输出:
1 = 1.00000012: False 1 is approximately equal to 1.00000012: True
示例11: Example
Module Example
Public Sub Main()
Dim value1 As Single = 1.163287e-36
Dim value2 As Single = 9.164234e-25
Dim result As Single = value1 * value2
Console.WriteLine("{0} * {1} = {2:R}", value1, value2, result)
Console.WriteLine("{0} = 0: {1}", result, result.Equals(0))
End Sub
End Module
输出:
1.163287E-36 * 9.164234E-25 = 0 0 = 0: True
示例12: Example
Module Example
Public Sub Main()
Dim value1 As Single = 3.065e35
Dim value2 As Single = 6.9375e32
Dim result As Single = value1 * value2
Console.WriteLine("PositiveInfinity: {0}",
Single.IsPositiveInfinity(result))
Console.WriteLine("NegativeInfinity: {0}",
Single.IsNegativeInfinity(result))
Console.WriteLine()
value1 = -value1
result = value1 * value2
Console.WriteLine("PositiveInfinity: {0}",
Single.IsPositiveInfinity(result))
Console.WriteLine("NegativeInfinity: {0}",
Single.IsNegativeInfinity(result))
End Sub
End Module
输出:
PositiveInfinity: True NegativeInfinity: False PositiveInfinity: False NegativeInfinity: True
示例13: Example
Module Example
Public Sub Main()
Dim values() As Object = { Byte.MinValue, Byte.MaxValue, Decimal.MinValue,
Decimal.MaxValue, Double.MinValue, Double.MaxValue,
Int16.MinValue, Int16.MaxValue, Int32.MinValue,
Int32.MaxValue, Int64.MinValue, Int64.MaxValue,
SByte.MinValue, SByte.MaxValue, UInt16.MinValue,
UInt16.MaxValue, UInt32.MinValue, UInt32.MaxValue,
UInt64.MinValue, UInt64.MaxValue }
Dim sngValue As Single
For Each value In values
If value.GetType() = GetType(Double) Then
sngValue = CSng(value)
Else
sngValue = value
End If
Console.WriteLine("{0} ({1}) --> {2:R} ({3})",
value, value.GetType().Name,
sngValue, sngValue.GetType().Name)
Next
End Sub
End Module
输出:
0 (Byte) --> 0 (Single) 255 (Byte) --> 255 (Single) -79228162514264337593543950335 (Decimal) --> -7.92281625E+28 (Single) 79228162514264337593543950335 (Decimal) --> 7.92281625E+28 (Single) -1.79769313486232E+308 (Double) --> -Infinity (Single) 1.79769313486232E+308 (Double) --> Infinity (Single) -32768 (Int16) --> -32768 (Single) 32767 (Int16) --> 32767 (Single) -2147483648 (Int32) --> -2.14748365E+09 (Single) 2147483647 (Int32) --> 2.14748365E+09 (Single) -9223372036854775808 (Int64) --> -9.223372E+18 (Single) 9223372036854775807 (Int64) --> 9.223372E+18 (Single) -128 (SByte) --> -128 (Single) 127 (SByte) --> 127 (Single) 0 (UInt16) --> 0 (Single) 65535 (UInt16) --> 65535 (Single) 0 (UInt32) --> 0 (Single) 4294967295 (UInt32) --> 4.2949673E+09 (Single) 0 (UInt64) --> 0 (Single) 18446744073709551615 (UInt64) --> 1.84467441E+19 (Single)
示例14: Example
Module Example
Public Sub Main()
Dim values() As Single = { Single.MinValue, -67890.1234, -12345.6789,
12345.6789, 67890.1234, Single.MaxValue,
Single.NaN, Single.PositiveInfinity,
Single.NegativeInfinity }
For Each value In values
Try
Dim lValue As Long = CLng(value)
Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})",
value, value.GetType().Name,
lValue, lValue.GetType().Name)
Catch e As OverflowException
Console.WriteLine("Unable to convert {0} to Int64.", value)
End Try
Try
Dim ulValue As UInt64 = CULng(value)
Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})",
value, value.GetType().Name,
ulValue, ulValue.GetType().Name)
Catch e As OverflowException
Console.WriteLine("Unable to convert {0} to UInt64.", value)
End Try
Try
Dim dValue As Decimal = CDec(value)
Console.WriteLine("{0} ({1}) --> {2} ({3})",
value, value.GetType().Name,
dValue, dValue.GetType().Name)
Catch e As OverflowException
Console.WriteLine("Unable to convert {0} to Decimal.", value)
End Try
Dim dblValue As Double = value
Console.WriteLine("{0} ({1}) --> {2} ({3})",
value, value.GetType().Name,
dblValue, dblValue.GetType().Name)
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output for conversions performed
' in a checked context:
' Unable to convert -3.402823E+38 to Int64.
' Unable to convert -3.402823E+38 to UInt64.
' Unable to convert -3.402823E+38 to Decimal.
' -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
'
' -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
' Unable to convert -67890.13 to UInt64.
' -67890.13 (Single) --> -67890.12 (Decimal)
' -67890.13 (Single) --> -67890.125 (Double)
'
' -12345.68 (Single) --> -12346 (0xFFFFFFFFFFFFCFC6) (Int64)
' Unable to convert -12345.68 to UInt64.
' -12345.68 (Single) --> -12345.68 (Decimal)
' -12345.68 (Single) --> -12345.6787109375 (Double)
'
' 12345.68 (Single) --> 12346 (0x000000000000303A) (Int64)
' 12345.68 (Single) --> 12346 (0x000000000000303A) (UInt64)
' 12345.68 (Single) --> 12345.68 (Decimal)
' 12345.68 (Single) --> 12345.6787109375 (Double)
'
' 67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
' 67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
' 67890.13 (Single) --> 67890.12 (Decimal)
' 67890.13 (Single) --> 67890.125 (Double)
'
' Unable to convert 3.402823E+38 to Int64.
' Unable to convert 3.402823E+38 to UInt64.
' Unable to convert 3.402823E+38 to Decimal.
' 3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
'
' Unable to convert NaN to Int64.
' Unable to convert NaN to UInt64.
' Unable to convert NaN to Decimal.
' NaN (Single) --> NaN (Double)
'
' Unable to convert Infinity to Int64.
' Unable to convert Infinity to UInt64.
' Unable to convert Infinity to Decimal.
' Infinity (Single) --> Infinity (Double)
'
' Unable to convert -Infinity to Int64.
' Unable to convert -Infinity to UInt64.
' Unable to convert -Infinity to Decimal.
' -Infinity (Single) --> -Infinity (Double)
' The example displays the following output for conversions performed
' in an unchecked context:
' -3.402823E+38 (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
' -3.402823E+38 (Single) --> 9223372036854775808 (0x8000000000000000) (UInt64)
' Unable to convert -3.402823E+38 to Decimal.
' -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
'
' -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
' -67890.13 (Single) --> 18446744073709483726 (0xFFFFFFFFFFFEF6CE) (UInt64)
' -67890.13 (Single) --> -67890.12 (Decimal)
' -67890.13 (Single) --> -67890.125 (Double)
'
' -12345.68 (Single) --> -12346 (0xFFFFFFFFFFFFCFC6) (Int64)
' -12345.68 (Single) --> 18446744073709539270 (0xFFFFFFFFFFFFCFC6) (UInt64)
' -12345.68 (Single) --> -12345.68 (Decimal)
' -12345.68 (Single) --> -12345.6787109375 (Double)
'
' 12345.68 (Single) --> 12346 (0x000000000000303A) (Int64)
' 12345.68 (Single) --> 12346 (0x000000000000303A) (UInt64)
' 12345.68 (Single) --> 12345.68 (Decimal)
' 12345.68 (Single) --> 12345.6787109375 (Double)
'
' 67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
' 67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
' 67890.13 (Single) --> 67890.12 (Decimal)
' 67890.13 (Single) --> 67890.125 (Double)
'
' 3.402823E+38 (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
' 3.402823E+38 (Single) --> 0 (0x0000000000000000) (UInt64)
' Unable to convert 3.402823E+38 to Decimal.
' 3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
'
' NaN (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
' NaN (Single) --> 0 (0x0000000000000000) (UInt64)
' Unable to convert NaN to Decimal.
' NaN (Single) --> NaN (Double)
'
' Infinity (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
' Infinity (Single) --> 0 (0x0000000000000000) (UInt64)
' Unable to convert Infinity to Decimal.
' Infinity (Single) --> Infinity (Double)
'
' -Infinity (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
' -Infinity (Single) --> 9223372036854775808 (0x8000000000000000) (UInt64)
' Unable to convert -Infinity to Decimal.
' -Infinity (Single) --> -Infinity (Double)